最近給老大做個安卓手機的小程序,希望實現 來去電的時候,提示一下(主要是看看有沒有這個號碼的相關事情)。
在網上找了很多,都是只有來電的懸浮窗。
算了,還是自己研究吧。
下面給出監聽部分和懸浮窗,可拖拽的。
//來去電廣播
public class InterceptCall extends BroadcastReceiver {
private static final String TAG = "PhoneStatReceiver";
private WindowManager wm;
WindowManager.LayoutParams params;
private float mTouchStartX;
private float mTouchStartY;
private float x;
private float y;
private Button tv;
Date curDate = null;
Context mcontext;
private void updateViewPosition() {
// 更新浮動窗口位置參數
params.x = (int) (x - mTouchStartX);
params.y = (int) (y - mTouchStartY);
wm.updateViewLayout(tv, params);
}
@Override
public void onReceive(Context context, Intent intent) {
mcontext = context;
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
// 如果是去電(撥出)
// mCallEvent = CALL_EVENT_OUTGOING;
MyApplication.NumberType = 1;
String mCallNumber = intent
.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i(TAG, "call OUT:" + mCallNumber);
MyApplication.PhoneNum = mCallNumber;
wm = null;
wm = (WindowManager) context.getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE);
// WindowManager.LayoutParams
params = new WindowManager.LayoutParams();
params.type = LayoutParams.TYPE_PHONE;
// 設置Window flag
params.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
| LayoutParams.FLAG_NOT_FOCUSABLE;
params.gravity = Gravity.LEFT | Gravity.TOP; // 調整懸浮窗口至左上角 <span >為實現拖拽,必須加上這個,不然會漂移,不信你試試。</span>
// 以屏幕左上角為原點,設置x、y初始
params.x = 20;
params.y = 5;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.format = PixelFormat.RGBA_8888;
tv = new Button(context);
tv.setText("雙擊查看:\n" + mCallNumber + "的記錄");// +sds);
wm.addView(tv, params);
tv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// 獲取相對屏幕的坐標,即以屏幕左上角為原點
x = event.getRawX();
y = event.getRawY() - 25; // 25是系統狀態欄的高度
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 獲取相對View的坐標,即以此View左上角為原點
mTouchStartX = event.getX();
mTouchStartY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
updateViewPosition();
break;
// 判斷手勢點擊起來的時候:
case MotionEvent.ACTION_UP:
updateViewPosition();
mTouchStartX = mTouchStartY = 0;
if (curDate != null) {
Date dateNow = new Date(System.currentTimeMillis());
// date轉成毫秒
long diff = dateNow.getTime() - curDate.getTime();
// 這樣得到的差值是微秒級別
if (diff < 400) {
// 雙擊
try {
if (wm != null) {
wm.removeView(tv);
}
} catch (Exception e) {
}
curDate = null;
Intent service = new Intent();
service.setClass(mcontext, TeleLogContent.class);
service.putExtra("PhoneNumer",
MyApplication.PhoneNum);
service.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mcontext.startActivity(service);
} else {
// 單擊
curDate = new Date(System.currentTimeMillis());
}
// 這裡也是單擊
} else {
// 單擊
curDate = new Date(System.currentTimeMillis());
}
break;
}
return true;
}
});
// AskCallStateThread callStateThread = new
// AskCallStateThread(context);
// callStateThread.start();
} else {
String comeInNum = intent
.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
System.out.println("來電號碼:" + comeInNum);
TelephonyManager telM = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
// 設置一個監聽器
telM.listen(new TelListener(context),
PhoneStateListener.LISTEN_CALL_STATE);
}
}
}