我可以在 android 中完全重新启动服务吗?
Can I restart service completely fresh in android?
我运行正在通过服务和其中的异步任务执行后台任务。首先,它从 db 获取所有数据并开始 运行 当用户更改某些选项时我销毁并再次启动服务以处理新数据但不幸的是服务 运行s 从它停止的地方开始。所以我想开始全新的服务。我试过了 START_NOT_STICKY
但没有用。
public class MyService extends Service {
public void onCreate(){
super.onCreate();
}
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle extras = intent.getExtras();
process.execute();
return START_NOT_STICKY;
}
public void onDestroy() {
stopSelf();
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
class ProcessImageAsync extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
// here im getting content from db and processing .
if (isCancelled()) {
System.exit(0);
}
}
}
}
我的 activity 代码是我重新启动服务的方式
public void startServiceFucntion(int type){
if(isMyServiceRunning(MyService.class)){
if (type == 1){
Intent serviceIntent = new Intent(this, MyService.class);
stopService(serviceIntent);
startServiceFucntion(0);
}
} else {
Intent serviceIntent = new Intent(this, MyService.class);
if(serviceReceiver == null){
registerReceiver(serviceReceiver, intentSFilter);
}
startService(serviceIntent);
}
}
我认为您没有正确停止服务,您的异步任务在后台继续 运行。您还需要在销毁服务时取消异步任务。
你不停止服务property.I认为你可以在AndroidManifest.xml中注册一个广播来重启服务或销毁服务
A service is a component that runs in the background to perform
long-running operations without needing to interact with the user and
it works even if application is destroyed.
AsyncTask enables proper and easy use of the UI thread. This class
allows to perform background operations and publish results on the UI
thread without having to manipulate threads and/or handlers.
所以你不能停止你的服务因为你的AsyncTask继续运行。
我运行正在通过服务和其中的异步任务执行后台任务。首先,它从 db 获取所有数据并开始 运行 当用户更改某些选项时我销毁并再次启动服务以处理新数据但不幸的是服务 运行s 从它停止的地方开始。所以我想开始全新的服务。我试过了 START_NOT_STICKY
但没有用。
public class MyService extends Service {
public void onCreate(){
super.onCreate();
}
public int onStartCommand(Intent intent, int flags, int startId) {
Bundle extras = intent.getExtras();
process.execute();
return START_NOT_STICKY;
}
public void onDestroy() {
stopSelf();
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
class ProcessImageAsync extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
// here im getting content from db and processing .
if (isCancelled()) {
System.exit(0);
}
}
}
}
我的 activity 代码是我重新启动服务的方式
public void startServiceFucntion(int type){
if(isMyServiceRunning(MyService.class)){
if (type == 1){
Intent serviceIntent = new Intent(this, MyService.class);
stopService(serviceIntent);
startServiceFucntion(0);
}
} else {
Intent serviceIntent = new Intent(this, MyService.class);
if(serviceReceiver == null){
registerReceiver(serviceReceiver, intentSFilter);
}
startService(serviceIntent);
}
}
我认为您没有正确停止服务,您的异步任务在后台继续 运行。您还需要在销毁服务时取消异步任务。
你不停止服务property.I认为你可以在AndroidManifest.xml中注册一个广播来重启服务或销毁服务
A service is a component that runs in the background to perform long-running operations without needing to interact with the user and it works even if application is destroyed.
AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
所以你不能停止你的服务因为你的AsyncTask继续运行。