2013-04-30 104 views
1

我正在制作一个示例应用程序,仅用于教育目的,我每分钟创建一个通知。我也有一个按钮来取消闹钟。通知和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; 
    } 

} 
+0

从哪里调用setAlarm和unsetAlarm? – jaibatrik 2013-04-30 18:35:44

+0

activity_main.xml的onClick属性 – Piddien 2013-04-30 18:36:52

回答

4

的问题是,当你设置报警取消您需要重新创建PendingIntentAlarm正是你如何创建它。因此请将您的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"); 
} 
+0

我正在查看android:launchMode =“singleInstance”现在的清单,可能会更好。无论如何,我将在此后检查这个 – Piddien 2013-04-30 18:37:35

+0

,在我的代码中,我取消了使用完全相同的PendingIntent。我将它存储在一个字段中。那应该不是问题吧? – Piddien 2013-04-30 18:57:04

+0

好的。这工作。但为什么我不能将它存储在一个领域并重用呢? – Piddien 2013-04-30 20:02:03