I want to make my alarmmanager to rings although after my application closed.
But now my code don`t call broadcastreceiver after my application closed.
I reg my broadcastreceiver in my manifest.
This is code that setting alarmmanager
final AlarmManager alarmManager=(AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent=new Intent(setTimeActivity.this,autoCheckReceiver.class);
intent.setAction("com.dayo.selfcheck.autoCheckReceiver");
final PendingIntent pendingIntent=PendingIntent.getBroadcast(setTimeActivity.this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(pendingIntent);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, timePicker.getHour());
calendar.set(Calendar.MINUTE, timePicker.getMinute());
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
And this code is my broadcastreceiver.
public class autoCheckReceiver extends BroadcastReceiver {
private String TAG="autoCR";
@Override
public void onReceive(Context context, Intent it) {
Log.d(TAG,"asdf");
Intent i = new Intent();
i.setClassName("com.dayo.selfcheck", "com.dayo.selfcheck.MainActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
this is my manifest file.
<receiver
android:name=".autoCheckReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.dayo.selfcheck.autoCheckReceiver"></action>
</intent-filter>
</receiver>
thanks!
You MUST specify android:exported="true"
on your <receiver>
declaration in order for AlarmManager
to launch your BroadcastReceiver
.
You don't need to call cancel()
on the AlarmManager
to cancel the Intent
before you schedule it, as AlarmManager
automatically does this.
You do not need to set the ACTION
in the Intent
for the broadcast, since you are using an explicit Intent
(you have specified the exact component to be used).
You also do not need to provide the <intent-filter>
in your <receiver>
declaration because you are using an explicit broadcast.