102.s1-使用WindowManager在窗口上显示view

WindowManager是Android中一个重要的服务(Service )。WindowManager Service 是全局的,是唯一的。它将用户的操作,翻译成为指令,发送给呈现在界面上的各个Window。Activity会将顶级的控件注册到 Window Manager 中,
当用户真是触碰屏幕或键盘的时候,Window Manager就会通知到,而当控件有一些请求产生,也会经由ViewParent送回到Window Manager中。从而完成整个通信流程。
整个Android的窗口机制是基于一个叫做 WindowManager,这个接口可以添加view到屏幕,也可以从屏幕删除view。它面向的对象一端是屏幕,另一端就是View,通过WindowManager的 addView方法创建View,这样产生出来的View根据
WindowManager.LayoutParams属性不同,效果也就不同了。比如创建 系统顶级窗口,实现悬浮窗口效果!WindowManager的方法很简单,基本用到的就三addView,removeView,updateViewLayout。

 

使用自定义的Toast显示窗体,让号码归属地自定义的显示,不用Toast显示,窗体随着电话拨号出现,挂断电话就消失。

AddressService.java

 

package com.ldw.safe.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.ldw.safe.db.dao.AddressDao;

/*
 * 来电提醒的服务
 */
public class AddressService extends Service {

	private TelephonyManager tm;
	private MyListener listener;
	private OutCallReceiver receiver;
	private WindowManager mWM;
	private TextView view;

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void onCreate(){
		super.onCreate();
		
		tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
		listener = new MyListener();
		tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);//监听打电话的状态
		
		receiver = new OutCallReceiver();
		IntentFilter filter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);
		registerReceiver(receiver, filter);//动态注册广播
	}
	
	class MyListener extends PhoneStateListener{
		//监听电话状态的变化
		@Override
		public void onCallStateChanged(int state, String incomingNumber){
			//
			switch(state){
			//电话铃声响起
			case TelephonyManager.CALL_STATE_RINGING:
				System.out.println("CALL_STATE_RINGING");
				//获取来电的号码归属地
				String address = AddressDao.getAddress(incomingNumber);
				//Toast的形式弹出号码归属地
				//Toast.makeText(AddressService.this, address, Toast.LENGTH_LONG).show();
				//自定义的Toast显示号码归属地
				showToast(address);
				break;
			//监听电话闲置的事件
			case TelephonyManager.CALL_STATE_IDLE:
				//关闭自定义的Toast显示
				if(mWM != null && view != null){
					//挂断电话的时候,移除窗体
					mWM.removeView(view);
				}
				break;
			default:
				break;
			}
			super.onCallStateChanged(state, incomingNumber);
		}
	}
	
	/*
	 * 监听去电 的广播接收者,添加权限PROCESS_OUTGOING_CALLS
	 */
	class OutCallReceiver extends BroadcastReceiver{

		@Override
		public void onReceive(Context context, Intent intent) {
			//获取到来电的号码
			String number = getResultData();
			//获取到号码的归属地
			String address = AddressDao.getAddress(number);
			//Toast的形式弹出号码归属地
			//Toast.makeText(context, address, Toast.LENGTH_LONG).show();
			//自定义的Toast显示号码归属地
			showToast(address);
			
		}
		
	}
	
    @Override
    public void onDestroy(){
    	super.onDestroy();
    	//关闭服务,停止来电监听
    	tm.listen(listener, PhoneStateListener.LISTEN_NONE);
    	
    	//注销广播
    	unregisterReceiver(receiver);
    }
    
    /*
     * 自定义归属地显示浮窗,取代Toast
     */
    private void showToast(String text){
    	mWM = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
    	
    	final WindowManager.LayoutParams mParams = new WindowManager.LayoutParams();
        final WindowManager.LayoutParams params = mParams;
        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.format = PixelFormat.TRANSLUCENT;
        params.type = WindowManager.LayoutParams.TYPE_TOAST;
        params.setTitle("Toast");
        
        view = new TextView(this);
        view.setText(text);
        view.setTextColor(Color.BLUE);
        //将view添加到窗体
        mWM.addView(view, params);
    }

}

 

 

 

你可能感兴趣的:(android整理)