0
我一直在搜索這個自早晨以來,並提到了大多數在stackoverflow上的android鬧鐘問題。Android鬧鐘啓動新的活動並重置鬧鐘
我想設置不同的意圖多個報警。收到警報後,我希望警報取消,並且活動在前面進行,以防其已經運行,或者在死亡時再次啓動,但是這次不應再次設置警報。我不希望其他警報生效。 目前,問題在於點擊通知後會再次啓動活動並重置警報。如果我嘗試使用alarmmanager.cancel取消它,它根本不通知用戶。 這裏是我的代碼,請大家幫忙
我的MainActivity這就是設置多個鬧鐘
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Calendar cal = Calendar.getInstance(); //for using this you need to import java.util.Calendar;
// add minutes to the calendar object
cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
cal.set(Calendar.HOUR_OF_DAY, 22);
cal.set(Calendar.MINUTE, 8);
// cal.add(Calendar.MINUTE, 1);
AlarmManager mgrAlarm = (AlarmManager) this.getSystemService(ALARM_SERVICE);
ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();
for(int i = 0; i < 10; ++i)
{
Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("title", "notification no."+String.valueOf(i));
intent.putExtra("NOTIFICATION_ID", String.valueOf(i));
// Loop counter `i` is used as a `requestCode`
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, i, intent, 0);
// Single alarms in 1, 2, ..., 10 minutes (in `i` minutes)
mgrAlarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + 60000 * i,
pendingIntent);
intentArray.add(pendingIntent);
}
}
我AlarmReceiver類
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager manger = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "Alarm App", System.currentTimeMillis());
Bundle extras=intent.getExtras();
String title=extras.getString("title");
int notif_id=Integer.parseInt(extras.getString("NOTIFICATION_ID"));
//here we get the title and description of our Notification
Class myclass = MainActivity.class;
PendingIntent contentIntent = PendingIntent.getActivity(context, notif_id,
new Intent(context, MainActivity.class), 0);
String note=extras.getString("note");
notification.setLatestEventInfo(context, note, title, contentIntent);
notification.flags = Notification.FLAG_INSISTENT;
notification.defaults |= Notification.DEFAULT_SOUND;
//here we set the default sound for our
//notification
// The PendingIntent to launch our activity if the user selects this notification
manger.notify(notif_id, notification);
}
};
非常感謝...有幫助 – user2184523 2013-04-27 18:22:55