I'm asking this question because my Java knowledge is really low... I need need to use this new API 27 USSD feature... Below if What I'm trying to do :
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*****************************
}
The golad I'm trying to achieve is to runn the USSD request and print the result in a Toast. When I launch the service, it says service created as expected, it goes into the doJob()
method as expected, but after that, nothing else happens... The app does not even crash... Just as if after enterring doJob()
no instructions was written...
Can you help me make this code work ?