我运行代码并得到以下结果,但我希望应用程序可以按“a”->“service ondestroy”->“b”->“c”的顺序运行,我该怎么办?
在我的方法2部分中,我尝试将代码放入函数new Handler().postDelayed(new Runnable() {}
,没关系,它按照“a”->“service ondestroy”->“b”->“c”的顺序运行,
我不知道为什么这条路能成功,我不知道这条路是不是好路!
结果
11-13 10:04:32.137 27947-27947/info.dodata.screenrecorder e/my:a
11-13 10:04:32.147 27947-27947/info.dodata.screenrecorder e/my:b
11-13 10:04:32.157 27947-27947/info.dodata.screenrecorder e/my:c
11-13 10:04:32.157 27947-27947/info.dodata.screenrecorder e/my:服务OnDestroy
UIAbou.CS
public class UIAbout extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_about);
Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
startService(intent1);
Button btnReturn = (Button) findViewById(R.id.btnReturn);
btnReturn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.e("My", "A");
Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
stopService(intent1);
Log.e("My", "B");
Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
Log.e("My", "C");
}
});
}
}
记录服务.cs
public class RecordService extends Service {
private Context mContext;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate(){
}
@Override
public void onDestroy(){
Log.e("My","Service OnDestroy");
super.onDestroy(); //It seems that the APP is OK if I remove this.
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent,flags,startId);
}
}
一号公路======================================
我设置了一个标记isServiceStoped以监视停止服务是否完成,但显示结果“11-13 11:31:23.107 7599-7599/info.dodata.screenRecorder e/my:”后,我的应用程序将挂起
新uiabout.cs
public class UIAbout extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_about);
Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
startService(intent1);
Button btnReturn = (Button) findViewById(R.id.btnReturn);
btnReturn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.e("My", "A");
Intent intent1 = new Intent(UIAbout.this, bll.RecordService.class);
stopService(intent1);
while (RecordService.isServiceStoped==false){
//It block
}
Log.e("My", "B");
Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
Log.e("My", "C");
}
});
}
}
新的recordservice.cs
public class RecordService extends Service {
public static boolean isServiceStoped=true;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate(){
}
@Override
public void onDestroy(){
Log.e("My", "Service OnDestroy");
isServiceStoped=true;
super.onDestroy(); //It seems that the APP is OK if I remove this.
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
isServiceStoped=false;
return super.onStartCommand(intent,flags,startId);
}
}
我的路2==========================================
我试着将代码放入函数
new Handler().postDelayed(new Runnable() {}
,没关系,它按照“a”->“service ondestroy”->“b”->“c”的顺序运行,我不知道为什么这条路能成功,我不知道这条路是否好
最后一个uiabout.cs
public class UIAbout extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_about);
Intent intent1 = new Intent(UIAbout.this,bll.RecordService.class);
startService(intent1);
Button btnReturn = (Button) findViewById(R.id.btnReturn);
btnReturn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.e("My", "A");
Intent intent1 = new Intent(UIAbout.this, bll.RecordService.class);
stopService(intent1);
new Handler().postDelayed(new Runnable() {
public void run() {
Log.e("My", "B");
Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
Log.e("My", "C");
}
}, 1);
}
});
}
}
最后一个recordservice.cs
public class RecordService extends Service {
private Context mContext;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate(){
}
@Override
public void onDestroy(){
Log.e("My", "Service OnDestroy");
super.onDestroy(); //It seems that the APP is OK if I remove this.
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent,flags,startId);
}
}
最佳答案
不,不能同时停止服务。stopService()是停止服务的请求。它会在以后的某个时候尽快停止。
不,不能从ondestroy()方法中删除super.ondestroy(),并且仍然可以正常工作。