1

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

我所做的是,当通知到来时,我单击它并单击我设置为运行 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");
    }

}

通知服务类

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;
    }

}
4

1 回答 1

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");
} 
于 2013-04-30T18:36:45.307 回答