gpt4 book ai didi

android - 当我强行关闭我的应用程序时服务停止。我怎样才能使服务保持不变?

转载 作者:搜寻专家 更新时间:2023-11-01 08:26:58 25 4
gpt4 key购买 nike

这是我的服务:

public class KeepAliveService  extends Service {
Alarm alarm = new Alarm();
public void onCreate()
{
Log.i("","KEEPALIVE onCreate");
super.onCreate();
Notification notification = new Notification();
startForeground(1337, notification);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
alarm.setAlarm(this);
Log.i("","KEEPALIVE onCreate start command");
return START_STICKY;
}

@Override
public void onStart(Intent intent, int startId)
{
alarm.setAlarm(this);
Log.i("","KEEPALIVE onStart");
}

@Override
public IBinder onBind(Intent intent)
{
return null;
}
}

我的 list 中有这个:

   <service
android:name="com.vidyo.vidyomod.KeepAliveService"
android:exported="true"
android:process=":KeepAliveService"
android:enabled="true">
</service>
<receiver android:process=":remote" android:name="com.vidyo.vidyomod.utils.Alarm"></receiver>
<receiver android:name="com.vidyo.vidyomod.utils.AutoStart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>

我是这样开始的:

    Intent i= new Intent(BaseActivity.this, KeepAliveService.class);
startService(i);

但是当我从应用程序列表(电话广场)中清除我的应用程序时,我得到了这个日志,之后服务就不再工作了:

05-16 14:52:22.698: I/ActivityManager(1102): Killing 9049:com.vidyo.vidyomod:KeepAliveService/u0a132, isShadow:false (adj 2): stop com.vidyo.vidyomod
05-16 14:52:22.725: D/ActivityManager(1102): SVC-handleAppDiedLocked: app = ProcessRecord{ff18702 9049:com.vidyo.vidyomod:KeepAliveService/u0a132, isShadow:false}, app.pid = 9049
05-16 14:52:22.726: W/ActivityManager(1102): Scheduling restart of crashed service com.vidyo.vidyomod/.KeepAliveService in 1000ms
05-16 14:52:22.726: V/BroadcastQueue(1102): removeReceiverListLocked app = ProcessRecord{ff18702 9049:com.vidyo.vidyomod:KeepAliveService/u0a132, isShadow:false}
05-16 14:52:22.726: V/BroadcastQueue(1102): removeReceiverListLocked app = ProcessRecord{ff18702 9049:com.vidyo.vidyomod:KeepAliveService/u0a132, isShadow:false}
05-16 14:52:22.726: V/BroadcastQueue(1102): removeReceiverListLocked app = ProcessRecord{ff18702 9049:com.vidyo.vidyomod:KeepAliveService/u0a132, isShadow:false}
05-16 14:52:22.857: I/ActivityManager(1102): Force stopping service ServiceRecord{3c2a0a1 u0 com.vidyo.vidyomod/.KeepAliveService, isShadow:false}
05-16 14:52:22.859: W/ActivityManager(1102): Spurious death for ProcessRecord{ff18702 0:com.vidyo.vidyomod:KeepAliveService/u0a132, isShadow:false}, curProc for 9049: null

编辑:

现在在我的 Activity 中我这样做:

    Intent i= new Intent(VidyoModApplication.this, KeepAliveService.class);
startService(i);

在我的服务中,在 onStardCom 并且我这样做:

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
alarm.setAlarm(this);
Log.i("","KEEPALIVE onCreate start command");
Intent notificationIntent = new Intent(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_HOME);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

Notification notification = new Notification.Builder(this)
.setContentTitle(getText(R.string.app_name))
.setContentText("Service is running")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendingIntent)
.build();

startForeground(1337, notification);
return START_STICKY;
}

但是,当我强行关闭应用程序时,它仍然会停止服务。找到这个例子后试了一下: http://www.truiton.com/2014/10/android-foreground-service-example/

最佳答案

我要在这里冒险。我不知道您使用的具体设备。但是,有许多设备具有用于“ protected ”或“特殊”应用程序的特殊设置页面,允许在后台运行。如果您有这些设备之一,并且您的应用不在“ protected ”应用列表中,并且您的 Service 被终止,Android 将不会重新启动它,即使您已将其声明为前台Service 并从 onStartCommand() 返回 START_STICKY

请检查您遇到此问题的设备是否属于这种情况。

参见 https://stackoverflow.com/a/41369032/769265https://stackoverflow.com/a/42120277/769265

关于android - 当我强行关闭我的应用程序时服务停止。我怎样才能使服务保持不变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44002209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com