我正在嘗試使用報警管理器進行報警。該應用運行時沒有任何錯誤消息,但沒有任何反應。AlarmManager不會執行任何操作
我嘗試了developer.android.com的解決方案和來自stackoverflow的建議。 我也嘗試複製我發現的完整教程,但沒有任何工作。
該應用在API22上運行,並在仿真器(API23)和真實設備(API22)上進行了測試。
這是我的代碼:
MainActivity.java startAlarm():
AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent myIntent = new Intent(MainActivity.this,AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0,myIntent,0);
manager.set(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime()+3000,pendingIntent);
AlarmReceiver.java:
public class AlarmReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"THIS IS MY ALARM",Toast.LENGTH_LONG).show();
}
}
AndroidManifest.xml中,<manifest...>
和</manifest>
之間:
<receiver android:process=":remote" android:name=".AlarmReceiver"></receiver>
你的時基是錯誤的隨意閱讀文件! https:// developer.android.com/reference/android/app/AlarmManager.html'RTC_WAKEUP'使用'System.currentTimeMillis()'作爲它的時間基準。 –
@ escape-llc確實如此,但有了這種組合,給定的時間將會在過去,所以警報會立即啓動,而不是無所事事。 –
清單中的元素在哪裏?它需要位於''標籤之間,但不包含在其他任何內容中,例如''或''標籤。 –