Intent callIntent = new Intent(Intent.ACTION_CALL).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
String hash = Uri.encode("#");
String ussd = ""+code+hash;
callIntent.setData(Uri.parse("tel:"+ussd));
callIntent.putExtra("simSlot", 1);
callIntent.putExtra("com.android.phone.extra.slot", 1);
startActivity(callIntent);
请帮助捕获变量中的 ussd 响应
不幸的是,目前没有标准方法可以在 Android 中捕获 USSD 响应。而且看来以后是没有办法做到这一点了。
使用 TelephonyManager 获取响应。我们可以使用handleUssdRequest发送请求并获取CharSequence形式的响应。然后我们可以在文本视图上显示响应。
private void sendUssdRequest(Context context, String ussdRequest) {
final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
if (tm != null) {
Class<?> telephonyManagerClass = Class.forName(tm.getClass().getName());
if (telephonyManagerClass != null) {
Method getITelephony = telephonyManagerClass.getDeclaredMethod("getITelephony");
getITelephony.setAccessible(true);
Object iTelephony = getITelephony.invoke(tm);
Method[] methodList = iTelephony.getClass().getMethods();
Method handleUssdRequest = null;
for (Method method : methodList) {
if (method.getName().equals("handleUssdRequest")) {
handleUssdRequest = method;
break;
}
}
if (handleUssdRequest != null) {
handleUssdRequest.setAccessible(true);
// 0 for SIM1, 1 for SIM2
handleUssdRequest.invoke(iTelephony, 0, ussdRequest, new ResultReceiver(new Handler()) {
@Override
protected void onReceiveResult(int resultCode, Bundle ussdResponse) {
Object p = ussdResponse.getParcelable("USSD_RESPONSE");
CharSequence message = "";
if (p != null) {
try {
Method getReturnMessage = p.getClass().getMethod("getReturnMessage");
if (getReturnMessage != null) {
message = (CharSequence) getReturnMessage.invoke(p);
textViewUssdResponse.setText(message);
}
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
});
}
}
}
} catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
}
}