【问题标题】:How to start a video (Inside an activity) when a call is answered and stop the activity when the call is ended?如何在接听电话时启动视频(在活动内)并在通话结束时停止活动?
【发布时间】:2020-01-01 15:12:00
【问题描述】:

我的代码运行良好,我使用的是 Brodcast 接收器。

在我的应用程序中,当用户单击一个按钮时,会启动一个电话,然后第二个活动 (outcall.java) 会弹出屏幕,在外呼活动中我有一个 VideoView,我想要做的是视频呼叫被应答时开始,呼叫结束时活动被终止。

我的代码还有另一个问题,我希望第二个活动仅在我使用应用程序内的按钮时启动,因为现在即使我打电话给朋友,它也会始终启动。

我们将不胜感激

这是我的 MainActivity

public class MainActivity extends AppCompatActivity {
Button play;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        registerReceiver(broadcastReceiver, new IntentFilter(""));
play = (Button)findViewById(R.id.button2);
final MediaPlayer mP=MediaPlayer.create(MainActivity.this,R.raw.reco);
play.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v) {
        if (mP.isPlaying()){
            mP.pause();
            play.setBackgroundResource(R.drawable.play);
        }else {
            mP.start();
            play.setBackgroundResource(R.drawable.pause);
        }
    }
                        });
        ActivityCompat.requestPermissions(this, new String[]{CALL_PHONE}, PackageManager.PERMISSION_GRANTED);
    }
      BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {


            OpenCallOut();



        }
    };
    public void OpenCallOut(){
        Intent intent = new Intent(this, Outcall.class);
        startActivity(intent);
    }

    public void CallButton(View view) {
        if (checkSelfPermission(Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                 return;
        }
        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "123456"));
        startActivity(intent);
         }
         @Override
    public void onDestroy() {

        finish();
        System.exit(0);

    }
}

这是我的广播,我在 codetoart 中找到了这个但不知道如何使用它

public class Broadcast extends BroadcastReceiver {
    private static final String TAG = "PhoneStateBroadcastReceiver";
    Context mContext;
    String incoming_nr;
    private int prev_state;
    @Override
    public void onReceive(Context context, Intent intent) {

        context.sendBroadcast(new Intent(""));


        TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();
        telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

        Bundle bundle = intent.getExtras();
        String phoneNr= bundle.getString("incoming_number");
        Log.v(TAG, "phoneNr: "+phoneNr);
        mContext=context;
    }


    public class CustomPhoneStateListener  extends PhoneStateListener {

        private static final String TAG = "CustomPhoneStateListener";

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

            if(incomingNumber!=null&&incomingNumber.length()>0) incoming_nr=incomingNumber;

            switch(state){
                case TelephonyManager.CALL_STATE_RINGING:
                    Log.d(TAG, "CALL_STATE_RINGING");
                    prev_state=state;
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    Log.d(TAG, "CALL_STATE_OFFHOOK");
                    prev_state=state;
                    break;
                case TelephonyManager.CALL_STATE_IDLE:
                    Log.d(TAG, "CALL_STATE_IDLE==>"+incoming_nr);
                    if((prev_state==TelephonyManager.CALL_STATE_OFFHOOK)){
                        prev_state=state;

                    }
                    if((prev_state==TelephonyManager.CALL_STATE_RINGING)){
                        prev_state=state;

                    }
                    break;

            }
        }
    }
}

这是我的第二个活动 Outcall.Java

public class Outcall1 extends AppCompatActivity {
    VideoView myVideo;
    private MediaController media_control;

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




        myVideo = (VideoView) findViewById(R.id.videoView);

        Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.myvideo);

        media_control = new MediaController(this);

        myVideo.setMediaController(media_control);

        myVideo.setVideoURI(uri);
        myVideo.start();

    }


}

【问题讨论】:

    标签: java android


    【解决方案1】:

    在android中发送广播基本上有多种方式。

    简单来说,有些是由于系统事件(例如所有拨出电话),而有些是特定应用程序私有的,即应用程序内的本地广播。

    1.在您的情况下,第二个活动总是启动,因为您的 onRecieve() 正在响应系统事件。因此,您应该尝试使用LocalBroadcastManager.sendBroadcast 在单击按钮时发送本地广播。 或者,您也可以使用 Service 来启动您的第二个活动。在这种情况下,在开始调用意图之前启动您的服务。在服务内您可以设置一个小的延迟,然后启动第二个活动。

    1. 为了在通话结束时完成您的第二个活动,您可以使用您发布但知道的上述广播接收器来检测通话结束,然后以防电话状态为通话断开。触发另一个广播以由第二个活动接收,该活动在接收它时完成活动。

    This 将帮助您执行第 2 步。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多