新建包com.example.mobilesafe.service,在该包下创建服务ShowCallLocationService,继承自service。
package com.example.mobilesafe.service;
import android.app.ActionBar;
import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.net.NetworkInfo;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import com.example.mobilesafe.R;
import com.example.mobilesafe.db.NumberAddressDao;
/**
* Created by sing on 14-1-13.
* desc:监听电话呼入的服务
*/
public class ShowCallLocationService extends Service {
//电话管理器
private TelephonyManager tm;
//窗体管理器
private WindowManager windowManager;
//电话状态改变的监听器
private MyPhoneListener listener;
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
listener = new MyPhoneListener();
tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
}
private class MyPhoneListener extends PhoneStateListener {
private View view;
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
//电话铃声正在响
case TelephonyManager.CALL_STATE_RINGING:
//查询出电话号码的归属地
NumberAddressDao numberAddressDao = new NumberAddressDao(getApplicationContext());
String address = numberAddressDao.getAddress(incomingNumber);
//通过布局填充器将布局转换为view
view = View.inflate(getApplicationContext(), R.layout.show_address, null);
//LinearLayout ll = (LinearLayout) view.findViewById(R.id.ll_show_address);
TextView tv = (TextView) view.findViewById(R.id.tv_show_address);
tv.setText(address);
final 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;
//显示在窗体上的style为半透明
params.format = PixelFormat.TRANSLUCENT;
//窗体view的类型为吐司
params.type = WindowManager.LayoutParams.TYPE_TOAST;
windowManager.addView(view, params);
break;
//电话空闲状态
case TelephonyManager.CALL_STATE_IDLE:
//将窗体上的吐司移除
if (view != null) {
windowManager.removeView(view);
view = null;
}
break;
//电话接通状态
case TelephonyManager.CALL_STATE_OFFHOOK:
//将窗体上的吐司移除
if (view != null) {
windowManager.removeView(view);
view = null;
}
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}
}
其中显示电话号码归属地的布局文件show_address.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:gravity="center_vertical"
android:id="@+id/ll_show_address" android:background="@drawable/call_locate_white">
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/notification"/>
<TextView style="@style/content_text"
android:id="@+id/tv_show_address"
android:textColor="@android:color/white"
android:text="归属地"/>
</LinearLayout>