码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2016-05-08 19:50:37      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

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

 

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

标签:

原文地址:http://www.cnblogs.com/taoshihan/p/5471162.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!