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

编程入门 行业动态 更新时间:2024-10-10 16:24:10
本文介绍了通过在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在对她的 Google+帖子,您必须在服务上实现onTaskremoved.

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)杀死应用程序的任何后台或空进程(请参阅 developer.android/guide/topics/fundamentals/processes-and-threads.html#Lifecycle (这意味着什么),以及(2)使用新 developer.android/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 developer.android/guide/topics/fundamentals/processes-and-threads.html#Lifecycle for what this means), and (2) uses the new developer.android/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中滑动删除应用程序时关闭服务

本文发布于:2023-11-27 12:39:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638113.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!