android-如何在30分钟内运行一次我的服务?
作者:互联网
任何人都可以说一个半小时运行一次服务的简单方法吗?
这绝不是一件容易的事,任何人都可以说如何在半小时内运行一次.
我用它来启动我的应用程序在系统启动,即使不起作用..?
我正在这样做:
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");
}
}
谢谢!
解决方法:
尝试这个 :
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);
标签:android,background-service 来源: https://codeday.me/bug/20191010/1885088.html