通过在android中滑动删除应用程序时关闭服务

时间:2013-12-05 05:25:14

标签: android android-service

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

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

    }



}

1 个答案:

答案 0 :(得分:4)

谷歌的员工Dianne Hackborn在对她Google+ posts中的一个评论中解释说,你必须在你的服务上实现onTaskremoved。

  当你轻扫最近的任务时,会发生[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告诉应用程序的任何服务有关任务   删除所以它可以做任何它认为合适的事情。

所以我认为你可以这样做:在那个回调你必须停止服务,并告诉警报管理器再次停止启动它。为此,首先,您需要将与AlarmManger一起使用的待定意图传递给服务,以便服务可以使用意图取消计划。 至少,你需要这一切:

在您的服务中

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