我已经看到很多关于这个主题的问题,但是在使用stopSelf()时仍然遇到问题,因为服务在调用stopSelf后没有调用onDestroy(),但是没有出现在设置上正在运行的应用程序选项卡上
我使用的服务有点不同......
我的服务绑定到一个可运行的任务,每次任务完成时,它会调用服务上的一个方法,检查是否有更多的任务要完成,如果没有,则调用stopSelf()。
根据我的逻辑,因为我在服务类中,this.stopSelf()应该引用服务的当前实例(我只运行此服务,并且每次最多绑定3个任务)。
我也知道系统将根据她的条款调用onDestroy,而不是立即。我还在等待超过10-20分钟而没有onDestroy被称为
一些代码可以更好地理解:
服务:
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;
}
}
任务:
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;
}
};
}
我怀疑我以某种方式使用错误的方法... 也许this.stopSelf()引用调用方法的实例?如果它绑定到任务而不是服务