应用程序进程终止后无法单击 Android 小部件
Can't click on Android widget after app process dies
我编写了一个带有两个按钮的 Android
小部件。如果我启动 "Activity" 一切正常,我可以单击这些按钮。但是如果我锁定 phone,进程会在几秒后终止
然后我尝试再次单击这些按钮,没有任何反应,除了 LOGCAT
:
I/ActivityManager: filter receiver for action = ButtonRefresh
updateAppWidget
,通过 @Override onUpdate(...)
:
调用
private final static String BUTTON_REFRESH = "ButtonRefresh";
public static void updateAppWidget(final Context context, final AppWidgetManager appWidgetManager,
final int appWidgetId) {
final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.myclasslayout);
//Click listener depending on @this (getPendingSelfIntent)
views.setOnClickPendingIntent(R.id.button_refresh, getPendingSelfIntent(context, BUTTON_REFRESH));
[...]
appWidgetManager.updateAppWidget(appWidgetId, views);
}
getPendingSelfIntent()
:
private static PendingIntent getPendingSelfIntent(Context context, String action) {
Intent intent = new Intent(context, MyClass.class);
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
onReceive()
:
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (BUTTON_REFRESH.equals(intent.getAction())) {
Log.i("MyLogtag", "Refresh"); //This never shows up in LOGCAT
}
}
AndroidManifest.xml
:
[...]
<receiver android:name=".MyClass">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/myclass_info" />
</receiver>
[...]
onReceive never 在应用进程 dead
...
时被调用
我明白了...
我必须改变
private final static String BUTTON_REFRESH = "ButtonRefresh";
到
private final static String BUTTON_REFRESH = "com.example.mypackage.BUTTON_REFRESH";
和AndroidManifest
:
<receiver android:name=".MyClass">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.example.mypackage.BUTTON_REFRESH"/>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/myclass_info" />
</receiver>
我编写了一个带有两个按钮的 Android
小部件。如果我启动 "Activity" 一切正常,我可以单击这些按钮。但是如果我锁定 phone,进程会在几秒后终止
然后我尝试再次单击这些按钮,没有任何反应,除了 LOGCAT
:
I/ActivityManager: filter receiver for action = ButtonRefresh
updateAppWidget
,通过 @Override onUpdate(...)
:
private final static String BUTTON_REFRESH = "ButtonRefresh";
public static void updateAppWidget(final Context context, final AppWidgetManager appWidgetManager,
final int appWidgetId) {
final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.myclasslayout);
//Click listener depending on @this (getPendingSelfIntent)
views.setOnClickPendingIntent(R.id.button_refresh, getPendingSelfIntent(context, BUTTON_REFRESH));
[...]
appWidgetManager.updateAppWidget(appWidgetId, views);
}
getPendingSelfIntent()
:
private static PendingIntent getPendingSelfIntent(Context context, String action) {
Intent intent = new Intent(context, MyClass.class);
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
onReceive()
:
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (BUTTON_REFRESH.equals(intent.getAction())) {
Log.i("MyLogtag", "Refresh"); //This never shows up in LOGCAT
}
}
AndroidManifest.xml
:
[...]
<receiver android:name=".MyClass">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/myclass_info" />
</receiver>
[...]
onReceive never 在应用进程 dead
...
我明白了...
我必须改变
private final static String BUTTON_REFRESH = "ButtonRefresh";
到
private final static String BUTTON_REFRESH = "com.example.mypackage.BUTTON_REFRESH";
和AndroidManifest
:
<receiver android:name=".MyClass">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.example.mypackage.BUTTON_REFRESH"/>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/myclass_info" />
</receiver>