0
我創建了一個Widget來顯示每5-10秒刷新一次的當前日期(稍後將增加持續時間)。 在onUpdate方法中創建警報以創建該服務的意圖。 創建了一個onReceive方法來擴展廣播接收器)來啓動服務,然後該服務隨着日期更新小部件。 當我調用onReceive小部件最初觸發並顯示日期。之後,警報不會觸發。Android:報警不能更新服務中的Widget
1)我的權利包括onReceive方法和調用startservice? 2)我如何知道創建警報處於活動狀態並且未觸發? 3)以下代碼有什麼問題?
請幫忙。
謝謝,山姆
public class WorldCupScores extends AppWidgetProvider {
public static String TAG = "myActivity";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "receiveeeeeeeeeeee");
Toast.makeText(context, "Receiver", Toast.LENGTH_LONG).show();
context.startService(new Intent(context, UpdateService.class));
super.onReceive(context, intent);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// To prevent any ANR timeouts, we perform the update in a service
//context.startService(new Intent(context, UpdateService.class));
AlarmManager alarmManager;
Log.d(TAG, "before intenttttt");
Intent intent = new Intent(context, UpdateService.class);
Log.d(TAG, "afterrrr intenttttt");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
intent, 0);
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.add(Calendar.SECOND, 10);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 5*1000, pendingIntent);
Toast.makeText(context, "My alarm started", Toast.LENGTH_LONG).show();
Log.d(TAG, "alarmmmmmmmm");
}
public static class UpdateService extends Service {
@Override
public void onStart(Intent intent, int startId) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "Inside Serviceeeeeeeeeeeeeeeeeeeee");
// Build the widget update for today
//RemoteViews updateViews = buildUpdate(this);
RemoteViews updateViews = new RemoteViews(this.getPackageName(),
R.layout.main);
Date date = new Date();
updateViews.setTextViewText(R.id.scores, "Current Time "
+ date);
// Push update for this widget to the home screen
ComponentName thisWidget = new ComponentName(this, WorldCupScores.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, updateViews);
super.onStart(intent, startId);
}
@Override
public IBinder onBind(Intent intent) {
// We don't need to bind to this service
return null;
}
}
}