Dismissing a custom incoming call screen on Android when the call is answered or the phone stops ringing -


i want have custom incoming call screen shows when there incoming call, , dismissed if call answered or phone stops ringing.

i've searched several posts on stackoverflow show me how can implement that, , far there , after checking pros , cons of each approach in terms of how fast screen gets shown, i've settled on windowmanager approach.

whenever call wm.removeview(ly) stack below

07-08 20:36:41.002  27547-27547/com.testtelephoney.customincomingcall e/androidruntime﹕ fatal exception: main process: com.testtelephoney.customincomingcall, pid: 27547 java.lang.illegalargumentexception: view=android.widget.linearlayout{7e63aae v.e..... ......i. 0,0-0,0} not attached window manager         @ android.view.windowmanagerglobal.findviewlocked(windowmanagerglobal.java:402)         @ android.view.windowmanagerglobal.removeview(windowmanagerglobal.java:328)         @ android.view.windowmanagerimpl.removeviewimmediate(windowmanagerimpl.java:84)         @ com.testtelephoney.customincomingcall.myphonestatelistener.oncallstatechanged(myphonestatelistener.java:43)         @ android.telephony.phonestatelistener$2.handlemessage(phonestatelistener.java:392)         @ android.os.handler.dispatchmessage(handler.java:102)         @ android.os.looper.loop(looper.java:145)         @ android.app.activitythread.main(activitythread.java:5832)         @ java.lang.reflect.method.invoke(native method)         @ java.lang.reflect.method.invoke(method.java:372)         @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1399) 

here code:

myphonestatelistener.java

import android.content.context; import android.graphics.pixelformat; import android.telephony.phonestatelistener; import android.telephony.telephonymanager; import android.util.log; import android.view.gravity; import android.view.layoutinflater; import android.view.view; import android.view.windowmanager; import android.widget.linearlayout; import android.widget.relativelayout;   public class myphonestatelistener extends phonestatelistener{     context mcontext;     public myphonestatelistener(context ct) {         mcontext = ct;     }     public void oncallstatechanged(int state,string incomingnumber){          windowmanager wm = (windowmanager) mcontext.getsystemservice(context.window_service);         windowmanager.layoutparams params = new windowmanager.layoutparams(windowmanager.layoutparams.match_parent, windowmanager.layoutparams.match_parent, windowmanager.layoutparams.type_system_alert|windowmanager.layoutparams.type_system_overlay,windowmanager.layoutparams.flag_not_touch_modal|windowmanager.layoutparams.flag_not_focusable, pixelformat.transparent);          params.height= windowmanager.layoutparams.match_parent;         params.width= windowmanager.layoutparams.match_parent;         params.format=pixelformat.transparent;         params.gravity= gravity.top;          linearlayout ly;          final layoutinflater inflater = (layoutinflater)mcontext.getsystemservice(context.layout_inflater_service);         ly=(linearlayout)inflater.inflate(r.layout.activity_ic,null);           switch (state){             case telephonymanager.call_state_idle:                 log.d("debug","idle");                 if(ly.getvisibility()== view.visible){                     wm.removeview(ly);                 }                 break;             case telephonymanager.call_state_offhook:                 log.d("debug","offhook");                 break;             case telephonymanager.call_state_ringing:                 log.d("debug","ringing");                 wm.addview(ly,params);                 break;         }     } } 

servicereceiver

import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.telephony.phonestatelistener; import android.telephony.telephonymanager;  public class servicereceiver extends broadcastreceiver {     public servicereceiver() {     }      @override     public void onreceive(context context, intent intent) {         // todo: method called when broadcastreceiver receiving         // intent broadcast.         myphonestatelistener phonelistener = new myphonestatelistener(context);         telephonymanager telephony = (telephonymanager)context.getsystemservice(context.telephony_service);         telephony.listen(phonelistener, phonestatelistener.listen_call_state);     } } 

androidmanifest

    <uses-permission android:name="android.permission.get_accounts" />     <uses-permission android:name="android.permission.read_profile" />     <uses-permission android:name="android.permission.read_contacts" />     <uses-permission android:name="android.permission.system_alert_window"/>     <receiver android:name=".servicereceiver" >             <intent-filter>                 <action android:name="android.intent.action.phone_state" />             </intent-filter>      </receiver> 

i've added flag mark view has been added. need improved off course.

 switch (state){         case telephonymanager.call_state_idle:             log.d("debug","idle");             if (viewcreated) {                 wm.removeview(ly);                 viewcreated = false;             }             break;         case telephonymanager.call_state_offhook:             log.d("debug","offhook");             break;         case telephonymanager.call_state_ringing:             log.d("debug","ringing");             if (!viewcreated){                 windowmanager.layoutparams params = new windowmanager.layoutparams(windowmanager.layoutparams.match_parent, windowmanager.layoutparams.match_parent, windowmanager.layoutparams.type_system_alert|windowmanager.layoutparams.type_system_overlay,windowmanager.layoutparams.flag_not_touch_modal|windowmanager.layoutparams.flag_not_focusable, pixelformat.transparent);                 params.height= windowmanager.layoutparams.match_parent;                 params.width= windowmanager.layoutparams.match_parent;                 params.format=pixelformat.transparent;                 params.gravity= gravity.top;                  ly=(linearlayout)inflater.inflate(r.layout.activity_login,null);                 wm.addview(ly, params);                 viewcreated=true;             }             break;     } 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -