0
我在將通知添加到我的應用程序時遇到了一些麻煩。例如,我想每天早上10點通知一次。這裏是我的代碼:Android鬧鈴管理器不會觸發我的活動
MainActivity.java
public class MainActivity extends AppCompatActivity {
private PendingIntent alarmIntent;
private AlarmManager alarmMgr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setAlarm();
}
private void setAlarm() {
alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 00);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 3, alarmIntent);
}
AlarmReceiver.java
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentTitle("My notification")
.setContentText("Hello World!");
Intent resultIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
}
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.turnu_a.todolist">
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".taskView.newTaskActivity" />
<activity android:name=".taskView.showTaskDetailsActivity" />
<activity android:name=".taskView.updateTaskActivity" />
<activity android:name=".settings.settingsActivity"></activity>
<receiver android:name=".Alarm.AlarmReceiver"></receiver>
</application>
事情是,即使我改變觸發器在一兩分鐘內發生的時間(爲了測試我的代碼),絕對沒有任何反應。我試圖在「setAlarm()」方法中創建一個通知,它的工作原理,這意味着問題不是來自創建通知,而是觸發alarmManager。 我會很感激任何幫助。謝謝。
AlarmManager不支持3秒的間隔,就像您當前設置的那樣(1000 * 3)。你是否在終端使用'adb she'll dumpsys alarm'檢查了鬧鐘信息?它應該顯示警報是如何設置的。可能會在第二天進行設置。 –