Android:创建在应用程序停止时运行的服务 [英] Android: Create service that runs when application stops

查看:24
本文介绍了Android:创建在应用程序停止时运行的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建在应用程序停止时继续运行的服务?我正在清单文件中的单独进程中创建服务:

How do you create a Service that keeps on running when your application is stopped? I am creating the service in a separate process in the manifest file:

<service
    android:name="com.example.myservice"
    android:process=":remoteservice" />

我也在服务的 onStartCommand 中返回 START_STICKY:

I am also returning START_STICKY in the Service's onStartCommand:

@Override
public in onStartCommand(Intent intent, int flags, int startId){
    return Service.START_STICKY;
}

推荐答案

我不会使用 android:process 属性 - 这实际上是在单独的进程中运行您的服务,并且很难执行诸如共享首选项之类的操作.当您的应用程序死掉时,您不必担心您的服务会死掉,该服务将继续运行(这是服务的重点).您也不想要绑定服务,因为它会在绑定时启动和消失.话虽如此:

I wouldn't use the android:process attribute - this actually runs your service in a separate process and makes it hard to do things like share preferences. You won't have to worry about your service dying when your application dies, the service will keep going (that is the point of a service). You also don't want a binding service because that will start and die when the bindings do. That being said:

<service
    android:enabled="true"
    android:exported="true"
    android:name=".MyService"
    android:label="@string/my_service_label"
    android:description="@string/my_service_description"
    <intent-filter>
        <action android:name="com.package.name.START_SERVICE" />
    </intent-filter>
</service>

您可以为启动服务的意图定义自己的操作(不能由系统启动 - 必须是活动).我喜欢将启用"设置为 true 以便系统可以实例化我的服务,并导出"以便其他应用程序可以发送意图并与我的服务进行交互.您可以在此处找到有关此内容的更多信息.

You can define your own action for the intent to launch the service (cannot be launched by the system - has to be an activity). I like to set "enabled" to true so the system can instantiate my service, and "exported" so other applications can send intents and interact with my service. You can find more about this here.

您还可以制作使用绑定的长时间运行的服务,如果您这样做,只需为绑定器添加和意图,例如:

You can also make a long running service that uses bindings, if you do this just add and intent for the binder such as:

<action android:name="com.package.name.IRemoteConnection" />

现在从您的活动中启动服务:

Now to start the service from your activity:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent serviceIntent = new Intent("com.package.name.START_SERVICE");
        this.startService(serviceIntent);

        // You can finish your activity here or keep going...
    }
}

现在开始服务:

public class MyService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        // This is triggered by the binder action intent.
        // You can return your binder here if you want...
        return null;
    }


    @Override
    public void onCreate() {
        // Called when your service is first created.
    }

    @Override
    public in onStartCommand(Intent intent, int flags, int startId) {
        // This is triggered by the service action start intent
        return super.onStartCommand(intent, flags, startId);
    }
}

现在您的服务应该可以运行了.为了帮助长寿,有一些技巧.使其作为前台服务运行(您在服务中执行此操作)- 这将使您创建一个粘贴在状态栏中的通知图标.同时保持你的 HEAP 灯,让你的应用程序不太可能被 Android 杀死.

Now your service should run. To help with the longevity there are some trick. Make it run as a foreground service (you do this in the service) - this will make you create a notification icon that sticks in the status bar. Also keep your HEAP light to make you application less likely to be killed by Android.

当您杀死 DVM 时,服务也会被终止 - 因此,如果您要进入设置-> 应用程序并停止应用程序,您将杀死 DVM 并因此终止服务.仅仅因为您在设置中看到您的应用程序正在运行并不意味着 Activity 正在运行.活动和服务具有不同的生命周期,但可以共享一个 DVM.请记住,如果不需要,您不必终止您的活动,只需让 Android 处理即可.

Also the service is killed when you kill the DVM - thus if you are going to Settings->Applications and stopping the application you are killing the DVM and thus killing the service. Just because you see your application running in the settings does no mean the Activity is running. The activity and service have different life-cycles but can share a DVM. Just keep in mind you don't have to kill your activity if not needed, just let Android handle it.

希望这会有所帮助.

这篇关于Android:创建在应用程序停止时运行的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆