0

안드로이드 앱에서 두 번 연속해서 전화를 걸고 싶습니다. 버튼을 클릭하면 앱이 첫 번째 번호로 전화를 겁니다. 아래 첫 번째 호출이 끝날 때이를 감지하는 broadcastreceiver를 만들었습니다. "첫 번째 통화 종료"라고 쓰고 두 번째 번호로 전화하십시오. 나는 PendingIntent와 함께해야한다고 생각한다. 필자는 코드 작성시 (필자가 PendingIntent 인 매개 변수를 가진) 메소드를 작성해야한다고 생각하는 주석을 달았다. 아무도 나를 보여줄 수 있나요?두 번째 전화를 걸려면 PendingIntent를 어떻게 사용해야합니까?

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); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243 , callIntent, 0); 

//i think here should I use pendingIntent somehow, but I have no idea how 





} 




@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("+11111111"); 








     } 
    }); 


} 



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("+22222222"); 
         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

이 코드는 보류중인 의도

그것은 호출이 수락 거부되거나 취소되는 경우 모든 경우에 작동하지 않고있다.

는 일 경우

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 
Button button1,button2; 
boolean FLAG = false; 

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

    button1 = (Button)findViewById(R.id.callButton); 
    button2 = (Button)findViewById(R.id.callButton2); 

    button1.setOnClickListener(this); 
    button2.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) { 

    if(v == button1) 
    { 

     Intent intent = new Intent(); 
     intent.setAction(Intent.ACTION_CALL); 
     intent.setData(Uri.parse("tel:"+"number1")); 
     startActivity(intent); 
     Toast.makeText(MainActivity.this, ""+FLAG, Toast.LENGTH_SHORT).show(); 
     FLAG = true; 
    } 

    if(v == button2) 
    { 
     Intent intent = new Intent(); 
     intent.setAction(Intent.ACTION_CALL); 
     intent.setData(Uri.parse("tel:"+"number2")); 
     startActivity(intent); 


    } 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    if(FLAG) 
    { 
     button2.performClick(); 
     new Handler().postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       FLAG = false; 
      } 
     },5000); 
    } 
    Toast.makeText(MainActivity.this, ""+"resumed"+FLAG, Toast.LENGTH_SHORT).show(); 

} 

}

+0

실제로 앱이 하나의 버튼이 있어야하고 버튼을 클릭시 앱이 첫 번째 번호를 호출에 대해 알려주세요. 첫 번째 호출이 끝나면 두 번째 숫자가 자동으로 호출됩니다. –

+0

새 메소드 만들기 secondCall(); button2.performClick() 대신에 secondCall()을 호출하십시오. –

관련 문제