how to run my service once in 30 minutes?

房东的猫 提交于 2019-12-01 12:14:54

问题


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!


回答1:


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);



回答2:


Use AlarmManager and its method setRepeating(). Some time ago I asked similar question. My interval was 24h, but with yours 0,5h it's the same story.



来源:https://stackoverflow.com/questions/16336028/how-to-run-my-service-once-in-30-minutes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!