通过在android中滑动删除应用程序时关闭服务 [英] Close the service when remove the app via swipe in android

查看:67
本文介绍了通过在android中滑动删除应用程序时关闭服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户从当前正在运行的应用程序列表中删除该应用程序时,我想关闭该服务.在这里,我正在做的是,当用户启动应用程序时,服务会启动并保持进度.但是,当用户通过滑动删除应用程序时,就会创建新的服务.我想关闭服务.下面是我的代码.

I want to close the service when user removes the app from current running app list. Here, what I'm doing, when user starts the app, service gets started and remain in progress. But when user removes the app via swipe, new service is being created. I want to close the service. Below is my code.

// Start service using AlarmManager



    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, 10);

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

    PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);

    AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                5000, pintent);
    startService(new Intent(getBaseContext(), MyService.class));

MyService.java

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class MyService extends Service {
    int count = 0;
    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        Toast.makeText(this, "The new Service was Created", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onStart(Intent intent, int startId) {
        // For time consuming an long tasks you can launch a new thread here...
        count++;
        Toast.makeText(this, " Service Started" + "  " + count, Toast.LENGTH_LONG).show();

    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();

    }



}

推荐答案

根据Google员工Dianne Hackborn在对她的

According with google's employee Dianne Hackborn explains in a comment on one of her Google+ posts, you have to implemente the onTaskremoved on your service.

[W]当您轻扫最近的任务时,会发生什么? (1)杀死应用程序的任何后台或空进程(请参阅 http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html#Lifecycle (这意味着什么),以及(2)使用新 http://developer.android.com/reference/android/app/Service.html#onTaskRemoved(android.content.Intent) 用于告知应用程序任何服务有关该任务的API 删除,因此它可以执行其认为适当的任何操作.

[W]hat specifically happens when you swipe away a recent task is it: (1) kills any background or empty processes of the application (see http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html#Lifecycle for what this means), and (2) uses the new http://developer.android.com/reference/android/app/Service.html#onTaskRemoved(android.content.Intent) API to tell any services of the application about the task being removed so it can do whatever it thinks is appropriate.

因此,我认为您可以这样操作:在该回调中,您必须停止该服务,并告诉警报管理器停止再次启动它.为此,首先,您需要将与AlarmManger一起使用的未决意图传递给服务,以便服务可以使用该意图来取消计划. 至少,您需要具备以下所有条件:

So I think you can do it this way: In that callback you have to stop the service, and tell the alarm manager to stop starting it again. For that, first of all, you need to pass to the service the pending intent that you use with the AlarmManger, so the service can use the intent to cancel the schedule. At least, you need all this:

为您服务

public class MyService extends Service {
    private DefaultBinder mBinder;
    private AlarmManager  alarmManager ;
    private PendingIntent alarmIntent;

    private void setAlarmIntent(PendingIntent alarmIntent){
        this.alarmIntent=alarmIntent;
    }

    public void onCreate() {
        alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        mBinder = new DefaultBinder(this);
    }

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

    public void onTaskRemoved (Intent rootIntent){
        alarmManager.cancel(alarmIntent);
        this.stopSelf();
    }
}

然后在其他文件中,创建DefaultBinder类

Then in other file, you create DefaultBinder Class

public class DefaultBinder extends Binder {
    MyService s;

    public DefaultBinder( MyService s) {
        this.s = s;
    }

    public MyService getService() {
        return s;
    }
}

您的活动

MyService service;
protected ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder binder) {
        service = ((DefaultBinder) binder).getService();
        service.setAlarmIntent(pIntent);
    }

    public void onServiceDisconnected(ComponentName className) {
        service = null;
    }
};

protected void onResume() {
    super.onResume();
    bindService(new Intent(this, MainService.class), mConnection,
            Context.BIND_AUTO_CREATE);
}

@Override
protected void onStop() {
    super.onStop();

    if (mConnection != null) {
        try {
            unbindService(mConnection);
        } catch (Exception e) {}
    }
}

这篇关于通过在android中滑动删除应用程序时关闭服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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