首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android告警启动新活动并重置告警

Android告警启动新活动并重置告警
EN

Stack Overflow用户
提问于 2013-04-27 06:12:59
回答 1查看 832关注 0票数 0

我从早上开始就一直在搜索这个问题,并提到了stackoverflow上的大多数android警报问题。

我试着用不同的意图设置多个闹钟。在收到警报时,我希望取消警报,并将活动放在前面,以防它已经运行,或者如果它被杀死,则重新启动,但这次不应再次设置警报。我不希望其他的警报受到影响。当前的问题是,单击通知会再次启动活动并重置警报。如果我尝试使用alarmmanager.cancel取消它,它根本不会通知用户。这是我的代码,请帮助

我的MainActivity设置了多个警报

代码语言:javascript
复制
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Calendar cal = Calendar.getInstance();       //for using this you need to import java.util.Calendar;

    // add minutes to the calendar object
    cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
    cal.set(Calendar.HOUR_OF_DAY, 22);
    cal.set(Calendar.MINUTE, 8);
//  cal.add(Calendar.MINUTE, 1);
    AlarmManager mgrAlarm = (AlarmManager) this.getSystemService(ALARM_SERVICE);
    ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();

    for(int i = 0; i < 10; ++i)
    {
       Intent intent = new Intent(this, AlarmReceiver.class);
       intent.putExtra("title", "notification no."+String.valueOf(i));
       intent.putExtra("NOTIFICATION_ID", String.valueOf(i));
       // Loop counter `i` is used as a `requestCode`
       PendingIntent pendingIntent = PendingIntent.getBroadcast(this, i, intent, 0);
       // Single alarms in 1, 2, ..., 10 minutes (in `i` minutes)
       mgrAlarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
                    SystemClock.elapsedRealtime() + 60000 * i, 
                    pendingIntent); 

       intentArray.add(pendingIntent);
    }
}

我的AlarmReceiver类

代码语言:javascript
复制
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    NotificationManager manger = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.ic_launcher, "Alarm App", System.currentTimeMillis());

            Bundle extras=intent.getExtras();
            String title=extras.getString("title");
            int notif_id=Integer.parseInt(extras.getString("NOTIFICATION_ID"));

    //here we get the title and description of our Notification
                Class myclass = MainActivity.class;
            PendingIntent contentIntent = PendingIntent.getActivity(context, notif_id, 
                    new Intent(context, MainActivity.class), 0);
            String note=extras.getString("note");
            notification.setLatestEventInfo(context, note, title, contentIntent);
            notification.flags = Notification.FLAG_INSISTENT;
            notification.defaults |= Notification.DEFAULT_SOUND;
    //here we set the default sound for our 
    //notification   
            // The PendingIntent to launch our activity if the user selects this notification
            manger.notify(notif_id, notification);
}

};
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-27 14:22:30

在您的MainActivity中,您可以通过在intent中添加一个参数来区分启动和通知。您无论如何都需要通知id来取消特定的通知。因此,您可以在MainActivity中尝试执行以下操作

代码语言:javascript
复制
  @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent appIntent = this.getIntent();
        int notif_id = appIntent.getIntExtra( "my_notification_id", -1 ) ;
        if( notif_id != -1 )
        {
            Log.d ( "LOG_TAG",  "Launched from Notification ");
            NotificationManager nm = (NotificationManager) getSystemService(     NOTIFICATION_SERVICE );
            nm.cancel( notif_id );

           /* Do the separate processing here */
           .....
        }
        else
        {
            /* Your previous onCreate code goes here */

在文件AlarmReceiver.java中,您需要进行以下更改

代码语言:javascript
复制
//PendingIntent contentIntent = PendingIntent.getActivity(context, notif_id, new           Intent(context, MainActivity.class), 0);

Intent appIntent = new Intent(  context, MainActivity.class );
appIntent.putExtra( "my_notification_id", notif_id );
PendingIntent contentIntent = PendingIntent.getActivity(context, notif_id, appIntent, 0);

希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16245997

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档