package com.heima.mobile;
import com.example.mobile.R;
import com.heima.Dao.LocationDao;
import com.heima.Utils.SharedprefrencesUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
public class Service_QueryLocation extends Service{
@ViewInject(R.id.et_number)
private EditText et_number;
@ViewInject(R.id.tv_address)
private TextView tv_address;
private final WindowManager.LayoutParams mParams = new WindowManager.LayoutParams();
private WindowManager wm;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
//获取到通话服务管理器
TelephonyManager tm=(TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
//电话状态监听器
MyPhoneStateListener lister=new MyPhoneStateListener();
//为电话管理器设置监听器,用于监听电话拨打状态
tm.listen(lister,PhoneStateListener.LISTEN_CALL_STATE);
//获取窗体服务
wm=(WindowManager) this.getSystemService(WINDOW_SERVICE);
}
//归属地的风格背景图
private int[] items = { R.drawable.call_locate_green,
R.drawable.call_locate_gray, R.drawable.call_locate_orange };
public class MyPhoneStateListener extends PhoneStateListener {
//电话接听状态改变时,有3种状态:闲置、铃响、挂断
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
View view = null;
switch(state)
{
//闲置
case TelephonyManager.CALL_STATE_IDLE:
if(view!=null)
{
wm.removeView(view);
}
break;
//铃声响起
case TelephonyManager.CALL_STATE_RINGING:
//获取号码归属地
String Location=LocationDao.getLocation(incomingNumber);
//填充视图
view=View.inflate(Service_QueryLocation.this,R.layout.show_toast,null);
int su=SharedprefrencesUtils.SetShareprefrences(Service_QueryLocation.this,"mobile","location", 0);
//为悬浮框填充背景
view.setBackgroundResource(items[su]);
TextView tv_address=(TextView) view.findViewById(R.id.tv_address);
tv_address.setText(Location);
//吐司的参数
final WindowManager.LayoutParams params = mParams;
//设置吐司的位置,在左上角
params.gravity = Gravity.LEFT | Gravity.TOP;
//当手指离开屏幕时候存的值
mParams.x =SharedprefrencesUtils.SetShareprefrences(Service_QueryLocation.this,"mobile","lastx", 0);
mParams.y=SharedprefrencesUtils.SetShareprefrences(Service_QueryLocation.this,"mobile","lasty", 0);
//设置Toast控件的宽和高
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
|WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
params.format = PixelFormat.TRANSLUCENT;
params.type = WindowManager.LayoutParams.TYPE_PRIORITY_PHONE;
wm.addView(view, params);
//设置土司的触摸时间
view.setOnTouchListener(new OnTouchListener() {
private int startX;
private int startY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction())
{
//当手指按下的时候
case MotionEvent.ACTION_DOWN:
// 首先获取到我手指触摸到屏幕的x轴和y轴
// 得到按下x轴的位置
startX = (int) event.getRawX();
// 得到按下y轴的位置
startY = (int) event.getRawY();
System.out.println("x轴的位置:" + startX + "y轴的位置:"
+ startY);
break;
break;
//当手指移动的时候
case MotionEvent.ACTION_MOVE:
break;
//当手指离开屏幕的时候
case MotionEvent.ACTION_UP:
// 获取到当前的x轴和y轴的值
int newX = (int) event.getRawX();
int newY = (int) event.getRawY();
// 当前的x轴减去开始的x轴。当前的y轴减去开始的y轴。得到之间差值
int dx = (int) (newX - startX);
int dy = (int) (newY - startY);
mParams.x += dx;
mParams.y += dy;
//避免让吐司跑到外面去
if (params.x < 0) {
params.x = 0;
}
if (params.y < 0) {
params.y = 0;
}
// 当当前的view大于屏幕的宽度的时候。然后把view的宽度设置成屏幕的最大值
if (params.x > (wm.getDefaultDisplay().getWidth() - view
.getWidth())) {
params.x = wm.getDefaultDisplay().getWidth()
- view.getWidth();
}
if (params.y > (wm.getDefaultDisplay().getHeight() - view
.getHeight())) {
params.y = wm.getDefaultDisplay().getHeight()
- view.getHeight();
}
System.out.println("当前x轴的位置:" + newX + "当前y轴的位置:"
+ newY);
// 更新view的位置
wm.updateViewLayout(view, params);
startX = (int) event.getRawX();
startY = (int) event.getRawY();
break;
break;
}
return false;
}
});
break ;
//电话被挂断
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
}
}
}
}