LittleFunny
LittleFunny

Reputation: 8405

Why does BroadcastReceiver not get called?

The BootReceiver never called even if the system was boot up using this command:

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -n com.android.canbedeleted.test/.BootReceiver

Permission I have put in the manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.android.canbedeleted.test">
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:label="test">
<receiver android:name="test.Droid.BootReceiver">
  <intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED" />
  </intent-filter>
</receiver>
</application>

The receiver tag i have tried removed it but don't see any different.

[BroadcastReceiver]
[IntentFilter (new string[] { Intent.ActionBootCompleted }, Priority = (int)IntentFilterPriority.HighPriority)]
public class BootReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    { 
        System.Diagnostics.Debug.WriteLine ("*********************************** Broadcast Received *********************************");
        Toast.MakeText(context, "Received intent!", ToastLength.Short).Show();
        //Intent message = new Intent();
        //message.AddFlags(ActivityFlags.NewTask);
        //message.SetClass(context, typeof(KeyboardService));
        //context.StartService(intent);
    }
}

Even if I tried to type the cmd like this, i will have stack trace as below. The test.Droid is the namespace of the class.

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -n com.android.canbedeleted.test/test.Droid.BootReceiver

StackTrace

Java.Lang.RuntimeException: Unable to instantiate receiver test.Droid.BootReceiver: java.lang.ClassNotFoundException: Didn't find class "test.Droid.BootReceiver" on path: DexPathList[[zip file "/data/app/com.android.canbedeleted.test-8.apk"],nativeLibraryDirectories=[/data/app-lib/com.android.canbedeleted.test-8, /vendor/lib, /system/lib]] ---> Java.Lang.ClassNotFoundException: Didn't find class "test.Droid.BootReceiver" on path: DexPathList[[zip file "/data/app/com.android.canbedeleted.test-8.apk"],nativeLibraryDirectories=[/data/app-lib/com.android.canbedeleted.test-8, /vendor/lib, /system/lib]] at java.lang.ClassNotFoundException: Didn't find class "test.Droid.BootReceiver" on path: DexPathList[[zip file "/data/app/com.android.canbedeleted.test-8.apk"],nativeLibraryDirectories=[/data/app-lib/com.android.canbedeleted.test-8, /vendor/lib, /system/lib]] at at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:67) at at java.lang.ClassLoader.loadClass(ClassLoader.java:497) at at java.lang.ClassLoader.loadClass(ClassLoader.java:457) at at android.app.ActivityThread.handleReceiver(ActivityThread.java:2513) at at android.app.ActivityThread.access$1800(ActivityThread.java:161) at at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341) at at android.os.Handler.dispatchMessage(Handler.java:102) at at android.os.Looper.loop(Looper.java:157) at at android.app.ActivityThread.main(ActivityThread.java:5356) at at java.lang.reflect.Method.invokeNative(Native Method) at at java.lang.reflect.Method.invoke(Method.java:515) at at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265) at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) at at dalvik.system.NativeStart.main(Native Method) --- End of inner exception stack trace --- at java.lang.RuntimeException: Unable to instantiate receiver test.Droid.BootReceiver: java.lang.ClassNotFoundException: Didn't find class "test.Droid.BootReceiver" on path: DexPathList[[zip file "/data/app/com.android.canbedeleted.test-8.apk"],nativeLibraryDirectories=[/data/app-lib/com.android.canbedeleted.test-8, /vendor/lib, /system/lib]] at at android.app.ActivityThread.handleReceiver(ActivityThread.java:2518) at at android.app.ActivityThread.access$1800(ActivityThread.java:161) at at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341) at at android.os.Handler.dispatchMessage(Handler.java:102) at at android.os.Looper.loop(Looper.java:157) at at android.app.ActivityThread.main(ActivityThread.java:5356) at at java.lang.reflect.Method.invokeNative(Native Method) at at java.lang.reflect.Method.invoke(Method.java:515) at at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265) at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) at at dalvik.system.NativeStart.main(Native Method) at Caused by: java.lang.ClassNotFoundException: Didn't find class "test.Droid.BootReceiver" on path: DexPathList[[zip file "/data/app/com.android.canbedeleted.test-8.apk"],nativeLibraryDirectories=[/data/app-lib/com.android.canbedeleted.test-8, /vendor/lib, /system/lib]] at at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:67) at at java.lang.ClassLoader.loadClass(ClassLoader.java:497) at at java.lang.ClassLoader.loadClass(ClassLoader.java:457) at at android.app.ActivityThread.handleReceiver(ActivityThread.java:2513) at ... 10 more

Upvotes: 1

Views: 780

Answers (2)

Sven-Michael St&#252;be
Sven-Michael St&#252;be

Reputation: 14760

Please remove the receiver in your AndroidManifest.xml. You don't have to declare it there if you are using attributes like BroadcastReceiver and IntentFilter. Else the generated manifest (\obj\Debug\android\AndroidManifest.xml) will contain two declarations of the receiver and this might cause some issues. Using the attributes has the advantage, that you don't have to care about the correct class names.

Upvotes: 1

Prashant Cholachagudda
Prashant Cholachagudda

Reputation: 13090

This com.android.canbedeleted.test/test.Droid.BootReceiver may not be correct.

With the 5.0 release, the default package names for Android Callable Wrappers will be based on the MD5SUM of the assembly-qualified name of the type being exported. This allows the same fully-qualified name to be provided from two different assemblies and not get a packaging error.

Check what is the generated BootReceiver name, under your project's \obj\Debug\android\AndroidManifest.xml file.

Read more about Android Callable Wrapper Naming topic here

Upvotes: 0

Related Questions