当前位置: 首页 > 知识库问答 >
问题:

Android:应用被杀时保持服务运行

邵君植
2023-03-14

我想要在后台运行intentservice,即使应用程序被关闭。我的意思是长按Home键->查看所有正在运行的应用程序->把我的应用程序划到一边->应用程序被杀死或者长按Back键->应用程序被杀死

我的代码如下所示。在我的主要活动中:

Intent intent = new Intent(this, MyService.class);
this.startService(intent);

在我的服务中:

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

}

我看到当app打开时服务正在运行。当我通过主页按钮最小化应用程序时,它还在运行。当我通过后退按钮关闭应用程序时,它还在运行。但如果我像上面提到的那样杀了它,它就会停下来。我怎么解决这个?

共有1个答案

寿意远
2023-03-14

所有的答案似乎都是正确的,所以我将继续在这里给出一个完整的答案。

首先,最简单的方法就是在应用程序被手动关闭时在Android中启动广播,然后定义一个自定义的broadcastreceiver来触发服务重启。

现在让我们跳入代码。

onDestroy()方法中的this.sendBroadcast(broadcastIntent);异步发送操作名为“RestartService”的广播的语句。稍后我们将使用此作为重新启动服务的触发器。

这里我们定义了一个简单的计时器任务,它每隔1秒在日志中打印一个计数器值,同时每次打印时自己递增。

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

创建广播接收器以响应restarter.java中自定义的广播

您刚刚在yourservice.java中定义的动作名为“restartservice”的广播现在应该触发一个将重新启动服务的方法。这是在Android中使用broadcastreceiver完成的。

我们重写broadcastreceiver中内置的onreceive()方法,以添加将重新启动服务的语句。startservice()将无法在Android Oreo 8.1及以上版本中正常工作,因为一旦应用程序被杀死,严格的后台策略将在重新启动后很快终止服务。因此,我们将startForegroundService()用于更高版本,并显示一个连续通知以保持服务运行。

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

定义mainactivity.java以在应用程序启动时调用服务。

这里我们定义了一个单独的isMyServiceRunning()方法来检查后台服务的当前状态。如果服务没有运行,我们使用startservice()启动它。

由于app已经在前台运行,我们无需将该服务作为前台服务推出,以防止其自身被终止。

请注意,在onDestroy()中,我们专门调用了stopservice(),以便调用被重写的方法。如果没有这样做,那么在应用程序被杀死之后,服务将自动结束,而不需要调用YourService.java中修改过的onDestroy()方法

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);
        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction("restartservice");
        broadcastIntent.setClass(this, Restarter.class);
        this.sendBroadcast(broadcastIntent);
        super.onDestroy();
    }
}

最后在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>

如果应用程序从任务管理器中被杀死,这将重新启动您的服务。只要用户不在应用程序设置中强制停止应用程序,此服务就会一直在后台运行。

更新:向Jacky博士致敬,因为他指出了这一点。上面提到的方法只有在调用服务的ondestroy()时才会起作用,在某些情况下可能并非如此,这是我没有意识到的。多谢了。

 类似资料:
  • 我想让IntentService在后台运行,即使应用程序被终止。但如果我从最近的屏幕上删除我的应用程序,我的服务就会停止。我怎样才能避免这种情况?换句话说,即使我的应用关闭了,最近的应用也无法运行,我该如何保持我的服务运行? 我的前台服务: 我的常数: 我的清单: 当应用程序打开时,我看到服务正在运行。当我通过home按钮最小化应用程序时,它仍在运行。当我通过后退按钮关闭应用程序时,它仍在运行。但

  • 我有一个实现远程后台服务的应用程序。这个服务是用来下载线程中的文件的(我想说这个服务是作为下载管理器工作的)。 当我想下载一个文件时,我将url发送给服务,服务启动一个线程(我使用的是AsyncTask,但它只在Android4.1中工作)。但下载迟早会停止,我能够知道这一点,因为我显示的通知不再更新。当我单击取消下载的通知时,将向服务发送一个挂起的意图,告诉它取消下载,但当服务重新创建时,将取消

  • 在我的应用程序中,我有一个在后台(和前台)运行。 在这个中,有一个,当它完成时,我想在某个中启动应用程序并在中自动执行一些操作,即使我的应用程序被杀死或关闭,我也希望它能正常工作。 我看到了一些答案,这是其中之一,但它对我不起作用。我找到了一个更好的解决方案,并将其作为答案发布在下面,但仍然只有在应用程序关闭但未完全关闭时才有效。 如何从后台启动,即使应用程序已关闭或关闭?

  • 我正在开发一个追踪用户位置的应用程序。我已经实现了一个定制服务来管理位置请求部分,它在每个活动中都能正常工作。该服务甚至出现在“运行服务”设置面板中。 当我最小化应用程序或锁定屏幕时,问题就开始了。 我想要的效果是,即使应用程序被最小化或屏幕被锁定,也能继续接收位置更新,但当用户从最近的应用程序中滑动应用程序时,停止与应用程序相关的一切(确切地说,谷歌地图或Waze的行为方式-当应用程序显示通知时

  • 问题内容: 我正在开发Django项目,并在专用服务器上对其进行测试。该项目正在运行: Django 1.9.6 virtual environment python 2.7 cx_Oracle 5.2.1 run 一切顺利。例如,项目正在运行并得到: 问题:服务器经常停机,而shell上没有显示错误 。我只是收到: 我如何获取更多信息以找到造成这种情况的原因? 注意 :目前没有gunicorn

  • 问题内容: 我正在使用redis在nodejs应用程序中提供会话支持。我已经安装了redis服务器,当我运行redis- server时,它可以工作,但是当我关闭终端redis时,它停止工作,并且不起作用。关闭终端后如何保持Redis服务器运行? 问题答案: 将Redis作为守护程序启动的最简单方法是编辑配置文件并更改以下行: 启动配置文件时,请确保在redis-server命令行上提供该配置文件