如何在30分钟内运行我服务一次?(how to run my service once in 30

2019-09-01 17:56发布

任何机构可以说我一个简单的方法,在半小时内运行一次的服务?

这不是atall工作任何机构可以说,如何在一个半小时请运行一次。

我用这个开始在系统启动我的应用程序,甚至不工作..?

我这样做:

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

谢谢!

Answer 1:

试试这个 :

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


Answer 2:

使用AlarmManager及其方法setRepeating() 前段时间我问类似的问题 。 我的间隔时间是24小时,但你0,5h这是同样的故事。



文章来源: how to run my service once in 30 minutes?