Question

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 :

  1. When Sim is changed : start a Service from broadcastReceiver, which starts a TimerTask. Then the TimerTask starts an AsyncTask which sends the email.

  2. When Sim is changed : same as point 1, but i start an IntentService instead of a Service.

  3. 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!

Was it helpful?

Solution

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();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top