Android:无法在BroadcastReceiver内接收警报

时间:2015-08-27 13:03:09

标签: android broadcastreceiver alarmmanager

我坚持以下内容:

我创建了一个简单的帮助器,它应该在预定义的时间内设置警报:

活动:

  private void setAlarm() {
        // TODO: Should be moved in the short time into BaseActivity Class ()
        // Add notification
        try {
            AlarmHelper ah = new AlarmHelper(this);
            ah.setAlarm();
            Logger.i("Alarm successfully Set");
        } catch(Exception e) {
            Logger.e(e.getMessage());

        }
    }

助手:

public class AlarmHelper {
    private Context mCtx;

    public AlarmHelper(Context ctx) {
        mCtx = ctx;
    }

    public void setAlarm() {
        try {
            Calendar calendar = Calendar.getInstance();

            calendar.set(Calendar.MONTH, 8);
            calendar.set(Calendar.YEAR, 2015);
            calendar.set(Calendar.DAY_OF_MONTH, 27);

            calendar.set(Calendar.HOUR_OF_DAY, 14);
            calendar.set(Calendar.MINUTE, 52);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.AM_PM,Calendar.PM);

            Intent myIntent = new Intent(mCtx, AlarmBroadcastReceiver.class);
            AlarmManager alarmManager = (AlarmManager) mCtx.getSystemService(mCtx.ALARM_SERVICE);
            alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), PendingIntent.getBroadcast(mCtx,1,  myIntent, PendingIntent.FLAG_UPDATE_CURRENT));
        } catch(Exception e) {
            Logger.e(e.getMessage());
        }
    }

权限:

  <!-- APP PERMISSIONS -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.alarm.permission.SET_ALARM"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>

接收机

   <!-- BROADCAST RECEIVERS -->
    <receiver android:name=".receiver.AlarmBroadcastReceiver"
        android:enabled="true"
        android:exported="true">
    </receiver>

我也试过了这个:

 <!-- BROADCAST RECEIVERS -->
        <receiver android:name="com.xxx.xxx.receiver.AlarmBroadcastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="YOUR_NAME" />
            </intent-filter>
        </receiver>





public class AlarmBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            // Device battery life will be significantly affected by the use of this API.
            PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "TAG");
            // Acquire the lock
            wl.acquire();
            Logger.d("ALARM RECEIVED!!!");
            //Release the lock
            wl.release();
        } catch (Exception e) {
            Logger.e("onReceive method cannot be processed");
            e.printStackTrace();
        }
    }
}

setAlarm 方法执行期间没有异常。但是 onReceive 方法永远不会被执行。

请问哪里有问题?

非常感谢您的任何建议。

3 个答案:

答案 0 :(得分:0)

您必须在intent-filter

中设置receiver
<receiver android:name=".receiver.AlarmBroadcastReceiver"
        android:enabled="true"
        android:exported="true">
    <intent-filter> 
        <action android:name="YOUR_NAME" />  
    </intent-filter>
    </receiver>

然后更改行

Intent myIntent = new Intent(mCtx, AlarmBroadcastReceiver.class);

Intent myIntent = new Intent("YOUR_NAME");

答案 1 :(得分:0)

对我来说,它没有意图过滤器,尝试设置这样的时间:

Enter

编辑: 并将整个包层次结构写入BroadCastReceiver的清单文件中,如下所示:

manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
pendingAlarm1Intent = PendingIntent.getBroadcast(this, 1, alarmIntent, 0);

....

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
// if the time is before now then add one day to it
if(calendar.getTimeInMillis()<System.currentTimeMillis())
    calendar.setTimeInMillis(calendar.getTimeInMillis()+86400000);
manager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingAlarm1Intent);

编辑:更新代码。我在我的一项活动中开始报警。

答案 2 :(得分:0)

问题在于:

calendar.set(Calendar.MONTH, 7);

Java Calendar从0开始计算,因此当前月份不应该是8但是7。抱歉我的错误。答案中的代码工作正常。