use*_*872 43 service android background intentservice
IntentService即使应用程序被杀,我也想继续在后台运行.并且被"杀死"我的意思是按住主页按钮很长时间 - > 查看所有正在运行的应用程序 - >将我的应用程序放在一边 - > 应用程序被杀或按下后退按钮很长时间 - > 应用程序被杀
我的代码如下.在我的MainActivity中:
Intent intent = new Intent(this, MyService.class);
this.startService(intent);
Run Code Online (Sandbox Code Playgroud)
在我的MyService中:
public class MyService extends IntentService {
@Override
protected void onHandleIntent(Intent intent) {
System.out.println("MyService started");
run();
}
private void run() {
while (true){
System.out.println("MyService still running");
doSomething();
waitSomeTime();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我看到应用程序打开时服务正在运行.当我通过主页按钮最小化应用程序时,它仍在运行.当我通过后退按钮关闭应用程序时,它仍在运行.但如果我如上所述杀死它,它就会停止.我该如何解决这个问题?
Bhe*_*aar 35
如果您的应用程序启动了您的服务,那么实际上您的服务正在主进程上运行.因此,当应用程序被杀死时,服务也将被停止.所以你可以做的是,从onTaskRemoved你的服务方法发送广播如下:
Intent intent = new Intent("com.android.ServiceStopped");
sendBroadcast(intent);
Run Code Online (Sandbox Code Playgroud)
并有一个广播接收器,将再次启动服务.我试过了.服务从所有类型的杀戮重新开始.
Say*_*Sil 29
所有的答案看起来都是正确的,所以我会继续在这里给出一个完整的答案.
首先,最简单的方法是在手动杀死应用程序时,在Android中启动广播,然后定义一个自定义以在此之后触发服务重新启动.BroadcastReceiver
现在让我们跳转到代码.
在中创建您的服务 YourService.java
请注意该onCreate()方法,我们为大于Android Oreo的 Build版本启动前台服务的方式不同.这是因为最近引入了严格的通知政策,我们必须定义自己的通知渠道才能正确显示它们.
该this.sendBroadcast(broadcastIntent);在onDestroy()方法是异步发送一个广播动作名称的声明"restartservice".我们稍后将使用它作为重启我们服务的触发器.
在这里,我们定义了一个简单的Timer任务,它每隔1秒打印一个计数器值,并Log在每次打印时自行递增.
public class YourService extends Service {
public int counter=0;
@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O)
startMyOwnForeground();
else
startForeground(1, new Notification());
}
@RequiresApi(Build.VERSION_CODES.O)
private void startMyOwnForeground()
{
String NOTIFICATION_CHANNEL_ID = "example.permanence";
String channelName = "Background Service";
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(chan);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.setOngoing(true)
.setContentTitle("App is running in background")
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(2, notification);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
startTimer();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
stoptimertask();
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("restartservice");
broadcastIntent.setClass(this, Restarter.class);
this.sendBroadcast(broadcastIntent);
}
private Timer timer;
private TimerTask timerTask;
public void startTimer() {
timer = new Timer();
timerTask = new TimerTask() {
public void run() {
Log.i("Count", "========= "+ (counter++));
}
};
timer.schedule(timerTask, 1000, 1000); //
}
public void stoptimertask() {
if (timer != null) {
timer.cancel();
timer = null;
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
创建一个广播接收器以响应您的自定义广播 Restarter.java
具有"restartservice"您刚定义的操作名称的广播YourService.java现在应该触发一个将重新启动您的服务的方法.这是BroadcastReceiver在Android中使用.
我们覆盖内置onRecieve()方法BroadcastReceiver以添加将重新启动服务的语句.在startService()将无法按照预期的工作,在以上的Android 8.1奥利奥,如严格的背景下政策将很快重启后终止服务,一旦应用程序被杀害.因此,我们使用startForegroundService()for更高版本并显示连续通知以保持服务运行.
public class Restarter extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("Broadcast Listened", "Service tried to stop");
Toast.makeText(context, "Service restarted", Toast.LENGTH_SHORT).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(new Intent(context, YourService.class));
} else {
context.startService(new Intent(context, YourService.class));
}
}
}
Run Code Online (Sandbox Code Playgroud)
定义您MainActivity.java在应用启动时调用服务.
这里我们定义一个单独的isMyServiceRunning()方法来检查后台服务的当前状态.如果服务没有运行,我们通过使用启动它startService().
由于应用程序已在前台运行,因此我们无需将服务作为前台服务启动,以防止自身被终止.
请注意,onDestroy()我们专门调用stopService(),以便调用我们的重写方法.如果没有这样做,那么在app被杀死之后服务就会自动结束,而不会调用我们修改过的onDestroy()方法YourService.java
public class MainActivity extends AppCompatActivity {
Intent mServiceIntent;
private YourService mYourService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mYourService = new YourService();
mServiceIntent = new Intent(this, mYourService.getClass());
if (!isMyServiceRunning(mYourService.getClass())) {
startService(mServiceIntent);
}
}
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
Log.i ("Service status", "Running");
return true;
}
}
Log.i ("Service status", "Not running");
return false;
}
@Override
protected void onDestroy() {
stopService(mServiceIntent);
super.onDestroy();
}
}
Run Code Online (Sandbox Code Playgroud)
最后在你的账号中注册 AndroidManifest.xml
以上三个类都需要单独注册AndroidManifest.xml.
请注意,我们定义了一个intent-filter与操作名称作为"restartservice"其中Restarter.java注册为receiver.这可确保BroadcastReciever在系统遇到具有给定操作名称的广播时调用我们的自定义.
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver
android:name="Restarter"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="restartservice" />
</intent-filter>
</receiver>
<activity android:name="MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="YourService"
android:enabled="true" >
</service>
</application>
Run Code Online (Sandbox Code Playgroud)
如果应用程序从任务管理器中被杀死,现在应该再次重新启动服务.只要用户没有Force Stop来自" 应用程序设置"的应用程序,此服务将继续在后台运行.
Ima*_*shi 10
在您的服务中,添加以下代码。
@Override
public void onTaskRemoved(Intent rootIntent){
Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
restartServiceIntent.setPackage(getPackageName());
PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmService.set(
AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + 1000,
restartServicePendingIntent);
super.onTaskRemoved(rootIntent);
}
Run Code Online (Sandbox Code Playgroud)
原因是您正在尝试使用 IntentService。这是API 文档中的一行
IntentService 执行以下操作:
处理完所有启动请求后停止服务,因此您无需调用 stopSelf()。
因此,如果您希望服务无限期地运行,我建议您扩展 Service 类。然而,这并不能保证您的服务将无限期地运行。如果你的服务是低优先级的,那么你的服务仍然有机会在低内存状态下被内核杀死。所以你有两个选择:
1)通过调用该方法保持它在前台运行startForeground() 。
2)如果服务被杀死,请重新启动该服务。这是文档中示例的一部分,其中讨论了服务被终止后重新启动服务
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
// For each start request, send a message to start a job and deliver the
// start ID so we know which request we're stopping when we finish the job
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
mServiceHandler.sendMessage(msg);
// If we get killed, after returning from here, restart
return START_STICKY;
}
Run Code Online (Sandbox Code Playgroud)
在onstart命令put START_STICKY...这个服务不会杀死,除非它做了太多任务而内核想要为它杀死它...
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("LocalService", "Received start id " + startId + ": " + intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
Run Code Online (Sandbox Code Playgroud)