【问题标题】:How should I use PendingIntent to make a second call?我应该如何使用 PendingIntent 拨打第二个电话?
【发布时间】:2017-01-27 15:25:08
【问题描述】:

我想在一个安卓应用程序中连续拨打两个电话。单击按钮后,应用程序会呼叫第一个号码。我在下面创建了广播接收器,它检测第一个呼叫何时结束。它应该写出“第一次通话结束”,然后拨打第二个号码。我想我应该用 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;

            }
        }
    }
}

}

【问题讨论】:

    标签: android broadcastreceiver call system-calls android-pendingintent


    【解决方案1】:

    此代码没有待处理的意图

    如果呼叫被接受、拒绝或取消,它将适用于所有情况。

    让我知道它是否适合你

    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();
    
    }
    

    }

    【讨论】:

    • 实际上,应用程序应该只有一个按钮,点击按钮后应用程序会调用第一个号码。当第一个通话结束时,它应该自动拨打第二个号码
    • 创建一个新方法 secondCall();而不是 button2.performClick() 调用 secondCall();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多