문제

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!

도움이 되었습니까?

해결책

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();
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top