3

How to open a custom UI on incoming call with answer and reject button on it,I want to show custom UI instead of default dialer. I am using below code but dialer is open and my activity is not opened:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.callintruptdemo"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".IncomingCall"></activity>

    <receiver android:name=".CallReceiver">
        <intent-filter >
            <action android:name="android.intent.action.PHONE_STATE"></action>
        </intent-filter>
    </receiver>
</application>

I have a broadcast receiver which listen for incoming call and in receiver is:

@Override
public void onReceive(Context context, Intent intent) {
       Log.d("CallReceiver","IncomingBroadcastReceiver: onReceive: ");

        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        Log.d("CallReceiver","IncomingBroadcastReceiver: onReceive: " + state);
        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
        {
            Intent i = new Intent(context, IncomingCall.class);
            i.putExtras(intent);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            context.startActivity(i);
        }
}

and code for IncomingCall class is:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

    setContentView(R.layout.activity_main);

    String number = getIntent().getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
    TextView text = (TextView)findViewById(R.id.text);
    text.setText("Incoming call from " + number);
}

but my custom UI is not displayed.

I also want button on my UI and how to receive a incoming call by click of that button. Thanks in advance.

edit:-- After updating my code I am able to open my custom UI, now I want to receive call by clicking a button on it. How to do this any help..

JulianHarty
  • 3,178
  • 3
  • 32
  • 46
Ravi Bhandari
  • 4,682
  • 8
  • 40
  • 68
  • Maybe your device has selected the default dialer to always receive incoming calls. Do a web search on how to clear your default app for that. – npace Jan 21 '14 at 08:22
  • i a testing it on imulator,when i run app it shows dialer but when i debug app,it show my custom ui.So where i am missing. – Ravi Bhandari Jan 21 '14 at 08:26
  • check this [link][1],[link1][2], and [link][3] [1]: http://stackoverflow.com/questions/9256218/show-my-activity-instead-of-standard-screen-for-incoming-call [2]: http://stackoverflow.com/questions/8699257/popup-over-incoming-call-screen [3]: http://stackoverflow.com/questions/2486547/android-incoming-call-screen – Imtiyaz Khalani Jan 21 '14 at 08:33

1 Answers1

3

First do the following :

try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

then call the abortBroadcast() method before calling your activity and make sure to put a priority in your intentFilter like the following :

 <intent-filter android:priority="99999" >
            <action android:name="android.intent.action.PHONE_STATE" />
  </intent-filter>

and to answer the incoming phone call from the custom UI do the following :

answerButton = (Button) findViewById(R.id.pickup);
    answerButton.setOnClickListener(new OnClickListener() {
        public void onClick(final View v) {
            Intent answer = new Intent(Intent.ACTION_MEDIA_BUTTON);
            answer.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP,   KeyEvent.KEYCODE_HEADSETHOOK));
     context.sendOrderedBroadcast(answer, null);

        }
    });

and to reject the call do the following :

rejectButton= (Button) findViewById(R.id.pickup);
    rejectButton= .setOnClickListener(new OnClickListener() {
        public void onClick(final View v) {
            Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON);
    buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
    getApplicationContext().sendOrderedBroadcast(buttonDown, "android.permission.CALL_PRIVILEGED");


    }
});

Hope that helps

JulianHarty
  • 3,178
  • 3
  • 32
  • 46
  • tried your code but still same default dialer is open on incoming call – Ravi Bhandari Jan 21 '14 at 10:05
  • after using your code and adding a delay before calling my custom UI i am able to show my UI,now how can i receive call by clicking button. – Ravi Bhandari Jan 21 '14 at 10:26
  • thats a new question :) plaese give me a correct answer and start a new question . –  Jan 21 '14 at 10:27
  • added your code alone is not working after adding delay it workes for me.or i can say your answer is not complete.and this is not a new question,this is also a part of my initial question. – Ravi Bhandari Jan 21 '14 at 10:49
  • i have try your code and found exception while receive call by clicking button.exception is -android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.ANSWER flg=0x10000000 } – Ravi Bhandari Jan 21 '14 at 11:17
  • after trying your code i have added one more code to it for making it working,before accept call i have to set intent Intent.ACTION_HEADSET_PLUG,so it is working for me.so i am marking your answer as correct.but one more thing is currently reject button is not working.so how to do this. – Ravi Bhandari Jan 23 '14 at 05:56