gpt4 book ai didi

android - Android的IntentService类中的死循环或空闲代码

转载 作者:行者123 更新时间:2023-11-29 23:54:11 25 4
gpt4 key购买 nike

我问这个问题是因为我的 Java 知识真的很低......我需要 需要使用这个新的 API 27 USSD 功能......下面是我正在尝试做的事情:

public class MyService extends IntentService {

// BEGIN of MyService Class properties ****

public static boolean jobInProgress = true;

private Handler myHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg); // I guess this will be on some message queue somewhere
}
};


TelephonyManager tm;

// END of properties *************************************

// BEGIN of MyService class abstract class methods implementation

class MyCallback extends TelephonyManager.UssdResponseCallback{

Context serviceContext;

MyCallback (Context serviceContext){
this.serviceContext = serviceContext;
}
public void onReceiveUssdResponse (TelephonyManager telephonyManager,
String request,
CharSequence response){
//Here since it's a System callback I guess my this.tm == telephonyManager parameter right ?
Toast.makeText(serviceContext, "Response from network is : " + response, Toast.LENGTH_LONG).show();

MyService.jobInProgress = false;
}

public void onReceiveUssdResponseFailed (TelephonyManager telephonyManager,
String request,
int failureCode){
Toast.makeText(serviceContext, "USSD request failed with code " + failureCode, Toast.LENGTH_LONG).show();
MyService.jobInProgress = false;
}
}
// END of abstract methods implementation******************

//BEGIN of MyService Class methods

@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service is created.", Toast.LENGTH_LONG).show();
}

@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service is destroyed", Toast.LENGTH_LONG).show();
}

@Override
protected void onHandleIntent(@Nullable Intent intent) {
doJob();
while(jobInProgress){
//I hang here to not call onDestroy to quickly...
}
}

private void doJob(){

//Get the instance of TelephonyManager
this.tm =(TelephonyManager)getSystemService(this.TELEPHONY_SERVICE);


if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
//Don't know How to use sendUssdRequest second and thrid arguments. Below is what I have tried with no success
this.tm.sendUssdRequest("#105*2#",new MyCallback(this),myHandler);
}



//END of class methods*****************************
}

我想要实现的目标是运行 USSD 请求并在 Toast 中打印结果。当我启动该服务时,它说服务按预期创建,它按预期进入 doJob() 方法,但在那之后,没有其他任何事情发生......该应用程序甚至没有崩溃......就好像输入了doJob()之后没有写指令...

你能帮我使这段代码工作吗?

最佳答案

它不起作用的原因是因为您同时使用了 Handler 和无限循环。

当您创建一个新的Handler 时,它会绑定(bind)到创建它的线程的线程/消息队列。因此,当您执行以下操作时:

while(jobInProgress){
//I hang here to not call onDestroy to quickly...
}

它将阻塞工作线程和 Handler。结果什么也没发生。

解决方案是使用普通服务并避免任何循环。

请记住,从 Android O 开始,您无法无休止地在后台运行您的服务。如果您的应用程序处于前台,则此方法将起作用。如果你想让它可靠地工作,请使用前台服务。

关于android - Android的IntentService类中的死循环或空闲代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50546316/

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