1

我试图动态加载 BroadcastReceiver 类并在这个问题上停留了超过 2 周。它让我发疯,项目快到了。

任何帮助将不胜感激。

我使用这段代码来调用 dex 类加载器:

DexClassLoader classLoader = new DexClassLoader(dexPath.getAbsolutePath(), optimizedDirectory.getAbsolutePath(), null, getClassLoader() );
                Class<?> dcl;
                dcl = classLoader.loadClass("com.example.mydexbuilddemo.action.UseRun");
                WorkFlow runControl = (WorkFlow) dcl.newInstance();
                runControl.runMyWork(v.getContext());

然后上面的代码调用这个类:

public class UseRun implements WorkFlow {@Override
public void runMyWork(Context context) {
    // TODO Auto-generated method stub
    Echo.showEcho(context);
    context.sendBroadcast(new Intent(context, MyOnBootReceiver.class));
}}

然后这个类 UserRun 调用 MyOnBootReceiver.class

因此 DexClassLoader 成功加载了 UserRun 类,但随后 UserRun 类无法调用 MyonBootReceiver.class。

在 logcat 中,执行 UserRun 类后它不会返回任何错误或任何内容。

它成功表明:

Echo.showEcho(context);

但是这不会加载:

context.sendBroadcast(new Intent(context, MyOnBootReceiver.class));

MyOnBootReceiver.class 内容:

public class MyOnBootReceiver extends BroadcastReceiver {@Override
public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "MY TOAST SHOW", Toast.LENGTH_SHORT).show();
}   }

我按照本教程来构建这个项目。

http://android-developers.blogspot.com/2011/07/custom-class-loading-in-dalvik.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+blogspot%2FhsDu+%28Android+Developers+Blog%29

我错过了什么吗,理论上这应该有效

这里还有 Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mydexbuilddemo"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
     >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
     <receiver android:name="com.example.demo2.act.MyOnBootReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>       


</application>

来自 Shelvin 建议的 LOG CAT(错误):

 00:20:44.783: D/dalvikvm(6997): ODEX file is stale or bad; removing and retrying (/data/data/com.example.mydexbuilddemo/app_dexout/secondary_dex.dex)
 00:20:44.803: D/dalvikvm(6997): DexOpt: --- BEGIN 'secondary_dex.jar' (bootstrap=0) ---
 00:20:45.163: D/dalvikvm(6997): DexOpt: --- END 'secondary_dex.jar' (success) ---
 00:20:45.163: D/dalvikvm(6997): DEX prep '/data/data/com.example.mydexbuilddemo/app_dex/secondary_dex.jar': unzip in 19ms, rewrite 360ms
 00:21:09.837: D/dalvikvm(9886): GC_FOR_ALLOC freed 84K, 8% free 17207K/18536K, paused 15ms, total 15ms
 00:21:09.877: D/dalvikvm(9886): GC_CONCURRENT freed <1K, 7% free 19509K/20844K, paused 3ms+3ms, total 16ms
 00:21:10.017: D/libEGL(9886): loaded /system/lib/egl/libEGL_adreno200.so
 00:21:10.017: D/libEGL(9886): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
 00:21:10.017: D/libEGL(9886): loaded /system/lib/egl/libGLESv2_adreno200.so
 00:21:10.077: D/OpenGLRenderer(9886): Enabling debug mode 0
 00:21:15.803: D/GestureDetector(9886): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 11 mFalseSizeCnt:0
 00:21:15.803: D/USE RUN(9886): use my run work 
 00:23:10.225: D/GestureDetector(9886): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 13 mFalseSizeCnt:0
4

0 回答 0