窗口小部件无法正常启动之后,应用程序已被强制停止 [英] Widget fails to launch properly after app has been force-stopped

查看:109
本文介绍了窗口小部件无法正常启动之后,应用程序已被强制停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个窗口小部件发射,以基于各种插件按钮pressed特定页面。它工作正常,当我有应用程序在内存中(即从部署(运行/调试),但是当我强制停止应用程序,窗口小部件不能正常工作。

当应用程序已经停止生效,小部件的第一次点击正常工作,并且为的onCreate相应的活动被调用。然后,当主屏幕键是pressed,和不同的插件按钮是pressed中,previous插件活性的onRestart方法被调用。

奇怪的是,B / C的日志消息不会出现,因为调试器不能连接,它已经相当困难的考验。有没有什么特别的,我做错了什么?

该流程 - >的 WidgetDispatchActivity 的(处理真正的小部件目标) - > ViewActivity > - >进一步分支( DetailViewActivity 在下面的例子)

的onupdate 的小工具:

 的OnUpdate(){
...


       意图viewIntent =新的意图(背景下,WidgetDispatchActivity.class);
                    viewIntent.putExtra(发射,WidgetInfo.VIEW_ACTIVITY);
                    PendingIntent viewPendIntent = PendingIntent.getActivity(上下文,2,viewIntent,PendingIntent.FLAG_UPDATE_CURRENT);

...
}
 

WidgetDispatchActivity的的onCreate

 叠B = getIntent()getExtras()。
        如果(B =空&安培;!&安培; b.isEmpty()){
            Log.i(LOG_TAG,空上捆绑WIDGET!);
        }其他{
            Log.i(LOG_TAG,小工具包都有项目!);
        }

        字符串目标= NULL;
        如果(B = NULL和放大器;!&安培; b.containsKey(发射)){
            目的地= b.getString(发射);
            Log.i(LOG_TAG,WIDGET产品从束:+目的地);
        }
        其他{
            Log.i(LOG_TAG,\启动\项目没有内包!);
        }

        如果(目标!= NULL){
            Log.i(LOG_TAG,PendingIntent从部件收到!);
            意图GOTO =新意图(这一点,ViewActivity.class);
               ...
}
}
 

LogCat中

  10-10 20:38:00.935:信息/ ActivityManager(58):启动活动:意向{行为= android.intent.action.MAIN猫= [android.intent.category 。首页] FLG = 0x10200000 CMP = com.android.launcher / com.android.launcher2.Launcher}
10-10 20:38:00.956:信息/ DetailViewActivity(1154):在onPause
10-10 20:38:00.975:WARN / InputManagerService(58):忽略的hideSoftInput:com.android.internal.view.IInputMethodClient$Stub$Proxy@450366d0
10-10 20:38:02.545:信息/ ActivityManager(58):启动活动:意向{FLG = 0x10000000处CMP = com.starbucks.mobilecard / com.xxx.xxx.widget.WidgetDispatchActivity bnds = [198298] [224321](有群众演员)}
10-10 20:38:02.585:信息/ ViewActivity(1154):ONRESTART
10-10 20:38:02.585:信息/ ViewActivity(1154):WIDGET征:: ==假
 

ViewActivity onRestart:

 保护无效onRestart(){
        super.onRestart();
        Log.i(LOG_TAGONRESTART);

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

AndroidManifest:

 <活动机器人:名称=com.xxx.xxx.ui.cards.ViewActivity机器人:标签=@字符串/ _view_title机器人:finishOnTaskLaunch =真正的机器人:clearTaskOnLaunch =真正的机器人:screenOrientation =画像/>

<活动机器人:名称=com.xxx.xxx.widget.WidgetDispatchActivity机器人:标签=@字符串/ widget_dispatch_title机器人:finishOnTaskLaunch =真/>
 

解决方案

Willmel,

在从当你强迫关闭正在执行你的工作的onDestroy停止你的部件的第一步()。在这里,你会想确保你跟你的WidgetHost,使您的Widget通信HostViews和RemoteViews。

该语句接收来自Android的任何事件停止WidgetHost。 MYHOST 是对象,往往是抱着引用您的小工具。

一个活动

  //从坐立不安停止小工具
    myHost.stopListening();
    MYHOST = NULL;
 

从那里,你会想删除任何提及任何HostViews的,你可能有一个参考。这是通过简单地抓住参考并将其设置为完成。在我的的onDestroy()我用下面的循环......

 如果(appWidgets!= NULL){
    最终诠释计数= appWidgets.size();
    的for(int i = 0; I<计数;我++){
        最终的Widget launcherInfo = appWidgets.get(ⅰ);
        launcherInfo.hostView = NULL;
    }
}
 

在这里, launcherInfo 是我的扩展AppWidget对象。 hostView 指的是我AppWidgetHostView(这是重新presents我的窗口小部件的视图)。该 appWidgets 数组是我的小工具,我的发射器正在跟踪的列表。 pretty的简单在所有。

与窗口小部件和WidgetHosts工作可能会出现问题,这取决于你的设置。在正确的地方上面的语句确实有助于缓解痛苦。

FuzzicalLogic

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 (i.e. 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. Then, when the home key is pressed, and a different widget button is pressed, the onRestart method of the previous widget Activity is called.

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. 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)

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:

 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

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:

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

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

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,

The first step in stopping your widgets from working when you Force Close is implementing your onDestroy(). Here you will want to make sure you talk to your WidgetHost, the HostViews and the RemoteViews that make up your Widget communications.

This statement stops the WidgetHost from receiving any events from Android. myHost is the object, often an Activity that is holding the references to your Widget.

// 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. This is done by simply grabbing the reference and setting it to null. In my onDestroy() I use the following loop...

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. hostView refers to my AppWidgetHostView (which is the view that represents my Widget). The appWidgets array is my list of Widgets that my launcher is tracking. Pretty simple over all.

Working with Widgets and WidgetHosts can be problematic, depending on your set up. The above statements in the right place really help to ease the suffering.

FuzzicalLogic

这篇关于窗口小部件无法正常启动之后,应用程序已被强制停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆