自定义Toast中用到了windowManager这个类android
一下为简单介绍:数据库
应用程序与窗口管理器的接口。WindowManager是Android中一个重要的服务。WindowManager Service是全局惟一的。它将用户的操做,翻译成指令,发送呈如今界面上的各个window。Activity会将顶级控件注册到Window Manager中,当用户触碰屏幕或键盘时,Window Manager就会通知到,而当控件有一些请求产生,也会经由ViewParent送回到Window Manager中,从而完成整个通讯流程。app
整个Android的窗口机制就是基于一个叫作WindowManager,这个接口可添加View到屏幕,也可从屏幕移除View。它面向的对象一端是屏幕,另外一端就是View,经过WindowManager的addView()建立view,这样产生的view根据WindowManager.LayoutParams属性不一样,效果也就不一样了。好比建立系统顶级窗口,实现悬浮窗口效果。注意显示出来就必定要销毁掉(remove())。ide
一、WindowManager wm=(WindowManager) getSystemService(Context.WINDOW_SERVICE);布局
二、WindowManager wm=(WindowManager) getWindowManger();this
下面直接贴代码spa
/** * 自定义土司 * * @param address */ public void myToast(String address) { /*绘制UI界面*/ view = View.inflate(this, R.layout.address_show, null); TextView textview = (TextView) view.findViewById(R.id.tv_address); //"半透明","活力橙","卫士蓝","金属灰","苹果绿" int[] ids = {R.mipmap.call_locate_white9, R.mipmap.call_locate_orange9, R.mipmap.call_locate_blue9 , R.mipmap.call_locate_gray9, R.mipmap.call_locate_green9}; /*得到用户设置的风格,默认为0半透明*/ SharedPreferences sharedPreferences = getSharedPreferences("config", MODE_PRIVATE); view.setBackgroundResource(ids[sharedPreferences.getInt("which", 0)]); textview.setText(address); //窗体的参数就设置好了 WindowManager.LayoutParams params = new WindowManager.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.format = PixelFormat.TRANSLUCENT; params.type = WindowManager.LayoutParams.TYPE_TOAST; windowManager.addView(view, params); }
Toast布局文件以下
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_vertical" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@android:drawable/ic_menu_call" /> <TextView android:textColor="#000000" android:id="@+id/tv_address" android:textSize="22sp" android:text="号码归属地" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
调用代码以下
package com.zaizai.safty.service; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.graphics.PixelFormat; import android.os.IBinder; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.view.View; import android.view.WindowManager; import android.widget.TextView; import com.zaizai.safty.R; import com.zaizai.safty.db.dao.NumberAddressQueryUtils; public class AddressService extends Service { //窗体管理者 private WindowManager windowManager; private View view; /** * 电话 */ private TelephonyManager telephonyManager; private MyListenerPhone myListenerPhone; private OutCallReceiver outCallReceiver; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } // 服务里面的内部类 //广播接收者的生命周期和服务同样 class OutCallReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // 这就是咱们拿到的播出去的电话号码 String phone = getResultData(); // 查询数据库 String address = NumberAddressQueryUtils.queryNumber(phone); // Toast.makeText(context, address, Toast.LENGTH_LONG).show(); myToast(address); } } private class MyListenerPhone extends PhoneStateListener { @Override public void onCallStateChanged(int state, String incomingNumber) { // state:状态,incomingNumber:来电号码 super.onCallStateChanged(state, incomingNumber); switch (state) { case TelephonyManager.CALL_STATE_RINGING:// 来电铃声响起 // 查询数据库的操做 String address = NumberAddressQueryUtils .queryNumber(incomingNumber); //Toast.makeText(getApplicationContext(), address, Toast.LENGTH_LONG).show(); myToast(address); break; case TelephonyManager.CALL_STATE_IDLE://电话的空闲状态:挂电话、来电拒绝 //把这个View移除 if (view != null) { windowManager.removeView(view); } break; default: break; } } } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); //电话服务 telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); // 监听来电 myListenerPhone = new MyListenerPhone(); //注册服务去监听电话 telephonyManager.listen(myListenerPhone, PhoneStateListener.LISTEN_CALL_STATE); //用代码去注册广播接收者Begin outCallReceiver = new OutCallReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction("android.intent.action.NEW_OUTGOING_CALL"); registerReceiver(outCallReceiver, filter); //用代码去注册广播接收者End //实例化窗体 windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); } /** * 自定义土司 * * @param address */ public void myToast(String address) { /*绘制UI界面*/ view = View.inflate(this, R.layout.address_show, null); TextView textview = (TextView) view.findViewById(R.id.tv_address); //"半透明","活力橙","卫士蓝","金属灰","苹果绿" int[] ids = {R.mipmap.call_locate_white9, R.mipmap.call_locate_orange9, R.mipmap.call_locate_blue9 , R.mipmap.call_locate_gray9, R.mipmap.call_locate_green9}; /*得到用户设置的风格,默认为0半透明*/ SharedPreferences sharedPreferences = getSharedPreferences("config", MODE_PRIVATE); view.setBackgroundResource(ids[sharedPreferences.getInt("which", 0)]); textview.setText(address); //窗体的参数就设置好了 WindowManager.LayoutParams params = new WindowManager.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.format = PixelFormat.TRANSLUCENT; params.type = WindowManager.LayoutParams.TYPE_TOAST; windowManager.addView(view, params); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); // 取消监听来电 telephonyManager.listen(myListenerPhone, PhoneStateListener.LISTEN_NONE); myListenerPhone = null; //用代码取消注册广播接收者 unregisterReceiver(outCallReceiver); outCallReceiver = null; } }