在Android中发言。我有一个应用程序和一个小部件。该小部件应该每n秒自动更新一次,它可以正常运行,直到父应用程序被终止。应用程序被杀后,它不再更新。这在基于计时器的更新和onClickPendingIntent更新上都会发生。
有没有办法检查父应用程序是否在窗口小部件中运行,以及它是否未运行以启动它? (最好不要使用getRunningTasks(或任何其他额外权限),但如果我必须这样做那么好)
这里是我的小部件代码的重要部分,为了进行调试,它将显示两个数字,一个基于计时器更新的数字和一个在按键上更新的数字。小部件基于计时器绘制。
private static final String SYNC_CLICKED = "automaticWidgetSyncButtonClick";
public static int counter = 0; // time based update
public static int counter2 = 0; // onClick update
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Timer timer = new Timer();
// 2 seconds for debug
timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 2000);
RemoteViews remoteViews;
ComponentName watchWidget;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_main);
watchWidget = new ComponentName(context, HelloWidget.class);
remoteViews.setOnClickPendingIntent(R.id.widget_textview, getPendingSelfIntent(context, SYNC_CLICKED));
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
}
protected PendingIntent getPendingSelfIntent(Context context, String action) {
Intent intent = new Intent(context, getClass());
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (SYNC_CLICKED.equals(intent.getAction())) {
counter2++;
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews;
ComponentName watchWidget;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_main);
watchWidget = new ComponentName(context, WidgetUpdater.class);
remoteViews.setTextViewText(R.id.widget_textview, "Updating...");
// unrelated async task here
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
}
}
private class MyTime extends TimerTask {
RemoteViews widgetView;
AppWidgetManager appWidgetManager;
SharedPreferences settings;
ComponentName thisWidget;
public MyTime(Context context, AppWidgetManager appWidgetManager) {
this.appWidgetManager = appWidgetManager;
settings = context.getSharedPreferences("prefs", 0);
widgetView = new RemoteViews(context.getPackageName(), R.layout.widget_main);
thisWidget = new ComponentName(context, HelloWidget.class);
}
@Override
public void run() {
counter++;
String hashrate = settings.getString("hashrate", null);
String rejected = settings.getString("rejected", null);
widgetView.setTextViewText(R.id.widget_textview, counter + "-" + counter2);
appWidgetManager.updateAppWidget(thisWidget, widgetView);
}
}
再次,当父应用程序被杀死时,我的小部件停止更新。有没有办法让我在小部件中使用我的onClick方法启动父应用程序,如果它目前没有运行?
答案 0 :(得分:0)
问题是最终您的AppWidgetProvider
将被系统销毁。它实际上只是一个BroadcastReceiver
,其生命周期仅限于onReceive()的持续时间,根据文档:
BroadcastReceiver对象仅在通话期间有效 to onReceive(Context,Intent)。一旦你的代码从此返回 功能,系统认为要完成的对象不再 活性
让BroadcastReceiver
暂停TimerTask
实施将失败,正如您已经体验过的那样。使用AlarmManager
在下一个预定时间间隔发送PendingIntent
的最佳方法。此PendingIntent
可以启动BroadcastReceiver
或Service
(可能是IntentService
),它将执行更新并安排下一次警报。