【问题标题】:Close the service when remove the app via swipe in android通过在android中滑动删除应用程序时关闭服务
【发布时间】:2016-02-16 19:22:39
【问题描述】:

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

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

    }



}

【问题讨论】:

    标签: android android-service


    【解决方案1】:

    据 google 的员工 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 类

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

    【讨论】:

    • 嘿,谢谢......它正在工作......但是当我进入主屏幕时,服务停止,当我重新启动我的应用程序时,服务开始......我不想要它......我想要的是,服务应该运行,直到我从应用程序列表中删除......我该怎么做???
    • 您可以在 onDestroy() 上执行,而不是在 onStop 回调中执行此操作。当活动不可见时调用 onStop,当活动不再在内存中时调用 onDestroy。无论如何,在 android 中这是不可信的,因为系统可以在不调用 onDestroy 的情况下销毁 Activity,或者即使您在应用列表中关闭它,也可以考虑让它在后台运行。
    • 让我编辑答案,以便对您更有用。
    • 是的,我尝试使用 onDestroy,但在这种情况下发生了什么,服务停止了,但我希望它继续下去,直到用户从当前应用程序列表中删除...
    • 在这种情况下,系统调用服务的onTaskRemoved(Intent rootIntent)。所以,只需按照更新示例中的方式实现它
    猜你喜欢
    • 1970-01-01
    • 2014-11-21
    • 2023-01-19
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多