AlarmManeger setExact无法正常工作

时间:2016-11-09 18:03:29

标签: java android alarmmanager android-alarms

setExact方法无法处理我的项目

Log.d("Alarm","Fire");
AlarmManagerBroadcastReceiver中的

在1分钟后没有出现!

    public class MainActivity extends AppCompatActivity {
    .
    .
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            .
            .

            Calendar alarm = Calendar.getInstance();
            alarm.setTimeInMillis(System.currentTimeMillis());
            alarm.add(Calendar.MINUTE,1);

            Intent myIntent = new Intent(MainActivity.this, AlarmManagerBroadcastReceiver.class);
            PendingIntent  pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent, 0);

            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarm.getTimeInMillis(), pendingIntent);
            .
            .
}
}

AlarmManagerBroadcastReceiver:

public class AlarmManagerBroadcastReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.d("Alarm","Fire");

       }
}

我试图扩展BroadcastReceiver而不是WakefulBroadcastReceiver。我还尝试设置diffrenet硬编码时间。然而,没有任何作用。

请帮忙

1 个答案:

答案 0 :(得分:0)

link(调度重复警报)可以帮助您。你必须添加

<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission> <receiver android:process=":remote" android:name=".AlarmManagerBroadcastReceiver"></receiver>