简体   繁体   English

应用程序强制停止后,Widget无法正常启动

[英]Widget fails to launch properly after app has been force-stopped

I have a widget which launches to a particular page based on the various widget buttons pressed. 我有一个小部件,它根据按下的各种小部件按钮启动到特定页面。 It works fine when I have the app in-memory (ie from deploy (run / debug), however when I force stop the app, the widget no longer functions properly. 当我将应用程序内存(即从部署(运行/调试))时,它工作正常,但是当我强制停止应用程序时,窗口小部件不再正常运行。

When the app has been force stopped, the first click of the widget works properly, and the onCreate for the corresponding Activity is called. 当应用程序被强制停止时,第一次单击该窗口小部件正常工作,并调用相应活动的onCreate。 Then, when the home key is pressed, and a different widget button is pressed, the onRestart method of the previous widget Activity is called. 然后,当按下主页键并按下不同的小部件按钮时,将调用前一个小部件Activity的onRestart方法。

It is odd, b/c the log messages do not appear, and since the debugger cannot be connected, it's been rather difficult to test. 奇怪的是,b / c没有出现日志消息,并且由于调试器无法连接,因此测试起来相当困难。 Is there anything in particular that I'm doing wrong? 我有什么特别的错误吗?

The flow is --> WidgetDispatchActivity (handle true widget destination) --> ViewActivity > --> further branch ( DetailViewActivity in below example) 流程是 - > WidgetDispatchActivity (处理真正的小部件目标) - > ViewActivity > - >进一步分支(下面的例子中的DetailViewActivity

onUpdate for widget: onUpdate for widget:

onUpdate(){
...


       Intent viewIntent = new Intent(context, WidgetDispatchActivity.class);
                    viewIntent.putExtra("launch", WidgetInfo.VIEW_ACTIVITY);
                    PendingIntent viewPendIntent = PendingIntent.getActivity(context, 2, viewIntent, PendingIntent.FLAG_UPDATE_CURRENT);

...
}

WidgetDispatchActivity's onCreate : WidgetDispatchActivity的onCreate

 Bundle b = getIntent().getExtras();
        if (b != null && b.isEmpty()){
            Log.i(LOG_TAG, "EMPTY BUNDLE ON WIDGET!");
        }else{
            Log.i(LOG_TAG, "WIDGET BUNDLE HAS ITEMS!");
        }

        String destination = null;
        if (b != null && b.containsKey("launch")){
            destination = b.getString("launch");
            Log.i(LOG_TAG, "WIDGET ITEM FROM BUNDLE WAS: " + destination);
        }
        else{
            Log.i(LOG_TAG, " \"launch\" item was not inside bundle!");
        }

        if(destination != null) {
            Log.i(LOG_TAG, "PendingIntent received from widget!");
            Intent goTo = new Intent(this, ViewActivity.class);
               ...
}
}

LogCat logcat的

10-10 20:38:00.935: INFO/ActivityManager(58): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10200000 cmp=com.android.launcher/com.android.launcher2.Launcher }
10-10 20:38:00.956: INFO/DetailViewActivity(1154): ONPAUSE
10-10 20:38:00.975: WARN/InputManagerService(58): Ignoring hideSoftInput of: com.android.internal.view.IInputMethodClient$Stub$Proxy@450366d0
10-10 20:38:02.545: INFO/ActivityManager(58): Starting activity: Intent { flg=0x10000000 cmp=com.starbucks.mobilecard/com.xxx.xxx.widget.WidgetDispatchActivity bnds=[198,298][224,321] (has extras) }
10-10 20:38:02.585: INFO/ViewActivity(1154): ONRESTART
10-10 20:38:02.585: INFO/ViewActivity(1154): WIDGET LAUNCH:: == false

ViewActivity onRestart: ViewActivity onRestart:

protected void onRestart(){
        super.onRestart();
        Log.i(LOG_TAG,"ONRESTART");

        Log.i(LOG_TAG,"WIDGET LAUNCH:: == " + WidgetInfo.getInstance().wasLaunchedFromWidget());
                 ...
}

AndroidManifest: AndroidManifest:

<activity android:name="com.xxx.xxx.ui.cards.ViewActivity" android:label="@string/_view_title" android:finishOnTaskLaunch="true" android:clearTaskOnLaunch="true" android:screenOrientation="portrait"/>

<activity android:name="com.xxx.xxx.widget.WidgetDispatchActivity" android:label="@string/widget_dispatch_title" android:finishOnTaskLaunch="true"/>

Willmel, Willmel,

The first step in stopping your widgets from working when you Force Close is implementing your onDestroy(). 当Force Close实现你的onDestroy()时,阻止你的小部件工作的第一步。 Here you will want to make sure you talk to your WidgetHost, the HostViews and the RemoteViews that make up your Widget communications. 在这里,您需要确保与构成Widget通信的WidgetHost,HostViews和RemoteView进行通信。

This statement stops the WidgetHost from receiving any events from Android. 此语句阻止WidgetHost从Android接收任何事件。 myHost is the object, often an Activity that is holding the references to your Widget. myHost是对象,通常是一个包含对Widget的引用的Activity。

// Stop the Widgets from fidgeting
    myHost.stopListening();
    myHost = null;

From there, you'll want to remove any reference to any of the HostViews you might have a reference to. 从那里,您将要删除对您可能引用的任何HostView的任何引用。 This is done by simply grabbing the reference and setting it to null . 这是通过简单地抓取引用并将其设置为null In my onDestroy() I use the following loop... 在我的onDestroy()我使用以下循环...

if (appWidgets != null) {
    final int count = appWidgets.size();
    for (int i = 0; i < count; i++) {
        final Widget launcherInfo = appWidgets.get(i);
        launcherInfo.hostView = null;
    }
}

In here, launcherInfo is my extended AppWidget object. 在这里, launcherInfo是我的扩展AppWidget对象。 hostView refers to my AppWidgetHostView (which is the view that represents my Widget). hostView引用我的AppWidgetHostView(这是代表我的Widget的视图)。 The appWidgets array is my list of Widgets that my launcher is tracking. appWidgets数组是我的启动器正在跟踪的小部件列表。 Pretty simple over all. 非常简单。

Working with Widgets and WidgetHosts can be problematic, depending on your set up. 使用Widgets和WidgetHosts可能会有问题,具体取决于您的设置。 The above statements in the right place really help to ease the suffering. 在正确的地方上面的陈述真的有助于减轻痛苦。

FuzzicalLogic FuzzicalLogic

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使应用程序被强制停止,也要重新启动服务,即使关闭应用程序,也要在后台继续运行服务如何? - Restart the service even if app is force-stopped and Keep running service in background even after closing the app How? 即使应用程序被强制停止,也要重新启动服务;关闭应用程序后,仍要在后台运行服务。 - Restart the service even if app is force-stopped and Keep running service in background even after closing the app How? Android应用在启动后立即停止 - Android app stopped right after launch 不幸的是,如果没有互联网访问,应用程序已停止 - Unfortunately App Has Been Stopped If No Internet Access 应用程序在首次启动后停止,但没有错误 - App stopped after first launch, but no errors tomcat已经停止了 - tomcat has been stopped 不幸的是,添加ADMOB后APP已停止 - Unfortunately, APP Has Stopped after added ADMOB 程序停止后,如何在布尔值中保持特定值? - How to maintain a particular value in boolean after the program has been stopped? 即使在Websphere中停止了应用程序之后,线程仍保持运行 - Thread keeps running even after application has been stopped in Websphere 不幸的是,小部件已停止运行android - Widget unfortunately has stopped android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM