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

时间:2013-04-30 18:27:08

标签: android android-activity notifications alarmmanager

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

我所做的是,当收到通知时,我单击它并单击我设置为运行unsetAlarm()的未设置按钮。但它每分钟都在困扰我。

如何停止通知?可能是活动在某种程度上重复了吗?如果是这种情况,我如何确保只有一个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;
    }

}

1 个答案:

答案 0 :(得分:4)

问题是要取消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");
}