Android 取消报警管理器
Android cancel alarmmanager
我正在尝试测试 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 的情况下取消所有警报?
这解决了我的问题。
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");
}
}
我正在尝试测试 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 的情况下取消所有警报?
这解决了我的问题。
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");
}
}