我想在我的Android應用中實現「每日提醒」功能,應該在設定的時間每天觸發一次。我嘗試的第一個實現是爲大多數人工作的,但是一些用戶子集(包括至少一個在三星的Android 4.3上運行的人)報告說,警報以比它應該更頻繁的方式觸發,例如,每10分鐘一次,每次他們打開應用程序,一般都很煩人。Android的每日鬧鈴發射太頻繁或只有一次
這裏的報警的啓用:
Intent myIntent = new Intent(ctx, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)ctx.getSystemService(Service.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, sched,
AlarmManager.INTERVAL_DAY,
pendingIntent);
再有就是這個AlarmReceiver類:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent service1 = new Intent(context, AlarmService.class);
context.startService(service1);
}
}
這是登記在AndroidManifest接收器:<receiver android:name=".AlarmReceiver"/>
最後還有的AlarmService ,它看起來像這樣:
public class AlarmService extends Service {
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate()
{
// TODO Auto-generated method stub
super.onCreate();
}
@SuppressWarnings("static-access")
@Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
Log.v("pm", "about to notify");
Intent intent1 = new Intent(this.getApplicationContext(), MainActivity.class);
intent1.setAction(Intent.ACTION_MAIN);
intent1.addCategory(Intent.CATEGORY_LAUNCHER);
//intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(this.getApplicationContext())
.setContentTitle("My App")
.setContentText("Don't forget that thing!")
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingNotificationIntent)
.getNotification();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
NotificationManager nManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nManager.notify(0, notification);
}
@Override
public void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
}
}
但是,正如我所說,人們報告說這每十分鐘就會發射一次!所以我嘗試將AlarmService更改爲一個較不被棄用的實現,但在這個過程中,現在人們說它只會觸發一次,然後再也不會!
我換成onStart
這個:
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.v("pm", "about to notify");
if (intent != null) {
Intent intent1 = new Intent(this.getApplicationContext(), MainActivity.class);
intent1.setAction(Intent.ACTION_MAIN);
intent1.addCategory(Intent.CATEGORY_LAUNCHER);
//intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(this.getApplicationContext())
.setContentTitle("My App")
.setContentText("Don't forget that thing!")
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingNotificationIntent)
.getNotification();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
NotificationManager nManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nManager.notify(0, notification);
} else {
Log.v("pm", "Null Intent");
}
return START_STICKY;
}
既然不能複製我的設備原來的問題,這是一個有點難以測試!我的兩個理論是:
- 的問題在於AlarmReceiver,喜歡它應該不會是啓動一個全新的服務,但做一些與現有的服務
- 我不應該去在排除空
intent
值我onStartCommand
功能
我只是有點緊張,嘗試2號以防萬一它導致人們的設備再次惹惱他們!
稱爲如果你能等待,這幾天我米工作在一個雙定時器(1個鬧鐘是每月和1個鏡頭,另一個是每天在固定的時間和重複)。問題是,我目前正在關閉工作,並在幾個小時之前不會回家......但我有一個接近工作的解決方案(只是它不能在重新啓動後繼續工作 - 仍在工作)。 –
我越來越想知道爲什麼我的AlarmReceiver創建服務,而不是直接顯示通知... – andygeers