EDIT: Anyone who stumbles upon this might want to know, that to fire the alarm after reboot, you need to register it in the Manifest and not runtime.
I have the following class which I have designed using guidelines from Here:
public class MainActivity extends Activity {
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
BroadcastReceiver br;
TextView t;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setup();
t = (TextView)findViewById(R.id.textView1);
ComponentName receiver = new ComponentName(getApplicationContext(), SampleBootReceiver.class);
PackageManager pm = getApplicationContext().getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 22); // Particular minute
calendar.set(Calendar.SECOND, 0);
alarmMgr = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000*60*60*24, alarmIntent);
}
public void setup() {
br = new BroadcastReceiver() {
#Override
public void onReceive(Context c, Intent i) {
Toast.makeText(c, "Rise and Shine!", Toast.LENGTH_LONG).show();
//Invoke the service here Put the wake lock and initiate bind service
t.setText("Hello Alarm set");
}
};
registerReceiver(br, new IntentFilter("com.testrtc") );
alarmIntent = PendingIntent.getBroadcast( this, 0, new Intent("com.testrtc"),
0 );
alarmMgr = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I have a SampleBootReceiver class which is:
public class SampleBootReceiver extends BroadcastReceiver {
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
BroadcastReceiver br;
TextView t;
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Toast.makeText(context, "Hello from Bootloader", 10000).show();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 22); // Particular minute
calendar.set(Calendar.SECOND, 0);
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000*60*60*24, alarmIntent);
}
}
}
Here is my Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testrtc"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<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/AppTheme" >
<activity
android:name="com.testrtc.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".SampleBootReceiver"
android:enabled="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
</intent-filter>
</receiver>
</application>
</manifest>
The alarm is working fine before restart, after reboot I get the toast message from BootReceiver class too. But the Alarm don't reset. Here I want to clarify one point, as the Docs state that the alarm wont be reset unless the app is started at least once by the user: Set the RECEIVE_BOOT_COMPLETED permission in your application's manifest. This allows your app to receive the ACTION_BOOT_COMPLETED that is broadcast after the system finishes booting (this only works if the app has already been launched by the user at least once): What is the context of this statement? If the user has to restart the app, anyway the onCreate will be called and the alarm will be set again. Or does this statement mean that throughout the life-cycle, the app has to run on the phone at least once?
I Think, you Alarm work fine but i do not call setup() method when your SampleBootReceiver start again , after boot complete.
added this line again in SampleBootReceiver to get alarmIntent
registerReceiver(br, new IntentFilter("com.testrtc") );
alarmIntent = PendingIntent.getBroadcast( this, 0, new Intent("com.testrtc"),
0 );
alarmMgr = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));
Thanks
Related
I need your help. I'm stuck with this problem. Alarms work properly. However, when device is rebooted, and the specified time stored in db is in the past (3pm) and current time is 4pm, how can I check to prevent triggering the alarm immediately?
Docs say:
If the stated trigger time is in the past, the alarm will be triggered immediately.
Here's what I've tried so far:
class DeviceBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Check if successful reboot
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
SharedPreferences shared = context.getSharedPreferences(Constants.PREF_NAME, Context.MODE_PRIVATE);
// Check if daily notification is enabled
// If yes, call NotificationPublisher to communicate with NotificationAlarmService
if(shared.getBoolean(Constants.KEY_IS_DAILY_NOTIFIED, false)) {
DBHelper dbHelper = new DBHelper(context);
DBConnector.dbConnect(dbHelper);
int DAILY_NOTIFICATION_ID = Constants.DAILY_VERSE_NOTIFICATION_1_ID;
ArrayList<Notification> notificationList = dbHelper.getNotifications();
Log.e("", "notificationList: " + notificationList.size());
for(Notification obj : notificationList) {
Calendar datetime = Calendar.getInstance();
datetime.set(Calendar.HOUR_OF_DAY, obj.getHourOfDay());
datetime.set(Calendar.MINUTE, obj.getMinute());
datetime.set(Calendar.SECOND, 0);
Calendar now = Calendar.getInstance();
if (now.after(datetime)) {
datetime.add(Calendar.DATE, now.get(Calendar.DATE) + 1);
}
Log.e("BOOT RECEIVER", "" + obj.getHourOfDay() + ":" + obj.getMinute());
Intent myIntent = new Intent(context, NotificationPublisher.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
DAILY_NOTIFICATION_ID++,
myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
datetime.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
pendingIntent);
}
}
}
}
AndroidManifest:
<receiver
android:name=".utils.DeviceBootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
What seems to be wrong with my code? I'd appreciate any help. Thanks!
I set & check alarm time like this and it works for me now:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
Calendar now = Calendar.getInstance();
now.setTimeInMillis(System.currentTimeMillis());
if (calendar.before(now))
calendar.add(Calendar.DAY_OF_MONTH, 1);
A sample code to set repetition of Alarm at interval day.
I have set the Alarm in MainActivity if your want to set Alarm once you can do it by extending Application class and write code to set Alarm inside onCreate.
public class MainActivity extends AppCompatActivity {
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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, 15);
calendar.set(Calendar.MINUTE, 30);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
}
}
SampleBootReciver - set Alarm after the device is rebooted
public class SampleBootReceiver extends BroadcastReceiver {
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent1 = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent1, 0);
Calendar now = Calendar.getInstance();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 15);
calendar.set(Calendar.MINUTE, 30);
if (calendar.before(now))
calendar.add(Calendar.DAY_OF_MONTH, 1);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
}
}
}
AlarmReceiver - perform action after Alarm is fired.
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
}
}
I'm just displaying a toast.
Lastly the manifest file -
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bits.kevz.samplealarm">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<receiver android:name=".SampleBootReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
I want you to run above code and check onBootCompleted output.
I want to create a repeating alarm from AlarmManager which is triggered at 21:00 every day to show a notification. So i create a service and declare that in manifest, in the service class i wrote a method for schedule repeating alarms.
public static void setRecurringAlarm(Context context) {
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, NotificationService.class);
PendingIntent pi = PendingIntent.getService(context, 0, i, 0);
am.cancel(pi);
Calendar updateTime = Calendar.getInstance();
updateTime.setTimeZone(TimeZone.getDefault());
updateTime.set(Calendar.HOUR_OF_DAY, 21);
updateTime.set(Calendar.MINUTE, 00);
updateTime.set(Calendar.SECOND, 00);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP,
updateTime.getTimeInMillis(),
AlarmManager.INTERVAL_FIFTEEN_MINUTES, pi);
}
(For test i set the delay INTERVAL_FIFTEEN_MINUTES).
I call this method at onResume() of my splash screen activity and in a BroadcastReceiver that is set up to receive BOOT_COMPLETED.
#Override
protected void onResume() {
super.onResume();
NotificationService.setRecurringAlarm(this);
}
this is my BroadcastReceicer :
public class NotificationBootReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
NotificationService.setRecurringAlarm(context);
}
}
and this is my manifest :
<receiver android:name=".notification.NotificationBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".notification.NotificationService" />
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
the problem is every time i open the app and splashscreen running the alarm triggered and notification will be shown.
I stucked on the following:
I created simple helper which should to set alarm in the predefined time:
In activity:
private void setAlarm() {
// TODO: Should be moved in the short time into BaseActivity Class ()
// Add notification
try {
AlarmHelper ah = new AlarmHelper(this);
ah.setAlarm();
Logger.i("Alarm successfully Set");
} catch(Exception e) {
Logger.e(e.getMessage());
}
}
Helper:
public class AlarmHelper {
private Context mCtx;
public AlarmHelper(Context ctx) {
mCtx = ctx;
}
public void setAlarm() {
try {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, 8);
calendar.set(Calendar.YEAR, 2015);
calendar.set(Calendar.DAY_OF_MONTH, 27);
calendar.set(Calendar.HOUR_OF_DAY, 14);
calendar.set(Calendar.MINUTE, 52);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM,Calendar.PM);
Intent myIntent = new Intent(mCtx, AlarmBroadcastReceiver.class);
AlarmManager alarmManager = (AlarmManager) mCtx.getSystemService(mCtx.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), PendingIntent.getBroadcast(mCtx,1, myIntent, PendingIntent.FLAG_UPDATE_CURRENT));
} catch(Exception e) {
Logger.e(e.getMessage());
}
}
Permissions:
<!-- APP PERMISSIONS -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
Receiver
<!-- BROADCAST RECEIVERS -->
<receiver android:name=".receiver.AlarmBroadcastReceiver"
android:enabled="true"
android:exported="true">
</receiver>
I tried also this:
<!-- BROADCAST RECEIVERS -->
<receiver android:name="com.xxx.xxx.receiver.AlarmBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="YOUR_NAME" />
</intent-filter>
</receiver>
public class AlarmBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
// Device battery life will be significantly affected by the use of this API.
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "TAG");
// Acquire the lock
wl.acquire();
Logger.d("ALARM RECEIVED!!!");
//Release the lock
wl.release();
} catch (Exception e) {
Logger.e("onReceive method cannot be processed");
e.printStackTrace();
}
}
}
No exception during the setAlarm method execution. But onReceive method is never executed.
Where can be problem please?
Many thanks for any advice.
You have to set an intent-filter in your receiver :
<receiver android:name=".receiver.AlarmBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="YOUR_NAME" />
</intent-filter>
</receiver>
Then change the line
Intent myIntent = new Intent(mCtx, AlarmBroadcastReceiver.class);
to
Intent myIntent = new Intent("YOUR_NAME");
For me, it's working withouth intent filter, try to set the time like this:
manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
pendingAlarm1Intent = PendingIntent.getBroadcast(this, 1, alarmIntent, 0);
....
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
// if the time is before now then add one day to it
if(calendar.getTimeInMillis()<System.currentTimeMillis())
calendar.setTimeInMillis(calendar.getTimeInMillis()+86400000);
manager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingAlarm1Intent);
EDIT:
and write the whole package hierarchy in the manifest file for the BroadCastReceiver, like this:
<application
....>
<receiver android:name="hu.bendaf.AlarmReceiver"/>
....
</application>
EDIT: update codes. I am starting the alarm in one of my activity.
The problem was in the:
calendar.set(Calendar.MONTH, 7);
Java Calendar is counting from month 0, so current month should not be 8 but 7. Sorry for my mistake. Code in the answer is working correctly.
I try to use alarm manager to send an intent at 8:30 but the alarm manager never send an intent to the receiver around 8:30.
I there is something messed up in the setAlarm() method but I could not figure it out.
Also many posts here also mentioned the service/receiver might be declared wrong in the manifest file, but I am not sure how to properly include receiver and service in the manifest. I have tried to incorporate the package name but it still does not work.
public class EventAlarmReceiver extends WakefulBroadcastReceiver {
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
private PendingIntent alarmIntent1;
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, EventSchedulingService.class);
startWakefulService(context, service);
}
public void setAlarm(Context context) {
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, EventSchedulingService.class);
intent.putExtra("silent", true);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
// Set the alarm's trigger time to 8:30 a.m.
calendar.set(Calendar.HOUR_OF_DAY,8);
calendar.set(Calendar.MINUTE, 30);
System.out.println(calendar.getTimeInMillis());
alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
}
}
Here is my service file
package edu.ssui.smartsilent;
public class EventSchedulingService extends IntentService {
public EventSchedulingService() {
super("EventSchedulingService");
}
#Override
protected void onHandleIntent(Intent intent) {
Log.d("SService", "m1");
AudioManager mgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if(intent.getExtras().getBoolean("silent")==true){
mgr.setStreamVolume(AudioManager.STREAM_RING, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
mgr.setStreamVolume(AudioManager.STREAM_ALARM, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
mgr.setStreamVolume(AudioManager.STREAM_SYSTEM, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
mgr.setStreamVolume(AudioManager.STREAM_NOTIFICATION, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
}
else
{
mgr.setStreamVolume(AudioManager.STREAM_RING, (int) (mgr.getStreamMaxVolume(AudioManager.STREAM_RING)*0.8), AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
mgr.setStreamVolume(AudioManager.STREAM_ALARM, (int) (mgr.getStreamMaxVolume(AudioManager.STREAM_ALARM)*0.8), AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
mgr.setStreamVolume(AudioManager.STREAM_SYSTEM, (int) (mgr.getStreamMaxVolume(AudioManager.STREAM_SYSTEM)*0.8), AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
mgr.setStreamVolume(AudioManager.STREAM_NOTIFICATION, (int) (mgr.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION)*0.8), AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
}
Log.d("SService", "m1");
EventAlarmReceiver.completeWakefulIntent(intent);
}
}
here is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.ssui.smartsilent"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".EventAlarmReceiver"></receiver>
<service android:name=".EventSchedulingService" />
</application>
</manifest>
You are calling:
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
PendingIntent.getBroadcast(context, intent) will retrieve a pendingintent that will perform a broadcast with the context and intent that you passed. So for example it will call Context.sendBroadcast(intent), which will send the intent to all interested BroadcastReceivers.
Instead of doing:
Intent intent = new Intent(context, EventSchedulingService.class);
Try passing a BroadcastReceiver to the intent constructor.
Add this Permission to Your menifest :
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
try this:
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, EventAlarmReceiver.class);
intent.putExtra("silent", true);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
// calendar.setTimeInMillis(System.currentTimeMillis());
// Set the alarm's trigger time to 8:30 a.m.
calendar.set(Calendar.HOUR_OF_DAY,8);
calendar.set(Calendar.MINUTE, 30);
System.out.println(calendar.getTimeInMillis());
alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
Also put logs for debugging and make sure you are calling setAlarm().
Hi I am creating an application which will fire the alarm in a particular interval regularly. It works well. But when i switched off my mobile and switched on again alarm not working. Please help to solve the issue.
My Alarm code is:
AlarmManager alarmMgr = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, ReminderReceiver.class);
intent.putExtra(Config.RECEIVE_ALARM_LIST, dataList);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context,
alarmId, intent, 0);
Calendar calendar = Calendar.getInstance();
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 1000 * 60 * mins, alarmIntent);
Add this in your AndroidManifest.xml:
<receiver android:name=".BootCompletedIntentReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
and
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Add this class:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootCompletedIntentReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent pushIntent = new Intent(context, MyService.class);
context.startService(pushIntent);
}
}
}
Create a BroadCastReceiver and call this put this alarm code in this receiver and add Boot Complete permission to that receiver so when your phone is switched on then that receiver will automatically called. Refer this :
public class Autostart extends BroadcastReceiver
{
public void onReceive(Context arg0, Intent arg1)
{
Log.i("Autostart", "**********started************");
AlarmManager alarmMgr = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, ReminderReceiver.class);
intent.putExtra(Config.RECEIVE_ALARM_LIST, dataList);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context,
alarmId, intent, 0);
Calendar calendar = Calendar.getInstance();
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 1000 * 60 * mins, alarmIntent);
}
}
AndroidManifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="package_name" android:versionCode="1" android:versionName="1.0"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".Autostart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
</manifest>