我正在尝试测试alarmmanager,我可以让它工作。通过BroadcastReceiver,我启动一个服务来创建一个通知。我设法在我在这里找到的材料的帮助下做到了这一点。我现在面临的问题让我停留了一天,取消了我创造的重复警报。它只是说它必须匹配用于创建它的pendingintent,我还硬编码id以确保它们匹配。对于像我这样未经训练的眼睛,我不会看到我在这里阅读的例子有所不同,但我希望你能帮助我找到我需要改变的东西。
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import java.util.Calendar;
public class CreateNotificationActivity extends Activity {
private PendingIntent pendingIntent;
private static AlarmManager alarmManager;
private Intent myIntent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(alarmManager == null){
alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
}
myIntent = new Intent(this , Receiver.class);
}
public void createNotification(View view) {
pendingIntent = PendingIntent.getBroadcast(this, 1, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000, pendingIntent);
}
public void cancelNotification(View view) {
pendingIntent = PendingIntent.getBroadcast(this, 1, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(pendingIntent);
pendingIntent.cancel();
}
}
如何取消我创建的闹钟?有没有办法取消没有id的所有警报?
答案 0 :(得分:1)
这解决了我的问题。
public class Alarm extends BroadcastReceiver
{
Calendar calendar;
String title;
String message;
@Override
public void onReceive(Context context, Intent intent)
{
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
// Put here YOUR code.
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager mNM = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent1 = new Intent(context.getApplicationContext(), TodayScreenActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent1, 0);
String t = intent.getStringExtra("title");
String m = intent.getStringExtra("message");
Notification mNotify = new Notification.Builder(context)
.setContentTitle(t)
.setContentText(m)
.setSmallIcon(android.R.drawable.ic_dialog_alert)
.setContentIntent(pIntent)
.setSound(sound)
.addAction(0, "Load Workbook", pIntent)
.build();
mNM.notify(intent.getIntExtra("id", 1), mNotify);
wl.release();
}
public void SetRepeatAlarm(Context context, Calendar c, int interval, int id)
{
AlarmManager am=(AlarmManager)context.getSystemService( Context.ALARM_SERVICE);
Intent i = new Intent(context, Alarm.class);
i.putExtra("title", title);
i.putExtra("message", message);
i.putExtra("id", id);
PendingIntent pi = PendingIntent.getBroadcast(context, id, i, PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), interval, pi); //1000 * 60 * 60 * 24 Millisec * Second * Minute * hours
Log.d("Dell", "Alarm - SetRepeatAlarm");
}
public void CancelAlarm(Context context, Task task)
{
Intent intent = new Intent(context, Alarm.class);
PendingIntent sender = PendingIntent.getBroadcast(context, (int)task.getTASK_COL_ID(), intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
Log.d("Dell", "Alarm - CancelAlarm");
}
}