自定義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; } }