gpt4 book ai didi

android - 通知和 AlarmManager - 取消我设置的闹钟

转载 作者:行者123 更新时间:2023-11-29 16:08:05 26 4
gpt4 key购买 nike

我正在制作一个仅用于教育目的的示例应用程序,我每分钟都会在其中创建一个通知。我还有一个取消闹钟的按钮。

我所做的是,当通知到来时,我单击它并单击我设置为运行 unsetAlarm() 的取消设置按钮。但它每时每刻都在困扰着我。

如何停止通知?会不会是 Activity 以某种方式重复了?如果是这种情况,如何确保只有一个 MainActivity 实例?

MainActivity 类

package no.perandersen.notifyontimeexample;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends Activity {

private AlarmManager alarmManager;
private PendingIntent notifyIntent;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
}


public void setAlarm(View v) {
Intent myIntent = new Intent(MainActivity.this,
NotificationService.class);

notifyIntent = PendingIntent.getService(MainActivity.this, 0,
myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE, 1);
Log.v(TAG, "time for alarm trigger:" + calendar.getTime().toString());
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 1 * 60 * 1000, notifyIntent);
}

public void unsetAlarm(View v) {
alarmManager.cancel(notifyIntent);
Log.v(TAG, "cancelling notification");
}

}

NotificationService 类

package no.perandersen.notifyontimeexample;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

public class NotificationService extends Service {

private NotificationManager nm;
private static final String TAG = "NotificationService";
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.v(TAG, "on onCreate");

}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Log.v(TAG, "on onStartCommand");
Intent mainActivityIntent = new Intent(this, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, mainActivityIntent, 0);

Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("bothering you")
.setContentText("Just bothering you from example code")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults|= Notification.DEFAULT_LIGHTS;
nm.notify(0, notification);

return super.onStartCommand(intent, flags, startId);

}

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

}

最佳答案

问题在于,要取消 Alarm,您需要重新创建 PendingIntent,这与您在设置闹钟时创建它的方式完全相同。因此,将您的 unsetAlarm() 更改为

  public void unsetAlarm(View v) {
Intent myIntent = new Intent(MainActivity.this,
NotificationService.class);
notifyIntent = PendingIntent.getService(MainActivity.this, 0,
myIntent, 0); // recreate it here before calling cancel

alarmManager.cancel(notifyIntent);
Log.v(TAG, "cancelling notification");
}

关于android - 通知和 AlarmManager - 取消我设置的闹钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16306600/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com