Android的广播接收器上BOOT_COMPLETED不启动报警 [英] Android BroadcastReceiver on BOOT_COMPLETED does not start alarm

查看:711
本文介绍了Android的广播接收器上BOOT_COMPLETED不启动报警的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个比较简单的解决方案来执行开机一些code。基本上,我想安排/在启动重新安排报警在未来执行某项任务。

I'm trying to use a relatively simple solution to execute some code on boot. Basically I want to schedule/reschedule an Alarm at boot to execute a certain task in the future.

在我的清单我有:

<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<receiver android:name="com.cswt.lcyairport.alarm.AlarmReceiver">
     <intent-filter>
          <action android:name="android.intent.action.BOOT_COMPLETED"/>
     </intent-filter>
</receiver>

和我的code:

@Override
public void onReceive(Context context, Intent intent) {
    this.context = context;

    String action = intent.getAction();
    if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {

        // Setup alarm
        scheduleAlarm();

        //Intent pushIntent = new Intent(context, MainActivity.class); 
        //pushIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //context.startActivity(pushIntent);

    }

}

private void scheduleAlarm() {
    long interval = 10*1000;

    Intent intentAlarm = new Intent(AlarmReceiver.ACTION_GO_TO_GATE);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager)       context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 10000, interval, pendingIntent);
}

如果我取消了code,开始它的伟大工程的活动,我看到BOOT_COMPLETED被我接收器捕获。然而,试图启动报警器不工作(也试图呈现出通知,它不工作)。
我怎样才能解决这个问题?

If I uncomment the code to start the activity it works great, and I see BOOT_COMPLETED is captured by my receiver. However, trying to start the alarm does not work (also tried showing a notification and it doesn't work). How can I solve this?

推荐答案

您必须使用不同的请求code,并将它们保存到某个设备后再次使用reboots.You必须使用相同的请求code,它你可以用来设置你的PendingIntent报警。然后,它会重新启动后的工作。而且你必须确保请求code不被再次使用,因为你说,如果你设定的要求code另一个闹钟设置与请求code 0 wit.h的PendingIntent报警,然后0那么你的previous报警将会被新的取代。所以,你不会得到的第一个报警。

You have to use different request Code and save them somewhere to use again after device reboots.You have to use the same request Code that you have used to set your Alarm with PendingIntent. Then it will work after reboot. And also you have to make sure that request Code isn't used again , because Say you set an Alarm with a request Code 0 wit.h PendingIntent , then if you set another Alarm with request Code 0 then your previous Alarm will be replaced by the newer one. So, you'll not get the first Alarm.

编辑:
设置阿拉姆与下面的代码片段

Set alam with the following snippet

public void SetAlarm(Calendar calendar, int reqCode) { String dateName = idea.getText().toString(); String dateNote = note.getText().toString(); Log.d("SetAlarm Texts", "Date : " + dateName + " Note: " + dateNote);

Intent myIntent = new Intent(mActivity, AlarmReceiver.class);

myIntent.putExtra("title", "Her : " + dateName);

myIntent.putExtra("notes", dateNote);

myIntent.putExtra("code", reqCode);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mActi vity, reqCode, myIntent, 0); AlarmManager alarmManager = (AlarmManager) mActivity

.getSystemService(Context.ALARM_ SERVICE);

alarmManager.set(AlarmManager.RT C, calendar.getTimeInMillis(), pendingIntent); }

这篇关于Android的广播接收器上BOOT_COMPLETED不启动报警的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆