telephonymanager sendussdrequest返回ussd响应

gwbalxhn  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(372)

我实现了发送ussd请求的方法,如下所示:

public String sendUssdRequest(String sendTo) {

    try {
        if (hasTelephony){
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

            Handler handler = new Handler(Looper.getMainLooper()) {
                @Override
                public void handleMessage(Message message) {
                    System.out.println("handle USSD*****: " + message.toString());
                }
            };

            TelephonyManager.UssdResponseCallback callback = new TelephonyManager.UssdResponseCallback() {
                private String USSDResponse="";
                @Override
                public void onReceiveUssdResponse(TelephonyManager telephonyManager, String request, CharSequence response) {
                    super.onReceiveUssdResponse(telephonyManager, request, response);
                    System.out.println("ussd: Success with response*****: " + response);
                    USSDResponse=""+response;
                }

                @Override
                public void onReceiveUssdResponseFailed(TelephonyManager telephonyManager, String request, int failureCode) {
                    super.onReceiveUssdResponseFailed(telephonyManager, request, failureCode);
                    System.out.println("ussd: failed with code*****: " + Integer.toString(failureCode));
                    USSDResponse="error";
                }

                /*public String getUSSD(){
                    return USSDResponse;
                }*/

            };

            telephonyManager.sendUssdRequest(sendTo,
                    callback,
                    handler);
        }else
            return "error";

    } catch(Exception e) {
        System.out.println("USSD Failed");
        return "error";
    }
}

我希望“Sendusdrequest”能够回复,我相信回复应该是未来或者类似的事情
由于无法在回调中添加getter,因此创建了以下类:

public class USSDCallback extends TelephonyManager.UssdResponseCallback{

private String USSDResponse="";
@Override
public void onReceiveUssdResponse(TelephonyManager telephonyManager, String request, CharSequence response) {
    super.onReceiveUssdResponse(telephonyManager, request, response);
    System.out.println("ussd: Success with response*****: " + response);
    USSDResponse=""+response;
}

@Override
public void onReceiveUssdResponseFailed(TelephonyManager telephonyManager, String request, int failureCode) {
    super.onReceiveUssdResponseFailed(telephonyManager, request, failureCode);
    System.out.println("ussd: failed with code*****: " + Integer.toString(failureCode));
    USSDResponse="error";
}

public String getUSSD(){
    return USSDResponse;
}

}

并在“Sendusdrequest”中这样称呼它:

public String sendUssdRequest(String sendTo) {
try {
    if (hasTelephony){
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        Handler handler = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(Message message) {
                System.out.println("handle USSD*****: " + message.toString());
            }
        };

        TelephonyManager.UssdResponseCallback callback = new USSDCallback();

        telephonyManager.sendUssdRequest(sendTo,
                callback,
                handler);

        return ((USSDCallback) callback).getUSSD();

    }else
        return "error";

} catch(Exception e) {
    System.out.println("USSD Failed");
    return "error";
}

}
当然,在响应完成之前会调用方法“getussd”,我不想使用timeunit.seconds.sleep(15)(或任何类似的东西)等它,那么有没有方法在响应完成时返回响应呢?
p、 s:我相信处理程序可能会在某种程度上被利用,因为它可以“安排消息和可运行程序在将来某个时候执行”,但我真的不知道!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题