-1

I want to check some web API and do something per x minutes. I think I should write a service on Android (is there any other solution?).

But how can do that?

I am thinking about writing a service class and in the manifest file I should add this line:

<service
    android:name="com.xx.yy.noti_check"
    android:enabled="true"
    >
</service>

And in my noti_check class I check my web API like this on onStartCommand:

public class noti_check  extends Service  {
    Context mcont;
    private Handler myhandler ;
    private long RETRY_TIME = 15000;
    private long START_TIME = 2000;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onStart(Intent intent, int startId) {

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
       mcont=this;
       myhandler= new Handler();
       myhandler.postDelayed(myRunnable, START_TIME);
       return Service.START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        try {
            myhandler.removeCallbacks(myRunnable);
        } 
        catch (Exception e) {
            // TODO: handle exception
        }
    }

    private Runnable myRunnable = new Runnable() {
        public void run() {
            try {
                new get_notifyalert(mcont).execute("")  ;
            }
            catch (Exception e) {
                // TODO: handle exception
            }
            myhandler.postDelayed(myRunnable, RETRY_TIME);
        }
    };
}

Is this is the right way?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CompEng
  • 7,161
  • 16
  • 68
  • 122

1 Answers1

0

Is this the right way?

No. Only have a service running when it is actively delivering value to the user. Watching the clock tick is not actively delivering value to the user. Use AlarmManager for periodic work like this.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • but the other all things are right? I mean sometimes my app is lock while there is internet connection. I think the service block my main app. Because when I did not use service everthing is okey. – CompEng Sep 01 '14 at 13:34
  • and I need to use this every x minutes **new get_notifyalert_service(mcont).execute("") ;** how can I call this func with alarmanager? – CompEng Sep 01 '14 at 13:37
  • @ErsinGülbahar: "but the other all things are right?" -- the rest of your code shown in your question would not exist. "I think the service block my main app" -- well, you are doing work on the main application thread, which is another reason to delete what you have. Use `AlarmManager` to trigger an `IntentService`, which gives you a background thread (unlike your current code) and shuts down when the work is complete (unlike your current code). If you need to wake up the device to do the work, use `WakefulBroadcastReceiver` as part of the process. – CommonsWare Sep 01 '14 at 13:40