Android 我应该在哪个范围内调用stopSelf()?
我已经看到了很多关于这个主题的问题,但是我在使用stopSelf()时仍然遇到问题,因为在调用stopSelf之后,服务没有调用onDestroy(),但是在“设置”的“运行应用”选项卡上没有出现 我使用的服务有点不同 我的服务绑定到一个可运行的任务,每次任务完成时,它都会调用服务上的一个方法,该方法检查是否还有更多的任务要完成,如果没有,则调用stopSelf() 根据我的逻辑,因为我在服务类中,所以这个.stopSelf()应该引用服务的当前实例(我只运行了这个服务,每次最多绑定3个任务) 我还知道系统将“按她的条件”调用onDestroy,而不是立即调用。但我已经等了10-20多分钟了,没有叫onDestroy 一些代码可以更好地理解: 服务:Android 我应该在哪个范围内调用stopSelf()?,android,multithreading,service,ondestroy,Android,Multithreading,Service,Ondestroy,我已经看到了很多关于这个主题的问题,但是我在使用stopSelf()时仍然遇到问题,因为在调用stopSelf之后,服务没有调用onDestroy(),但是在“设置”的“运行应用”选项卡上没有出现 我使用的服务有点不同 我的服务绑定到一个可运行的任务,每次任务完成时,它都会调用服务上的一个方法,该方法检查是否还有更多的任务要完成,如果没有,则调用stopSelf() 根据我的逻辑,因为我在服务类中,所以这个.stopSelf()应该引用服务的当前实例(我只运行了这个服务,每次最多绑定3个任务)
public class MediaDownloadService extends Service {
private DBHelper helper;
private ExecutorService executor;
private static final String TAG = "com.sharongur,root.myapplication";
private final IBinder sharonsBinder = new MyLocalBinder();
File file;
@Override
public IBinder onBind(Intent intent) {
return sharonsBinder;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
helper = new DBHelper(getApplicationContext());
executor = new ThreadPoolExecutor(2,3,3000,TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());
Log.e("requestsExists", helper.requestsExists() + "");
if(helper.requestsExists()){
// map of the index of the request and the string of the absolute path of the request
Map<Integer,String> requestMap = helper.getRequestsToExcute(3);
Set<Integer> keySet = requestMap.keySet();
Iterator<Integer> iterator = keySet.iterator();
Log.e("MAP",requestMap.toString());
//checks if the DB requests exists
if(!requestMap.isEmpty()){
//execute them and delete the DB entry
while(iterator.hasNext()){
int iteratorNext = iterator.next();
Log.e("ITREATOR", iteratorNext + "");
file = new File(requestMap.get(iteratorNext));
Log.e("file", file.toString());
Log.e("thread Opened", "Thread"+iteratorNext);
executor.submit(new MyTask(file, getApplicationContext(), iteratorNext), 1);
helper.requestTaken(iteratorNext);
}
}
}
return START_STICKY;
}
public void startNewTask(){
Log.e("requestExists", helper.requestsExists()+"");
if(helper.requestsExists()){
Map<Integer,String> requestMap = helper.getRequestsToExcute(1);
Set<Integer> keySet = requestMap.keySet();
Iterator<Integer> iterator = keySet.iterator();
while(iterator.hasNext()){
int iteratorNext = iterator.next();
file = new File(requestMap.get(iteratorNext));
Log.e("file", file.toString());
Log.e("thread Opened", "Thread"+iteratorNext);
executor.submit(new MyTask(file, getApplicationContext(), iteratorNext), 1);
helper.requestTaken(iteratorNext);
}
}else{
executor.shutdown();
Log.e("stopself", "stop self after this");
this.stopSelf();
}
}
@Override
public void onCreate() {
super.onCreate();
Log.e("ONCREATE", "SERVICE CREATED");
}
@Override
public void onDestroy() {
Log.e("ONDESTROY", "SERVICE DESTROYED");
super.onDestroy();
}
public class MyLocalBinder extends Binder{
MediaDownloadService getService(){
return MediaDownloadService.this;
}
}
我怀疑我用错了方法。。。
可能this.stopSelf()引用了调用该方法的实例?如果它绑定到任务而不是服务关于
ondestory()
,如果有一个未完成的绑定到服务,我不会期望stopSelf()
导致服务被破坏,因为服务仍然在使用中。@commonware好吧,我想我当时还不完全理解。因为每次任务完成后我都会解开它。。。在任何情况下,我不应该看到服务在设置app->running上运行吗?如果你说他还在使用?如果服务创建了一个新线程,并且调用了stopSelf,那么服务可能会消亡,不是吗?无论服务是否启动,新线程都将继续工作?bindService是否打开新服务?如果是这样,我想这是我的问题。。。我的任务连接到每个任务上的服务,在这种情况下,我创建了许多服务实例。。。每一项任务实际上都是。我以为bindService只绑定到现有的服务。。。这就是问题所在吗?“在任何情况下,我不应该看到服务在设置app->running上运行吗?”--我没有运行很多测试来查看是什么触发了应用程序显示在该列表中。“无论服务是否启动,新线程都将继续其工作?”--您分叉的线程独立于服务。服务是向操作系统发出的信号,表明您的进程正在做对用户有价值的工作,以使您的进程保持更长的时间。我们使用服务管理长期后台线程,这样我们包含这些线程的进程就有可能长到足以让我们完成工作。;“bindService是否打开了一个新服务?”--否。服务是自然的单例服务。在任何时候都将有零个或一个正在运行的服务实例。
public class MyTask implements Runnable {
private File _file;
private Context context;
private DBHelper helper;
private int requestId;
private MediaDownloadService sharonsService;
boolean isBound = false;
public MyTask(File file, Context context, int requestId) {
this._file = file;
this.context = context;
this.requestId = requestId;
}
@Override
public void run() {
Intent intent = new Intent(context,MediaDownloadService.class);
context.bindService(intent,sharonsConnection,Context.BIND_AUTO_CREATE);
// some work
sharonsService.startNewTask();
helper.deleteRequest(requestId);
context.unbindService(sharonsConnection);
} catch (IOException e) {
Log.e("Callable try", post.toString());
}
}
private ServiceConnection sharonsConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder service) {
MyLocalBinder binder = (MyLocalBinder) service;
sharonsService = binder.getService();
isBound = true;
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
isBound = false;
}
};
}