Dhaval Chheda
Dhaval Chheda

Reputation: 5147

Can I program what should be displayed on the screen when I receive a phone call in android and also while I am on that call

I am trying to create a custom display on my app so when somebody browsing my app receives a phone call then it should display a custom image and then when the person answers the call and during the duration of the call there will be another image on it. I have been able to identify the phone events successfully but I am not able to override the display of the default phone app. Please check the code I am using and advise what can be done

public class MainActivity extends Activity {

    ImageView imageView1 , imageView2;

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

        TelephonyManager telephonyManager =
                (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

        PhoneStateListener callStateListener = new PhoneStateListener() {
            public void onCallStateChanged(int state, String incomingNumber)
            {
                if(state==TelephonyManager.CALL_STATE_RINGING){
                    /*Toast.makeText(getApplicationContext(), "Phone Is Riging",
                            Toast.LENGTH_LONG).show();*/
                    imageView1 = (ImageView) findViewById(R.id.sample_image);
                    imageView1.setVisibility(ImageView.VISIBLE);
                    imageView1.setImageResource(R.drawable.image1);
                }
                if(state==TelephonyManager.CALL_STATE_OFFHOOK){
                    /*Toast.makeText(getApplicationContext(),"Phone is Currently in A call",
                            Toast.LENGTH_LONG).show();*/
                    imageView1.setVisibility(View.GONE);
                    imageView2 = (ImageView) findViewById(R.id.sample_image_two);
                    imageView2.setVisibility(ImageView.VISIBLE);
                }

                if(state==TelephonyManager.CALL_STATE_IDLE){
                    Toast.makeText(getApplicationContext(), "phone is neither ringing nor in a call",
                            Toast.LENGTH_LONG).show();
                }
            }
        };
        telephonyManager.listen(callStateListener,PhoneStateListener.LISTEN_CALL_STATE);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

}

activity_main file looks like this

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.android.kiddo.calltesting.MainActivity" >

    <ImageView
        android:id="@+id/sample_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/image1"
        android:scaleType="centerCrop"
        android:visibility="gone"/>

    <ImageView
        android:id="@+id/sample_image_two"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/image2"
        android:scaleType="centerCrop"
        android:visibility="gone"/>

</RelativeLayout>

Will really appreciate any kind of assistance received. Thanking you in anticipation,

Upvotes: 0

Views: 41

Answers (1)

Dhaval Chheda
Dhaval Chheda

Reputation: 5147

Thank you Mohit and Rohit , i was able to use Broadcast Receiver to solve my issue.. Thanks a lot for your help

My code looks something like this as of now

    public class Receiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {                                         
            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);                         
            String msg = "Phone state changed to " + state;

            if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) {                                   
                /*String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);  // 5
                msg += ". Incoming number is " + incomingNumber;*/

                // TODO This would be a good place to "Do something when the phone rings"
                 // set opacity of the phone app
                setAlpha(100);
                //start activity
                Intent i = new Intent();
                i.setClassName("com.android.kiddo.broadcastreceivertutorial","com.android.kiddo.broadcastreceivertutorial.Ringing");
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(i);

            }
}

Upvotes: 1

Related Questions