安卓:创建一个运行时应用程序停止服务时应、创建一个、程序

2023-09-05 11:24:05 作者:吻你的资本专属我 ?

如何创建一个服务,保持在当你的应用程序停止运行?我创建一个单独的进程服务清单文件中:

 <服务
    机器人:名称=com.example.myservice
    机器人:工艺=:remoteservice/>
 

我也回START_STICKY在服务的onStartCommand:

  @覆盖
公众onStartCommand(意向意图,诠释标志,诠释startId){
    返回Service.START_STICKY;
}
 

解决方案

我不会使用android:process属性 - 这实际运行您的服务在一个单独的进程,使得它很难做的事情一样份额preferences 。你会不会担心你的服务临死的时候你的应用程序死亡,该服务将继续下去,(这是一个服务点)。你也不想绑定服务,因为这将启动并死亡时,绑定做。话虽这么说:

 <服务
    机器人:启用=真
    机器人:出口=真
    机器人:为MyServiceNAME =
    机器人:标签=@字符串/ my_service_label
    机器人:说明=@字符串/ my_service_description
    <意向滤光器>
        <作用机器人:名称=com.package.name.START_SERVICE/>
    &所述; /意图滤光器>
< /服务>
 
安卓手机启动app程序一直 停止运行 怎么办

您可以定义自己的行动意图推出的服务(不能被系统启动 - 必须是一个活动)。我喜欢设置为Enabled为true,以便系统可以实例化我的服务,而出口,以便其他应用程序可以发送意图和我的服务进行交互。你可以找到更多关于这个这里。

您还可以使用绑定,如果你这样做只是添加和意图的粘合剂如长时间运行的服务:

 <作用机器人:名称=com.package.name.IRemoteConnection/>
 

现在从你的活动启动服务:

 公共类MainActivity延伸活动{

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        意图serviceIntent =新的意向书(com.package.name.START_SERVICE);
        this.startService(serviceIntent);

        //你可以在这里完成你的活动或继续下去......
    }
}
 

现在的服务:

 公共类的MyService延伸服务{

    @覆盖
    公众的IBinder onBind(意向意图){
        //这是通过粘合剂行动意图触发。
        //你可以在这里将您的粘合剂,如果你想...
        返回null;
    }


    @覆盖
    公共无效的onCreate(){
        //第一次创建服务时调用。
    }

    @覆盖
    公众onStartCommand(意向意图,诠释标志,诠释startId){
        //这是由服务行动启动意图引发
        返回super.onStartCommand(意向,标志,startId);
    }
}
 

现在你的服务应该运行。为了帮助长寿也有一些诀窍。让它作为前台服务,(这样做的服务)运行 - 这会让你创建一个坚持在状态栏通知图标。同时请您堆光,使你的应用程序不太可能搭载Android被杀死。

此外,当你杀了DVM服务被杀害 - 因此,如果你要设置 - >应用程序,并阻止你正在扼杀DVM的应用程序,因此查杀服务。仅仅因为你看到运行中的设置您的应用程序没有意思的活动正在运行。活动和服务有不同的生命周期,但可以共享一个DVM。请记住,你没有杀人,如果不需要你的活动,只是让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" />

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

解决方案

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>

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

Now for the service:

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

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.

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.

Hope this helps.