2015-08-03 45 views
4

我創建一個應用程序在Android中,這需要在後臺運行USSD代碼。沒有送我的後臺應用程序,運行USSD代碼在Android和保持應用程序在firstground

每當我使用Intent.ACTION_CALL運行USSD

String ussdCode = "*" + "123" + Uri.encode("#"); 
startActivity(new Intent("android.intent.action.CALL", Uri.parse("tel:" + ussdCode))); 

發給我的後臺應用程序,並打開撥號界面上我的應用程序。

所以有可能沒有前開撥號接口運行USSD代碼。

謝謝。

+0

同樣的問題有:http://stackoverflow.com/questions/5449464/calls-a-ussd-number-without-opening-the-phone-app-in-android – Plugie

回答

1

使用下面的代碼:

String ussdCode = "*123#"; 
Intent intent = new Intent(Intent.ACTION_CALL); 
intent.setData(ussdToCallableUri(ussdCode)); 
try{ 
    startActivity(intent); 
} catch (SecurityException e){ 
    e.printStackTrace(); 
} 

這裏是你的USSD代碼轉換爲可調用USSD方法:

private Uri ussdToCallableUri(String ussd) { 

    String uriString = ""; 

    if(!ussd.startsWith("tel:")) 
     uriString += "tel:"; 

    for(char c : ussd.toCharArray()) { 

     if(c == '#') 
      uriString += Uri.encode("#"); 
     else 
      uriString += c; 
    } 

    return Uri.parse(uriString); 
} 
+2

我不明白爲什麼這個答案被接受。您的代碼與OP編寫的代碼相同。你的代碼只有更多的行。 –

相關問題