Search code examples
androidbackground-service

how to run my service once in 30 minutes?


Can any body say me a simple way to run an service once in half an hour?

this is not atall working can any body say how to run it once in half an hour pls.

i use this for start my app on system boot even that is not working..?

i am doing this :

autostart.java

public class autostart extends BroadcastReceiver 
{
    public void onReceive(Context arg0, Intent arg1) 
    {
        Intent intent = new Intent(arg0,back_process.class);
        arg0.startService(intent);
        Log.i("Autostart", "started");
    }
}

Back_Process.java

 public class gps_back_process extends Service
    {
        private static final String TAG = "MyService";
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
        public void onDestroy() {

            Toast.makeText(this, "SERVICE STOPPED ..!", Toast.LENGTH_LONG).show();
            Log.d(TAG, "onDestroy");
        }

        @Override
        public void onStart(Intent intent, int startid)
        {
            Intent intents = new Intent(getBaseContext(),MainActivity.class);
            intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intents);
            Toast.makeText(this, "SERVICE STARTED", Toast.LENGTH_LONG).show();
            Log.d(TAG, "onStart");
        }
    }

Thank you!


Solution

  • Try this :

    private AlarmManager alarmMgr;
    private PendingIntent alarmIntent;
    
    alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmReceiver.class);
    alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    
    alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() +
            60 * 1000, alarmIntent);