自定义Toast中用到了windowManager这个类
一下为简单介绍:
应用程序与窗口管理器的接口。WindowManager是Android中一个重要的服务。WindowManager Service是全局唯一的。它将用户的操作,翻译成指令,发送呈现在界面上的各个window。Activity会将顶级控件注册到Window Manager中,当用户触碰屏幕或键盘时,Window Manager就会通知到,而当控件有一些请求产生,也会经由ViewParent送回到Window Manager中,从而完成整个通信流程。
整个Android的窗口机制就是基于一个叫做WindowManager,这个接口可添加View到屏幕,也可从屏幕移除View。它面向的对象一端是屏幕,另一端就是View,通过WindowManager的addView()创建view,这样产生的view根据WindowManager.LayoutParams属性不同,效果也就不同了。比如创建系统顶级窗口,实现悬浮窗口效果。注意显示出来就一定要销毁掉(remove())。
三、获取其实例的方法
1、WindowManager wm=(WindowManager) getSystemService(Context.WINDOW_SERVICE);
2、WindowManager wm=(WindowManager) getWindowManger();
下面直接贴代码
/**
* 自定义土司
*
* @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;
}
}