I want to get a notification every day (at 13:00) with alarmManager
, but when the device is restarted, the alarm is active for one day only and does not work for 2nd or 3rd day unless I run my application.
Why does it run only once?
MainActivity.class:
public class MainActivity extends ActionBarActivity implements OnClickListener
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alarmKurulum();
}
public void alarmKurulum() {
Intent alarmIntent = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar alarmStartTime = Calendar.getInstance();
alarmStartTime.set(Calendar.HOUR_OF_DAY, 18);
alarmStartTime.set(Calendar.MINUTE, 05);
alarmStartTime.set(Calendar.SECOND, 00);
alarmStartTime.set(Calendar.MILLISECOND, 0);
Calendar now = Calendar.getInstance();
if(alarmStartTime.getTimeInMillis() < now.getTimeInMillis()) {
alarmStartTime.setTimeInMillis(alarmStartTime.getTimeInMillis() + 86400000);
}
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmStartTime.getTimeInMillis(), pendingIntent);
}
AlarmReceiver.class:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service1 = new Intent(context, AlarmService.class);
context.startService(service1);
}
}
TurnonAlarm.class (for reboot device)
public class TurnOnAlarmReceive extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar alarmStartTime = Calendar.getInstance();
alarmStartTime.set(Calendar.HOUR_OF_DAY, 18);
alarmStartTime.set(Calendar.MINUTE, 05);
alarmStartTime.set(Calendar.SECOND, 00);
alarmStartTime.set(Calendar.MILLISECOND, 0);
Calendar now = Calendar.getInstance();
if(alarmStartTime.getTimeInMillis() < now.getTimeInMillis()) {
alarmStartTime.setTimeInMillis(alarmStartTime.getTimeInMillis() + 86400000);
}
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmStartTime.getTimeInMillis(), pendingIntent);
}
}
}
AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ferhengakurdi9865"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="23" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light" >
<service android:name=".AlarmService" />
<receiver android:name=".AlarmReceiver" />
<receiver android:name=".TurnOnAlarmReceive" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>