gpt4 book ai didi

android - 如何在 Android 中以 block 方法停止服务?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:22:03 25 4
gpt4 key购买 nike

  1. 我运行代码得到如下结果,但我希望App能够按照“A”->“Service OnDestroy”->“B”->“C”的顺序运行,我怎么能做 ?

  2. 我的方式 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:Service 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");
}
});

}

}

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);
}


}

=======================我的方式 1 ================== ====================

我设置了一个标记 isServiceStoped 来监控停止服务是否完成,但是我的应用程序在显示结果“11-13 11:31:23.107 7599-7599/info.dodata.screenrecorder E/My:A”后挂断了

新的 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);
}
}

最佳答案

  1. 不,您不能同步停止服务。 stopService() 是停止服务的请求。它会在一段时间后尽快停止。

  2. 不,您不能从 onDestroy() 方法中删除 super.onDestroy() 并且仍然让它正常工作。

关于android - 如何在 Android 中以 block 方法停止服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33684879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com