Reputation: 193
I have a broadcastReceiver that detects when Sim card is changed. I want to send an email each 5 or so minutes with the location of the phone. I have 3 choices in mind :
When Sim is changed : start a Service from broadcastReceiver, which starts a TimerTask. Then the TimerTask starts an AsyncTask which sends the email.
When Sim is changed : same as point 1, but i start an IntentService instead of a Service.
When Sim is changed : start a Service from BroadcastReceiver, which starts an AlarmManager,and then the AsyncTask.
Can you please tell me which one is better and safer? Thank you!
Upvotes: 1
Views: 56
Reputation: 10203
You can combine IntentService
with AlarmManager
. When sim card changed, your broadcastReceiver
should start your intent service, then service send an email and schedule the next time to send. Take a look at this answer and make sure you know when to stop sending email to avoid drain battery.
Your service should look like:
@Override
protected void onHandleIntent(Intent intent)
{
// send email.
// check whether or not sending next time
if(canSendNextTime()){
scheduleNextUpdate();
}
}
Upvotes: 1