2017-01-26 1 views
0

안드로이드 앱에서 연속해서 두 번 전화를하고 싶습니다. 버튼을 클릭하면 앱이 첫 번째 번호로 전화를 겁니다. 아래 첫 번째 호출이 끝날 때이를 감지하는 broadcastreceiver를 만들었습니다. "첫 번째 통화 종료"라고 쓰고 두 번째 번호로 전화하십시오. 작동하지 않는 것 같습니다. 아무도 내 코드에서 실수를 발견 할 수 있습니까?My 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'을 등록 할 수신 방송의 수신기의 등록을 취소하려고? XML의 일부인 'AndroidManifest.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 –

관련 문제