2017-01-26 10 views
0

我想在Android應用程序中連續進行兩個調用。點擊按鈕後,該應用會呼叫第一個號碼。我在下面創建了broadcastreceiver,它檢測到第一次通話結束的時間。它應該寫出「第一次通話結束」,然後撥打第二個號碼。它似乎沒有工作。任何人都可以在我的代碼中發現錯誤嗎?我的廣播接收器似乎檢測到通話已結束,但仍無法完成該操作。有人看到我的代碼中存在缺陷嗎?

public class MainActivity extends AppCompatActivity { 

public void calling(String phone) { 


    Intent callIntent = new Intent(Intent.ACTION_CALL) 
      .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    callIntent.setData(Uri.parse("tel:" + phone)); 
    callIntent.putExtra("com.android.phone.extra.slot", 1); 
    startActivity(callIntent); 
    Intent intent = new Intent(this, CallReciever.class); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243 , intent, 0); 
    startActivity(callIntent); 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Button b = (Button) this.findViewById(R.id.CallButton); 


    b.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      calling("+36309577686"); 

     } 
    }); 
} 

public class CallReciever extends BroadcastReceiver { 

    private Context mContext; 
    private CustomPhoneStateListener mPhoneListener; 
    private String incoming_nr; 
    private int prev_state; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     mContext = context; 

     if (mPhoneListener == null) { 
      mPhoneListener = new CustomPhoneStateListener(); 

      // TelephonyManager object 
      TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 


      // Register our listener with TelephonyManager 
      telephony.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE); 
     } 
    } 

    /* Custom PhoneStateListener */ 
    class CustomPhoneStateListener extends PhoneStateListener { 

     @Override 
     public void onCallStateChanged(int state, String incomingNumber) { 

      if (!TextUtils.isEmpty(incomingNumber)) { 
       incoming_nr = incomingNumber; 
      } 

      switch (state) { 
       case TelephonyManager.CALL_STATE_RINGING: 
        prev_state = state; 
        break; 

       case TelephonyManager.CALL_STATE_OFFHOOK: 
        prev_state = state; 
        break; 

       case TelephonyManager.CALL_STATE_IDLE: 
        if ((prev_state == TelephonyManager.CALL_STATE_OFFHOOK)) { 
         // A call has now ended 
         //it writes out the call end, but does not call. why? 
         Toast.makeText(mContext, "Call End", Toast.LENGTH_SHORT).show(); 
         calling("+36303853440"); 
         prev_state = state; 
        } 
        else if ((prev_state == TelephonyManager.CALL_STATE_RINGING)) { 
         // Rejected or Missed call 
         Toast.makeText(mContext, "Rejected Call", Toast.LENGTH_SHORT).show(); 
         prev_state = state; 
        } 
        break; 

      } 
     } 
    } 
} 
} 
+0

你在哪裏註冊'BroadcastReceiver'?如果它在'AndroidManifest.xml'中,則顯示那部分XML。 – Darwind

+0

@Darwind你可以在這裏看看它:codeshare.io/GkmbjA –

+0

我想你有問題通過回答的意見之一回答? – Darwind

回答

0

您還沒有在任何地方註冊CallReciever。試試這個

Button b = (Button) this.findViewById(R.id.CallButton); 


b.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     calling("+36309577686"); 
     registerReceiver(new CallReceiver()); 
    } 
}); 

然後註銷的廣播接收器接收

+0

但在清單中註冊的那個: 不是那麼簡單嗎? (我也嘗試過你的建議,但它需要一個'registerReceiver()'方法) –

+0

可以發佈你的AndroidManifest.xml文件。 – arjun

+0

你可以在這裏看看它:codeshare.io/GkmbjA –

相關問題