开发者

Android Widget + Service

I'm having problems calling a class function from my Widget as the result of a service action.

public class HelloWidget extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
        int[] appWidgetIds) {

        Intent intent = new Intent(context, NeoService.class);
        context.startService(intent);

        MyClass mClass = new MyClass(context, appWidgetManager);
        mClass.doUpdate();

        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }
}

The call to mClass.doUpdate() above works just fine. But then when I try to do it again in my onReceive it doesn't work and I get a forceclose as soon as the correct action is received:

@Override
    public void onReceive(Context context, Intent intent) {

     if(Intent.ACTION_TIME_TICK.equals(action))
    {
        mTime.doUpdate();
    //  Toast.makeText(context, "TicK Tock!", Toast.LENGTH_LONG).show();
    }
}

If I comment out the mTime.doUpdate(); and uncomment the Toast it displays the toast just fine as it should.

Edit: The error message I get is as follows:

11-18 18:25:00.201: ERROR/AndroidRuntime(2759): FATAL EXCEPTION: main
11-18 18:25:00.201: ERROR/AndroidRuntime(2759): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.TIME_TICK flg=0x40000004 (has extras) } in de.thesmile.android.widget.HelloWidget@48411298
11-18 18:25开发者_运维百科:00.201: ERROR/AndroidRuntime(2759):     at android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:905)
11-18 18:25:00.201: ERROR/AndroidRuntime(2759):     at android.os.Handler.handleCallback(Handler.java:587)
11-18 18:25:00.201: ERROR/AndroidRuntime(2759):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-18 18:25:00.201: ERROR/AndroidRuntime(2759):     at android.os.Looper.loop(Looper.java:123)
11-18 18:25:00.201: ERROR/AndroidRuntime(2759):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11-18 18:25:00.201: ERROR/AndroidRuntime(2759):     at java.lang.reflect.Method.invokeNative(Native Method)
11-18 18:25:00.201: ERROR/AndroidRuntime(2759):     at java.lang.reflect.Method.invoke(Method.java:521)
11-18 18:25:00.201: ERROR/AndroidRuntime(2759):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
11-18 18:25:00.201: ERROR/AndroidRuntime(2759):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
11-18 18:25:00.201: ERROR/AndroidRuntime(2759):     at dalvik.system.NativeStart.main(Native Method)
11-18 18:25:00.201: ERROR/AndroidRuntime(2759): Caused by: java.lang.NullPointerException
11-18 18:25:00.201: ERROR/AndroidRuntime(2759):     at de.thesmile.android.widget.HelloWidget.onReceive(HelloWidget.java:45)
11-18 18:25:00.201: ERROR/AndroidRuntime(2759):     at android.app.ActivityThread$PackageInfo$ReceiverDispatcher$Args.run(ActivityThread.java:892)


Your problem results from the BroadcastReceiver lifecycle. The AppWidgetProvider is a BroadcastReceiver and the BroadcastReceiver is considered active only while proceeding the callback method. When the BroadcastReceiver is inactive (so after the callback method return) your process may be killed at any time if the system needs memory. When the process is killed, your mTime object is killed too, what causes the NullPointerException.

It is nicely explained here: (especially the second "Note:") http://developer.android.com/guide/topics/appwidgets/index.html#AppWidgetProvider

and here: http://developer.android.com/guide/topics/fundamentals.html#broadlife

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜