Monitoring of phone status in the service

Source: Internet
Author: User

 Public classAddressserviceextendsService { Public Static FinalString tag = "Addressservice"; PrivateTelephonymanager MTM; PrivateMyphonestatelistener Mphonestatelistener; Private FinalWindowmanager.layoutparams Mparams =NewWindowmanager.layoutparams (); PrivateView Mviewtoast; PrivateWindowManager MWM; PrivateString maddress; PrivateTextView Tv_toast; PrivateHandler Mhandler =NewHandler () { Public voidhandlemessage (android.os.Message msg) {tv_toast.settext (maddress);    };    }; Private int[] mdrawableids; @Override Public voidonCreate () {//When you start the service for the first time, you need to manage the display of the toast.//Phone Status monitoring (when the service is turned on, it needs to be monitored, when the phone status is not required to listen)//1, Phone Manager ObjectMTM =(Telephonymanager) Getsystemservice (Context.telephony_service); //2, monitor phone statusMphonestatelistener =NewMyphonestatelistener ();        Mtm.listen (Mphonestatelistener, phonestatelistener.listen_call_state); //Get Form ObjectMWM =(WindowManager) Getsystemservice (Window_service); Super. OnCreate (); }        classMyphonestatelistenerextendsphonestatelistener{//3, Manual override, the method that triggers when the phone status changes@Override Public voidOncallstatechanged (intState , String Incomingnumber) {            Switch(state) { CaseTelephonymanager.call_state_idle://idle State, no activity (removal of toast)LOG.I (Tag, "Hang up the phone, it's free ...... ..."...)); //when you hang up the phone, the form needs to remove the toast.                if(mwm!=NULL&& mviewtoast!=NULL) {Mwm.removeview (mviewtoast); }                 Break;  CaseTelephonymanager.call_state_offhook://off-hook status, at least one phone activity. The event either calls (dialing) or calls                 Break;  Casetelephonymanager.call_state_ringing://Bell (show toast)LOG.I (Tag, "ring up ... ...".........));                Showtoast (Incomingnumber);  Break; }            Super. oncallstatechanged (State, Incomingnumber); }} @Override Publicibinder onbind (Intent arg0) {return NULL; }         Public voidshowtoast (String incomingnumber) {FinalWindowmanager.layoutparams params =Mparams; Params.height=WindowManager.LayoutParams.WRAP_CONTENT; Params.width=WindowManager.LayoutParams.WRAP_CONTENT; Params.flags=WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE//                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE can be touched by default|WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; Params.format=pixelformat.translucent; //Show toast at the time of the bell, consistent with the phone typeParams.type =WindowManager.LayoutParams.TYPE_PHONE; Params.settitle ("Toast"); //Specify the location of the toast (Specify the toast in the upper left corner)Params.gravity = gravity.left+Gravity.top; //Toast Display effect (toast Layout file), Xml-->view (toast), hang the toast on the WindowManager formMviewtoast = View.inflate ( This, R.layout.toast_view,NULL); Tv_toast=(TextView) Mviewtoast.findviewbyid (r.id.tv_toast); //gets the index of the color value text from the SP, matches the picture, and serves as the displayMdrawableids =New int[]{r.drawable.call_locate_white, R.drawable.call_locate_orange, r.drawab        Le.call_locate_blue, R.drawable.call_locate_gray, r.drawable.call_locate_green}; intToaststyleindex = Sputil.getint (Getapplicationcontext (), Constantvalue.toast_style, 0);                Tv_toast.setbackgroundresource (Mdrawableids[toaststyleindex]); //hanging on a form in a view (permission)Mwm.addview (mviewtoast, params); //after obtaining the call number, we need to make a call number queryquery (Incomingnumber); }        Private voidQueryFinalString Incomingnumber) {        NewThread () { Public voidrun () {maddress=addressdao.getaddress (Incomingnumber); Mhandler.sendemptymessage (0);        };    }.start (); } @Override Public voidOnDestroy () {//Cancel monitoring of the phone status (the object that listens for calls when the service is turned on)        if(mtm!=NULL&& mphonestatelistener!=NULL) {Mtm.listen (Mphonestatelistener, Phonestatelistener.listen_none); }        Super. OnDestroy (); }}

Monitoring of phone status in the service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.