注意:
/*toast窗体类型默认不响应窗体点击事件
params.type = WindowManager.LayoutParams.TYPE_TOAST;*/
/*制定具有电话优先级的窗体类型,需要添加权限 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>*/
params.type = WindowManager.LayoutParams.TYPE_PRIORITY_PHONE;
代码如下:
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.os.SystemClock;import android.telephony.PhoneStateListener;import android.telephony.TelephonyManager;import android.util.Log;import android.view.Gravity;import android.view.MotionEvent;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 static final String TAG = "zaizai"; //窗体管理者 private WindowManager windowManager; private View view; /** * 电话 */ private TelephonyManager telephonyManager; private MyListenerPhone myListenerPhone; private OutCallReceiver outCallReceiver; private WindowManager.LayoutParams params; private SharedPreferences sharedPreferences; @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 */ private long[] mHits = new long[2]; public void myToast(String address) { /*绘制UI界面*/ view = View.inflate(this, R.layout.address_show, null); TextView textview = (TextView) view.findViewById(R.id.tv_address); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { /*双击两次居中*/ System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1); mHits[mHits.length - 1] = SystemClock.uptimeMillis(); if (mHits[0] >= (SystemClock.uptimeMillis() - 500)) { /*成功双击两次*/ Log.i(TAG, "双击成功:"); params.x = windowManager.getDefaultDisplay().getWidth() / 2 - view.getWidth() / 2; params.y = windowManager.getDefaultDisplay().getHeight() / 2 - view.getHeight() / 2; windowManager.updateViewLayout(view, params); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt("lastx", params.x); editor.putInt("lasty", params.y); editor.commit(); } } }); view.setOnTouchListener(new View.OnTouchListener() { int startX; int startY; @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN://手指按下屏幕 { startX = (int) event.getRawX(); startY = (int) event.getRawY(); Log.i(TAG, "开始位置:" + startX + "," + startY); break; } case MotionEvent.ACTION_MOVE://手指在屏幕上移动 { int newX = (int) event.getRawX(); int newY = (int) event.getRawY(); Log.i(TAG, "移动后位置:" + newX + "," + newY); int dx = newX - startX; int dy = newY - startY; Log.i(TAG, "移动后的偏移量:" + dx + "," + dy); Log.i(TAG, "更新ImageView在窗体的偏移量:" + dx + "," + dy); /*重新设置窗体的开始位置,结束位置*/ params.x += dx; params.y += dy; if (params.x < 0) { params.x = 0; } else if (params.y < 0) { params.y = 0; } else if (params.x > (windowManager.getDefaultDisplay().getWidth() - view.getWidth())) { params.x = windowManager.getDefaultDisplay().getWidth() - view.getWidth(); } else if (params.y > (windowManager.getDefaultDisplay().getHeight() - view.getHeight())) { params.y = windowManager.getDefaultDisplay().getHeight() - view.getHeight(); } windowManager.updateViewLayout(view, params); // 重新初始化手指的开始结束位置。 startX = (int) event.getRawX(); startY = (int) event.getRawY(); break; } case MotionEvent.ACTION_UP://手指离开屏幕 { //记录控件距离屏幕左上角的坐标 SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt("lastx", params.x); editor.putInt("lasty", params.y); editor.commit(); break; } } return false;//事件处理完毕,返回ture,将事件拦截掉,不让负空间布局响应触摸事件 } }); //"半透明","活力橙","卫士蓝","金属灰","苹果绿" 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 = getSharedPreferences("config", MODE_PRIVATE); view.setBackgroundResource(ids[sharedPreferences.getInt("which", 0)]); textview.setText(address); //窗体的参数就设置好了 params = new WindowManager.LayoutParams(); params.height = WindowManager.LayoutParams.WRAP_CONTENT; params.width = WindowManager.LayoutParams.WRAP_CONTENT; /*吐司位置设置 params.gravity = Gravity.TOP; 多个参数使用+号连接*/ params.gravity = Gravity.TOP + Gravity.LEFT; /*左边100 顶部100 */ params.x = sharedPreferences.getInt("lastx", 0); params.y = sharedPreferences.getInt("lasty", 0); params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE /* | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE * 去掉响应时间*/ | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; params.format = PixelFormat.TRANSLUCENT; /*toast窗体类型默认不响应窗体点击事件 params.type = WindowManager.LayoutParams.TYPE_TOAST;*/ /*制定具有电话优先级的窗体类型,需要添加权限 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>*/ params.type = WindowManager.LayoutParams.TYPE_PRIORITY_PHONE; 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; } }
时间: 2024-10-25 06:08:44