简体   繁体   English

重新启动或升级应用程序后,我的Android小部件停止更新

[英]After rebooting or upgrading the app, my Android widget stops updating

Whenever I reboot my phone or upgrade the app (on my test device and on Android emulator) the widget stops updating until I create a new instance of the widget. 每当我重启手机或升级应用程序(在测试设备和Android模拟器上)时,小部件都会停止更新,直到我创建新的小部件实例为止。 Then both instances of the widget will start updating again. 然后,小部件的两个实例将再次开始更新。 I assume it's something with calling the onUpdate() on old WidgetIds , but I can't figure it out. 我以为是在旧的WidgetIds上调用onUpdate() ,但我无法弄清楚。 Here's a small snipped of my code. 这是我的代码的一小段。

 public class NewAppWidget extends AppWidgetProvider {

private static final String refresh = "b_refresh";

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);

    Intent intent = new Intent(context, NewAppWidget.class);
    intent.setAction(refresh);
    intent.putExtra("appWidgetId", appWidgetId);

    views.setOnClickPendingIntent(R.id.refresh, PendingIntent.getBroadcast(context,0,intent, PendingIntent.FLAG_UPDATE_CURRENT));
    appWidgetManager.updateAppWidget(appWidgetId, views);
}

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

    for (int appWidgetId : appWidgetIds) {
        updateAppWidget(context, appWidgetManager, appWidgetId);
    }
}

@Override
public void onReceive(Context context, Intent intent) {
    if(refresh.equals(intent.getAction())) {
        Toast.makeText(context, "Clicked2", Toast.LENGTH_LONG).show();
    }
}

} }

EDIT: Here's my manifest.xml 编辑:这是我的manifest.xml

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name=".NewAppWidget">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <action android:name="android.appwidget.action.EXTRA_APPWIDGET_IDS"/>
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/new_app_widget_info" />
    </receiver>

    <activity android:name=".NewAppWidgetConfigureActivity">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
        </intent-filter>
    </activity>
</application>

Add a call to super.onReceive() to your onReceive() . super.onReceive()的调用添加到onReceive()

Explanation 说明

If you look at the source for the base class onReceive() , you can see that it implements part of the framework logic for managing Widget lifecycle. 如果查看基类onReceive()的源代码,您会发现它实现了用于管理Widget生命周期的框架逻辑的一部分。 (Also hinted at by the docs ). (也由docs提示 )。 It handles APPWIDGET_UPDATE and is, in fact, what's responsible for calling onUpdate() in the first place. 它处理APPWIDGET_UPDATE ,实际上,它APPWIDGET_UPDATE负责调用onUpdate() (Eg, when the system boots up, and it needs to draw your initial widget, it sends your app an APPWIDGET_UPDATE , which gets passed to onReceive() ). (例如,当系统启动时,它需要绘制您的初始小部件,它将向您的应用程序发送APPWIDGET_UPDATE ,并将其传递给onReceive() )。 So, I'm not 100% sure how onUpdate() was ever getting called, in your case, but I assume you have some code somewhere else that calls updateAppWidget() , and that's the only reason your widgets appeared to work even momentarily. 因此,对于您的情况,我不确定100%如何onUpdate() onUpdate() ,但是我假设您在其他地方有一些代码调用了updateAppWidget() ,这是您的小部件似乎可以瞬间工作的唯一原因。

暂无
暂无

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

相关问题 我的android应用“意外停止” - My android app “stops unexpectedly” APP Widget Textview 未更新(Java,android 工作室) - APP Widget Textview not updating (Java, android studio) Asterisk / Java:重新启动Java应用程序后如何获得呼叫的开始时间? - Asterisk/Java: How do I get the start time of a call after rebooting my Java app? 我的android主屏幕小部件出现了,但没有更新 - My android homescreen widget appears, but is not updating 窗口小部件停止工作后,我关闭了我的应用 - Widget stops working atfter I close my app 更新到 androidx 后:“膨胀 class android.support.design.widget.NavigationView 时出错。” 为什么应用程序崩溃? - After Updating to androidx : 'Error inflating class android.support.design.widget.NavigationView.' Why is the app crashing? Android 位置在一段时间后停止更新位置 - Android location stops updating location after some time 为什么可运行对象在我的小部件中停止? - Why the runnable stops in my widget? 通过SQLite Manager更新SQLite数据库列之后,为什么更改未反映在Android App中? - After updating my SQLite database columns via SQLite Manager, why are the changes not reflected in my Android App? 错误:升级Android SDK工具后,“找不到以下类:-android.support.v7.widget.GridLayout” - Error: “The following classes could not be found: - android.support.v7.widget.GridLayout” after upgrading Android SDK Tools
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM