Skip to content Skip to sidebar Skip to footer

Android: Presenting A Notification During A Call?

I have a broadcast receiver that listens to incoming calls. And I want to tweak the incoming call screen. Right now I can present toasts and add notifications to the notification b

Solution 1:

You need a BroadcastReceiver like that:

publicclassIncomingBroadcastReceiverextendsBroadcastReceiver {

    @OverridepublicvoidonReceive(Context context, Intent intent) {

        MyLog.d("IncomingBroadcastReceiver: onReceive: ");

        Stringstate= intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        MyLog.d("IncomingBroadcastReceiver: onReceive: " + state);
        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
        {
            Intenti=newIntent(context, IncomingCallActivity.class);
            i.putExtras(intent);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            context.startActivity(i);
        }

    }

}

And register it in the manifest to <action android:name="android.intent.action.PHONE_STATE"></action>.

Then create an Activity like that:

publicclassIncomingCallActivityextendsActivity {

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {

        MyLog.d("IncomingCallActivity: onCreate: ");

        // TODO Auto-generated method stubsuper.onCreate(savedInstanceState);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

        setContentView(R.layout.main);

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

which has this layout:

<?xml version="1.0" encoding="utf-8"?><TextViewxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/text"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center_vertical|center_horizontal"android:text="text"android:windowBackground="@android:color/transparent"android:windowIsTranslucent="true"android:windowAnimationStyle="@android:style/Animation.Translucent"></TextView>

This will produce a translucent dialog-like activity on top of the incoming call screen, that allows the user to answer the call (doesn't interfere with touch events).

Solution 2:

Up to Android 2.3 you can not override the calling screen, but you can show a Toast message on it. The longest period of a toast is 3 seconds and after that it will dissapear. You can however create a thread that calls show() method of the toast every 2 seconds. Something like that:

Thread t = new Thread() {
        publicvoidrun() {

            try {
                while (true) {
                    if( isInCall ){

                        toast.cancel();
                        break;
                    }

                    toast.show();
                    sleep(1850);
                }
            } catch (Exception e) {

                Log.d("Exception: " + e.toString());
            }
        }
    };
    t.start();

You have to declare toast:

private Toast toast;

You have to init the toast object befor showing it.

toast = newToast(getBaseContext());    
LayoutInflaterinflater= (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    Viewlayout= inflater.inflate(R.layout.toast, null, false);

    toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(layout);

Solution 3:

This is not possible, answer coming straight from the Android team

http://markmail.org/message/wttvtmxzqnsbyodx#query:+page:1+mid:fjtly3xmriql5xxa+state:results

Post a Comment for "Android: Presenting A Notification During A Call?"