2015-05-19 81 views
1

我想通過我的應用程序通過放置額外的數據打電話,但在通話過程中我無法接收該數據可以解釋下面我錯了什麼是我的撥打電話的代碼和接收呼叫android add extra and call programmatically

String uri = "tel:"+my_name; 
     Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(uri)); 
     callIntent.putExtra("data", "mynewdata"); 
     callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     _context.startActivity(callIntent); 

並調用接收器

public class PhoneStateReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 

     final Bundle bundle = intent.getExtras(); 
     if(bundle != null) { 
      showToast(intent.getExtras().getString("data")); 
     } 

     if (intent.getAction().equals("android.intent.action.PHONE_STATE")) { 
      String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); 
      Log.d(TAG,"PhoneStateReceiver**Call State=" + state); 
      if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) { 
       if(recieveCall){ 

       } 
       Log.d(TAG,"PhoneStateReceiver**Idle"); 
      } else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { 

      } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) { 
      } 
     } else if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) { 
      // Outgoing call 
      String outgoingNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); 
      Log.d(TAG,"PhoneStateReceiver **Outgoing call " + outgoingNumber); 

      setResultData(null); // Kills the outgoing call 

     } else { 
      Log.d(TAG,"PhoneStateReceiver **unexpected intent.action=" + intent.getAction()); 
     } 
      } 
    } 

我收到呼叫完美,甚至廣播工作完美,但我從沒有意圖獲取數據「intent.getExtras()。getBoolean(」數據「)」

+0

其實你的數據傳遞給Intent.ACTION_CALL問題並且只能使用getIntent()在Intent.ACTION_CALL中獲取數據。您收到的廣播是針對其他一些不包含您提供的意圖數據的線程。你在廣播中的意圖與你傳遞的意圖不同。您需要以其他方式傳遞數據。 –

+0

嘗試像這樣'Bundle bundle = intent.getBundleExtra(「data」);' – Vijay

+0

或'intent.getStringExtra(「data」));' – Vijay

回答

1

在這裏,你需要創建一個Java文件(可以將其命名爲Constants.java):

public class Constants 
{ 
public static boolean data; 
public static String str = ""; 
} 

現在看到的變化我在項目與評論一起做。

String uri = "tel:"+my_name; 
     Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(uri)); 
     Constants.data = true; /*see change, set data in Constants.data*/ 
     Constants.str = "some data to pass..."; /*see change*/ 
     callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     _context.startActivity(callIntent); 

現在,在廣播接收器看到的變化做...

public class PhoneStateReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 

     final Bundle bundle = intent.getExtras(); 
     if(bundle != null && Constants.data) { /*changes done, get boolean from the Constants.data*/ 
      showToast("hi i have recieve"); 
      showToast(Constants.str); /*changes done*/ 
     } 

     if (intent.getAction().equals("android.intent.action.PHONE_STATE")) { 
      String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); 
      Log.d(TAG,"PhoneStateReceiver**Call State=" + state); 
      if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) { 
       if(recieveCall){ 

       } 
       Log.d(TAG,"PhoneStateReceiver**Idle"); 
      } else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { 

      } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) { 
      } 
     } else if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) { 
      // Outgoing call 
      String outgoingNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); 
      Log.d(TAG,"PhoneStateReceiver **Outgoing call " + outgoingNumber); 

      setResultData(null); // Kills the outgoing call 

     } else { 
      Log.d(TAG,"PhoneStateReceiver **unexpected intent.action=" + intent.getAction()); 
     } 
      } 
    } 

希望這會解決您如何來傳遞數據

+0

讓我試試這個等一分鐘 –

+0

它的工作就像一個魅力 –

+0

它已經在工作。我希望它解決了你的問題。 :) –