简体   繁体   中英

Call IntentService from BroadcastReceiver in Android Wear

Is it possible to start a service from broadcast receiver in Android wear? I was able to start service from activity.

I tried the same:

Intent i = new Intent(context, ServiceToSendData.class);
context.startService(i);

I am getting the following error:

     java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.SCREEN_OFF flg=0x50000010 } in com.ysz.demo.ScreenDetectionReceiver@b3d20fc0
        at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:769)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5026)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at android.content.ComponentName.<init>(ComponentName.java:77)
        at android.content.Intent.<init>(Intent.java:3848)
       at com.ysz.demo.ScreenDetectionReceiver.trigger(ScreenDetectionReceiver.java:68)
       at      com.ysz.demo.ScreenDetectionReceiver.checktimeandsendtoshake(ScreenDetectionReceiver.java:60)  at com.ysz.demo.ScreenDetectionReceiver.onReceive(ScreenDetectionReceiver.java:41)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:759)

Looking at the call stack, the only possible explanation for this exception is that context is null.

Intent i = new Intent(context, ServiceToSendData.class);

...

public Intent(Context packageContext, Class<?> cls) {
    mComponent = new ComponentName(packageContext, cls);  // <- Line 3848
}

...

public ComponentName(Context pkg, Class<?> cls) {
    mPackage = pkg.getPackageName();  // <- Line 77
    mClass = cls.getName();
}

Is that the case?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM