0

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>
Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
rami78
  • 7
  • 5

1 Answers1

2

You shouldn't be using a thread sleep for this. The system will quickly kill off your service (in a relative sense of things, 24 hours is a long time to have the same app in memory). Instead you should be using an AlarmManager and request that your app has an intent fired so that you can do work every day. On boot, register the AlarmManager to do work every few days. Also, whenever you do a bit of work, save how long you've up (perhaps in a SharedPreferences), so you can know to stop requesting updates from the AlarmManager after that much time. This will get you a design where your app can go out of memory and die off -- safely absolving you of any memory killing complaints -- and then do work again when necessary.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
Kristopher Micinski
  • 7,572
  • 3
  • 29
  • 34
  • Right, AlarmManager is a must here, as the system will not keep your app in memory for 24 hours -- it will kill it off. The only way to get a job like this to run in the background will be to use an AlarmManager. – Kristopher Micinski Mar 22 '12 at 18:46
  • @Krtistopher Micinski I read the article. Now i have to create an activity with the alarmmanager in it and a broadcastreceiver and call the activity onreceive of BroadcastReceiver. Am i right ? – rami78 Mar 22 '12 at 18:50
  • you don't call the activity of the BroadcastReceiver. You do the work in the BroadcastReceiver. Activities are for handling UI stuff, not things like processing background work. In this case you are doing additional background work, not actually doing UI stuff. (Hopefully.) If you are instead doing things where you do need to update the UI, then you should instead save the information (after doing background work) to a database and then update the UI thread similarly. – Kristopher Micinski Mar 22 '12 at 18:53
  • aaa .You mean that i will only add broascastreceiver with the alarmmanager in it and my thread(without sleep of-course)? – rami78 Mar 22 '12 at 18:56
  • No, you won't use a thread at all. Using a thread in Android is probably a bad idea. Just do the work in the broadcast receiver when you get the intent which you set up with an AlarmManager. That's the best place to put this kind of thing! (You should probably google more tutorials and find an example which concretizes this.) – Kristopher Micinski Mar 22 '12 at 18:57
  • I have this broadcastreceiver and i tried to add the alarm manager without success. Must i add the alarmmanager to InfoReceiver.java or to myBroadcastreceiver?(Also i remove the thread from inforeceiver – rami78 Mar 22 '12 at 19:35
  • You don't add the alarm manager anywhere, you get it via a system service from your context, and then request that it start your broadcast receiver. You should read some more tutorials on AlarmManageer to ensure you understand it's operation: http://stackoverflow.com/questions/4741757/use-alarmmanager-and-service-to-perform-schedule-notification-only-during-specif – Kristopher Micinski Mar 22 '12 at 20:12
  • I changed the above code. could you check it. I tested it and my code run only 2 times and after that stopped – rami78 Mar 22 '12 at 21:23
  • If you force stop your application the AlarmManager will no longer work. Make sure you are registering your broadcast receiver in your Manifest file. – zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz Mar 22 '12 at 21:31