继续在之前监听来电的服务AddressService里,添加成员方法MyToast()
获取TextVIEw对象,new出来,构造参数:上下文对象
调用TextVIEw对象的setText()方法,设置文本
调用TextVIEw对象的setTextSize()方法,设置大小,参数:int
调用TextVIEw对象的setTextcolor()方法,设置颜色
获取WindowManager对象,调用getSystemService()方法,参数:WINDOW_SERVICE
调用WindowManager对象的addVIEw()方法,添加进视图,参数:VIEw对象,WindowManager.LayoutParams对象
LayoutParams对象的设置参考Toast类的show()方法
此时,打电话和接电话会显示出来这个VIEw,但是消不掉了
在之前判断来电的方法里,进行监听电话空闲状态,去除这个VIEw
switch判断中添加TelephonyManager.CALL_STATE_IDLE
判空一下,判断TextVIEw对象不为空
调用WindowManager对象的 removeVIEw()方法,参数:VIEw对象(上面添加的TextVIEw)
使用布局文件
新建一个布局文件address_toast.xml
线性布局,横向排列,一个ImageVIEw,一个TextVIEw,TextVIEw定义ID
调用VIEw.inflate()方法,获取VIEw对象,参数:上下文,资源文件,null
添加这个VIEw就可以了
package com.qingguow.mobilesafe.service;import androID.app.Service; androID.content.Intent; androID.content.IntentFilter; androID.graphics.color; androID.os.IBinder; androID.telephony.PhonestateListener; androID.telephony.TelephonyManager; androID.vIEw.WindowManager; androID.vIEw.WindowManager.LayoutParams; androID.Widget.TextVIEw; com.qingguow.mobilesafe.receiver.Outcallreceiver; com.qingguow.mobilesafe.utils.NumberqueryAddressUtil;/** * 来电显示 * * @author taoshihan * */public class AddressService extends Service { private TelephonyManager tm; MyPhonestateListener phonestateListener; Outcallreceiver outcallreceiver; WindowManager wm; TextVIEw vIEw; @OverrIDe public IBinder onBind(Intent arg0) { // Todo auto-generated method stub return null; } * 服务创建 */ @OverrIDe voID onCreate() { super.onCreate(); tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); phonestateListener = new MyPhonestateListener(); tm.Listen(phonestateListener,PhonestateListener.ListEN_CALL_STATE); 注册广播 outcallreceiver= Outcallreceiver(); IntentFilter filter= IntentFilter(); filter.addAction("androID.intent.action.NEW_OUTGOING_CALL"); registerReceiver(outcallreceiver,filter); } private class MyPhonestateListener PhonestateListener { @OverrIDe voID onCallStateChanged(int state,String incomingNumber) { .onCallStateChanged(state,incomingNumber); switch (state) { case TelephonyManager.CALL_STATE_RINGING: String info = NumberqueryAddressUtil .queryAddress(incomingNumber); Toast.makeText(getApplicationContext(),info,1).show(); 自定义吐司 myToast(info); break; case TelephonyManager.CALL_STATE_IDLE:空闲状态 if(vIEw!=){ wm.removeVIEw(vIEw); } default: ; } } } * 服务销毁 onDestroy() { .onDestroy(); 取消监听 tm.Listen(phonestateListener,PhonestateListener.ListEN_NONE); phonestateListener=; 解除注册广播 unregisterReceiver(outcallreceiver); outcallreceiver= * 自定义吐司 * @param info */ myToast(String info) { wm=(WindowManager) getSystemService(WINDOW_SERVICE); vIEw= TextVIEw(getApplicationContext()); vIEw.setText(info); vIEw.setTextcolor(color.GREEN); vIEw.setTextSize(18); LayoutParams params = LayoutParams(); 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] 手机卫士自定义吐司全部内容,希望文章能够帮你解决[android] 手机卫士自定义吐司所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)