我正在設置每日報警。它適用於應用程序在鬧鐘時運行,但在應用程序未運行時無法運行。 這是我的聲明接收器的清單:應用未運行時觸發報警
<receiver android:name="com.myAppPackage.alarm.AlarmReceiver"
android:enabled="true"
android:exported="true"/>
工作室警告我:導出接收器不需要任何許可。
真我也沒有添加機器人:許可也不及意向接收器和應用部分沒有任何權限標籤。
而這是廣播接收器:
package com.myAppPackage.alarm;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
public class AlarmReceiver extends BroadcastReceiver {
public AlarmReceiver(){}
@Override
public void onReceive(Context context, Intent intent) {
final Intent syncIntent = new Intent(context, AlarmActivity.class);
syncIntent.addFlags(FLAG_ACTIVITY_NEW_TASK);
context.startActivity(syncIntent);
}
}
該報警在以下方法配置的(在這個例子中配置爲每日不精確抵銷在13點48):
public static void configureDailySync(Context context) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
final int hourOfDay = 13;
final int minuteOfHour = 48;
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minuteOfHour);
alarmManager.setInexactRepeating(
AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY,
alarmPendingIntent);
}
不限幫助非常歡迎!謝謝!!
新聞:越來越近......如果我在MainActivity onCreate()方法中複製警報設置代碼,它就會工作!這是我複製的代碼:
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
final int hourOfDay = 13;
final int minuteOfHour = 48;
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minuteOfHour);
alarmManager.setInexactRepeating(
AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY,
alarmPendingIntent);
當如在OnCreate()在MainActivity的話,根本觸發甚至當應用程序被關閉...... ,但創造超過報警當我打電話呼叫的從MainActivity這樣的方法:
MyAppAccount.configureDailySync(this);
它不工作! MyAppAccount是一個普通的類,沒有擴展任何東西......我試圖讓MyAppAccount擴展AppCompatActivity,以防萬一它重要但沒有任何東西...... 噢,好吧......上面的嘗試在上面執行警報設置MainActivity的onCreate()方法並不總是正常工作......最令人費解的是什麼! :-(
你嘗試在應用程序類中放置configureDailySync()時會發生什麼? –