星空网 > 软件开发 > 操作系统

[android] 手机卫士自定义吐司

继续在之前监听来电的服务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.

线性布局,横向排列,一个ImageView,一个TextVIew,TextView定义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);  }}

 




原标题:[android] 手机卫士自定义吐司

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

tiktok安卓免费下载:https://www.goluckyvip.com/tag/83695.html
tiktok俄罗斯版下载:https://www.goluckyvip.com/tag/83696.html
tiktok国际下载安卓:https://www.goluckyvip.com/tag/83697.html
tiktok国际版下载最新:https://www.goluckyvip.com/tag/83698.html
tiktok安卓全球破解版下载:https://www.goluckyvip.com/tag/83699.html
theft:https://www.goluckyvip.com/tag/837.html
加拿大本地账户开立是否需要加拿大身份? :https://www.kjdsnews.com/a/1842166.html
加拿大本地账户开立是否需要加拿大身份? :https://www.xlkjsw.com/news/92266.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流