1

My code is working perfectly, i used a Brodcast Receiver.

In my application when the user click a button, a phone call is started, and the second activity (outcall.java) popup the screen, in the outcall Activity i have a VideoView, what I want to do is that the Video starts when the call is answered, and the activity is killed when the call is ended.

I have another problem with my code, i want the second activity to launch only when I use the button inside the application, because now it launches always even if i call a friend.

help will be appreciated

Here is my 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);

    }
}

Here is my Broadcast, i found this in codetoart but don't know how to use it

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;

            }
        }
    }
}

Here is my second activity 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();

    }


}

Hamita
  • 25
  • 1
  • 5

1 Answers1

0

There are basically multiple ways for sending broadcasts in android.

In simple terms, some are due to system events such as the all the outgoing calls, while some are private to a particular application i.e the local broadcasts within the app.

1.In your case, the second activity is launching always because your onRecieve() is responding to system events. Hence you should try to sending local broadcast on the button click using the LocalBroadcastManager.sendBroadcast. Alternatively , you can also use Service for starting your second activity .In this case , before starting call intent start your service .Inside the servie you can set a small delay and then start the second activity.

  1. And for finishing your second activity on call end , you can use the above broadcast reciever that you posted but know for detecting call end, and then in case when the state of phone is call disconnected. Fire another broadcast to be recieved by the second activity which finishes the activity on recieving it.

This will help you with the implementation of the step 2.

Kaveri
  • 1,060
  • 2
  • 12
  • 21
  • Thank you for your answer, the problem i am newbie, and i dont know how to use the snippest in the link, can you modify it for me? – Hamita Jan 01 '20 at 16:09
  • this https://android--code.blogspot.com/2015/12/android-how-to-send-and-receive-local.html should you to implement the local broadcasts – Kaveri Jan 01 '20 at 16:27