博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[android] 手机卫士自定义吐司
阅读量:7117 次
发布时间:2019-06-28

本文共 4010 字,大约阅读时间需要 13 分钟。

继续在之前监听来电的服务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,一个TextVIewTextView定义id

在自定义吐司的方法中

调用View.inflate()方法,获取View对象,参数:上下文,资源文件,null

添加这个View就可以了

 

package com.qingguow.mobilesafe.service;import android.app.Service;import android.content.Intent;import android.content.IntentFilter;import android.graphics.Color;import android.os.IBinder;import android.telephony.PhoneStateListener;import android.telephony.TelephonyManager;import android.view.WindowManager;import android.view.WindowManager.LayoutParams;import android.widget.TextView;import com.qingguow.mobilesafe.receiver.OutcallReceiver;import com.qingguow.mobilesafe.utils.NumberQueryAddressUtil;/** * 来电显示 *  * @author taoshihan *  */public class AddressService extends Service {    private TelephonyManager tm;    private MyPhoneStateListener phoneStateListener;    private OutcallReceiver outcallReceiver;    private WindowManager wm;    private TextView view;    @Override    public IBinder onBind(Intent arg0) {        // TODO Auto-generated method stub        return null;    }    /**     * 服务创建     */    @Override    public void onCreate() {        super.onCreate();        tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);        phoneStateListener = new MyPhoneStateListener();        tm.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);                //注册广播        outcallReceiver=new OutcallReceiver();        IntentFilter filter=new IntentFilter();        filter.addAction("android.intent.action.NEW_OUTGOING_CALL");        registerReceiver(outcallReceiver, filter);    }    private class MyPhoneStateListener extends PhoneStateListener {        @Override        public void onCallStateChanged(int state, String incomingNumber) {            super.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!=null){                    wm.removeView(view);                }                break;            default:                break;            }        }    }    /**     * 服务销毁     */    @Override    public void onDestroy() {        // TODO Auto-generated method stub        super.onDestroy();        //取消监听        tm.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);        phoneStateListener=null;        //解除注册广播        unregisterReceiver(outcallReceiver);        outcallReceiver=null;    }    /**     * 自定义吐司     * @param info     */    public void myToast(String info) {        wm=(WindowManager) getSystemService(WINDOW_SERVICE);        view=new TextView(getApplicationContext());        view.setText(info);        view.setTextColor(Color.GREEN);        view.setTextSize(18);                LayoutParams params = new 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);    }}

 

转载地址:http://kmfel.baihongyu.com/

你可能感兴趣的文章
puppet确保程序运行
查看>>
Java spring boot 2.0连接mysql异常:The server time zone value 'Öйú±ê×¼Ê...
查看>>
Python爬虫入门教程 7-100 蜂鸟网图片爬取之二
查看>>
使用expo在安卓模拟器中运行React Native程序
查看>>
码农西游 | 写一本技术书可以赚多少钱
查看>>
最详细的Vue Hello World应用开发步骤
查看>>
标签打印软件如何制作三角形合格证
查看>>
阿里云Web应用防火墙知识,了解阿里云Web应用防火墙
查看>>
通过iotop与performance_schema.threads查看mysql的IO使用情况
查看>>
struts
查看>>
Spring Cloud服务短路Netflix Hystrix
查看>>
mysql5.7 修改默认密码
查看>>
把.Net开发环境迁移到Linux上去
查看>>
Android BottomNavigationBar底部导航控制器的使用
查看>>
Kubernetes Dashboard on Ubuntu 16.04安装记录
查看>>
网站验收新标准:所有颜色均可以在可视化后台修改
查看>>
hello blog
查看>>
第6章 编程范式 《丰富多彩的编程世界》
查看>>
2.1命令行和JSON的配置「深入浅出ASP.NET Core系列」
查看>>
软件研发成本估算:成本的构成及含义?如何计算?
查看>>