[Android] Mobile defender Custom Toast

Source: Internet
Author: User

Continue in the service addressservice that previously monitored incoming calls , add Member Method mytoast ()

Get TextView object,new out, construct parameter: Context object

Call the SetText () method of the TextView object to set the text

Call the settextsize () method of the TextView Object , set the size, parameter:int

Call the SetTextColor () method of the TextView object to set the color

Get the windowmanager object, call the getsystemservice () method, Parameter:window_service

Call the AddView () method of the windowmanager object, Add the view, Parameters: View object, Windowmanager.layoutparams Object

Layoutparams Object Settings refer to the Show () method of the Toast class

At this point, the call and the phone will show the view, but it can not be eliminated

In the previous method of judging the call, the listening phone idle state, to remove the View

add telephonymanager.call_state_idle in switch judgment

Judge the TextView object not to be empty .

Call the Removeview () method of the windowmanager object , Parameter:theView Object (added above TextView)

Working with layout files

Create a new layout file address_toast.xml

Linear layout, horizontal arrangement, one ImageView, one TextVIew,TextVIew definition ID

In the custom Toast method

Call the view.inflate () method to get the View Object, Parameters: Context, resource file,null

Add this View to it.

 PackageCom.qingguow.mobilesafe.service;ImportAndroid.app.Service;Importandroid.content.Intent;ImportAndroid.content.IntentFilter;ImportAndroid.graphics.Color;ImportAndroid.os.IBinder;ImportAndroid.telephony.PhoneStateListener;ImportAndroid.telephony.TelephonyManager;ImportAndroid.view.WindowManager;ImportAndroid.view.WindowManager.LayoutParams;ImportAndroid.widget.TextView;ImportCom.qingguow.mobilesafe.receiver.OutcallReceiver;ImportCom.qingguow.mobilesafe.utils.NumberQueryAddressUtil;/*** Call Display * *@authorTaoshihan **/ Public classAddressserviceextendsService {PrivateTelephonymanager TM; PrivateMyphonestatelistener Phonestatelistener; PrivateOutcallreceiver Outcallreceiver; PrivateWindowManager WM; PrivateTextView View; @Override Publicibinder onbind (Intent arg0) {//TODO auto-generated Method Stub        return NULL; }    /*** Service Creation*/@Override Public voidonCreate () {Super. OnCreate (); TM=(Telephonymanager) Getsystemservice (Telephony_service); Phonestatelistener=NewMyphonestatelistener ();                Tm.listen (Phonestatelistener, phonestatelistener.listen_call_state); //Register a broadcastOutcallreceiver=NewOutcallreceiver (); Intentfilter Filter=NewIntentfilter (); Filter.addaction ("Android.intent.action.NEW_OUTGOING_CALL");    Registerreceiver (outcallreceiver, filter); }    Private classMyphonestatelistenerextendsPhonestatelistener {@Override Public voidOncallstatechanged (intState , String Incomingnumber) {            Super. oncallstatechanged (State, Incomingnumber); Switch(state) { CaseTelephonyManager.CALL_STATE_RINGING:String Info=numberqueryaddressutil. queryaddress (Incomingnumber); //Toast.maketext (Getapplicationcontext (), info, 1). Show (); //Custom ToastMytoast (info);  Break;  CaseTelephonymanager.call_state_idle://Idle State                if(view!=NULL) {Wm.removeview (view); }                 Break; default:                 Break; }        }    }    /*** Service Destruction*/@Override Public voidOnDestroy () {//TODO auto-generated Method Stub        Super. OnDestroy (); //Cancel MonitoringTm.listen (Phonestatelistener, Phonestatelistener.listen_none); Phonestatelistener=NULL; //To release a registered broadcastUnregisterreceiver (Outcallreceiver); Outcallreceiver=NULL; }    /*** Custom Toast *@paramInfo*/     Public voidmytoast (String info) {WM=(WindowManager) Getsystemservice (Window_service); View=NewTextView (Getapplicationcontext ());        View.settext (info);        View.settextcolor (Color.green); View.settextsize (18); Layoutparams params=NewLayoutparams (); Params.height=WindowManager.LayoutParams.WRAP_CONTENT; Params.width=WindowManager.LayoutParams.WRAP_CONTENT; Params.flags=WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE|WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; Params.type=WindowManager.LayoutParams.TYPE_TOAST;    Wm.addview (view, params); }}

[Android] Mobile defender Custom Toast

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.