我正在制作一个我想每秒更新一次的小部件(它就像一个倒计时/计时器小部件),所以我使用 AlarmManager 和广播接收器来实现这一点,而不会唤醒手机并耗尽所有电池,但我运行小部件时,LogCat 中不断出现错误:
08-18 18:40:43.368 390-1988/system_process I/ActivityManager: Process com.dysign.livetubecountdown (pid 9784) has died.
08-18 18:40:44.282 390-414/system_process I/ActivityManager: Start proc com.dysign.livetubecountdown for broadcast com.dysign.livetubecountdown/.WidgetAlarmManager: pid=9809 uid=10144 gids={50144, 3003, 1028}
08-18 18:40:44.306 9809-9809/com.dysign.livetubecountdown E/Trace: error opening trace file: No such file or directory (2)
如您所见,该过程不断死亡,然后重新启动,这发生在手机既醒又睡时。
这是我启动 AlarmManager 的方式(这是在小部件提供程序类中):
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, WidgetAlarmManager.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
am.setRepeating(AlarmManager.RTC, System.currentTimeMillis() + 1000, 1000 , pi);
}
这是广播接收器代码:
@Override
public void onReceive(Context context, Intent intent) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_main);
DateFormat format = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM, Locale.getDefault());
remoteViews.setTextViewText(R.id.widgetTextView, "TIME = " + format.format(new Date()));
ComponentName thisWidget = new ComponentName(context, Widget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(thisWidget, remoteViews);
}
任何帮助将不胜感激,谢谢!