I have the service InfoReceiver
. This service starts when the application starts and still be running when the application is closed.Also i have added the application to start on boot completed.The service will call the code i want every 82 milliseconds(24 hours). I don't know how to make thread sleep while the phone is shutdown. Also i want to destroy completed the service after 120 hours.
here is my code
Thank you in advance!
InfoReceiver.java
public class InfoReceiver extends Service {
@Override
public void onCreate()
{
super.onCreate();
//do something code
}
}
MyBroadcastreceiver.java
public class MyBroadcastReceiver extends BroadcastReceiver {
private Context mContext;
@Override
public void onReceive(Context context, Intent intent) {
mContext = context;
Calendar cal = Calendar.getInstance();
// add 5 minutes to the calendar object
cal.add(Calendar.SECOND, 84000);
Intent startServiceIntent = new Intent(context, InfoReceiver.class);
PendingIntent startPIntent = PendingIntent.getBroadcast(context, 0, startServiceIntent, 0);
AlarmManager am = (AlarmManager) mContext.getSystemService(mContext.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), startPIntent);
}
}
Manifest file
<service android:name=".InfoReceiver"
android:enabled="true">
<intent-filter>
<action android:name=".InfoReceiver"/>
</intent-filter>
</service>
<receiver android:process=":remote" android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>