点击会开启服务。
sivAddress.setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
if (sivAddress.isChecked()) {
sivAddress.setChecked(false);
stopService(new Intent(SettingActivity.
this,
//只写this则表示OnClickListener,所以要写SettingActivity.this,
AddressService.
class));
// 停止归属地服务
}
else {
sivAddress.setChecked(true);
startService(new Intent(SettingActivity.
this,
AddressService.class));
// 开启归属地服务,就会调用AddressService的onCreate方法监听电话的状态。
}
}
});
检测服务是否正在运行:
package com.itheima52.mobilesafe.utils;
import java.util.List;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
/**
* 服务状态工具类
*
* // 根据归属地服务是否运行来更新checkbox
boolean serviceRunning = ServiceStatusUtils.isServiceRunning(this,
"com.itheima52.mobilesafe.service.AddressService");//Service的包名类名
*/
public class ServiceStatusUtils {
/**
* 检测服务是否正在运行
*/
public static boolean isServiceRunning(Context ctx, String serviceName) {
ActivityManager am =
(ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
List runningServices = am.getRunningServices(100);// 获取系统所有正在运行的服务,最多返回100个
for (RunningServiceInfo runningServiceInfo : runningServices) {
String className = runningServiceInfo.service.getClassName();// 获取服务的名称
// System.out.println(className);
if (className.equals(serviceName)) {// 服务存在
return true;
}
}
return false;
}
}
自定义组合控件:
package com.itheima52.mobilesafe.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.itheima52.mobilesafe.R;
/**
* 设置中心的自定义组合控件
*
*
private void initAddressStyle() {
scvAddressStyle = (SettingClickView) findViewById(R.id.scv_address_style);
scvAddressStyle.setTitle("归属地提示框风格");
int style = mPref.getInt("address_style", 0);// 读取保存的style
scvAddressStyle.setDesc(items[style]);
scvAddressStyle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showSingleChooseDailog();
}
});
}
*/
public class SettingClickView extends RelativeLayout {
private TextView tvTitle;
private TextView tvDesc;
public SettingClickView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView();
}
public SettingClickView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public SettingClickView(Context context) {
super(context);
initView();
}
/**
* 初始化布局
*/
private void initView() {
// 将自定义好的布局文件设置给当前的SettingClickView
View.inflate(getContext(), R.layout.view_setting_click, this);
tvTitle = (TextView) findViewById(R.id.tv_title);
tvDesc = (TextView) findViewById(R.id.tv_desc);
}
public void setTitle(String title) {
tvTitle.setText(title);
}
public void setDesc(String desc) {
tvDesc.setText(desc);
}
}
来去电归属地显示,自定义弹框:
package com.itheima52.mobilesafe.service;
import com.itheima52.mobilesafe.R;
import com.itheima52.mobilesafe.db.dao.AddressDao;
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.Color;
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 android.widget.Toast;
/**
* 来电提醒的服务
* 清单文件注册:
*
*/
public class AddressService extends Service {
private TelephonyManager tm;
private MyListener listener;
private OutCallReceiver receiver;
private WindowManager mWM;
private View view;
private SharedPreferences mPref;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
mPref = getSharedPreferences("config", MODE_PRIVATE);
//来电是通过TelephonyManager服务来监听,去电是通过广播接受者来接收,这是安卓的api决定的。
tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
listener = new MyListener();
tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);// 监听来电的状态,
receiver = new OutCallReceiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);
registerReceiver(receiver, filter);// 动态注册广播
}
class MyListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:// 电话铃声响了
System.out.println("电话铃响...");
String address = AddressDao.getAddress(incomingNumber);// 根据来电号码查询归属地
// Toast.makeText(AddressService.this, address,
// Toast.LENGTH_LONG)
// .show();
showToast(address);
break;
case TelephonyManager.CALL_STATE_IDLE:// 电话闲置状态,挂断电话的时候消除showToast()方法产生的WindowManager窗口。
if (mWM != null && view != null) {
mWM.removeView(view);// 从window中移除view,否则这个窗口会一直显示在手机屏幕。
view = null;
}
break;
default:
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}
/**
* 监听去电的广播接受者 需要权限: android.permission.PROCESS_OUTGOING_CALLS
*/
class OutCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String number = getResultData();// 获取去电电话号码
String address = AddressDao.getAddress(number);
Toast.makeText(context, address, Toast.LENGTH_LONG).show();
showToast(address);
}
}
@Override
public void onDestroy() {
super.onDestroy();//停掉本AddressService服务,但是他里面的tm服务监听没有停止,
tm.listen(listener, PhoneStateListener.LISTEN_NONE);// 停止来电监听
unregisterReceiver(receiver);// 注销去电广播
}
/**
* 自定义归属地浮窗
*/
private void showToast(String text) {
mWM = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);//系统服务,WindowManager可以一直显示在手机屏幕上。
//模仿的Toast源码
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;
params.setTitle("Toast");
// view = new TextView(this);
view = View.inflate(this, R.layout.toast_address, null);
/*
http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/call_locate_blue"
android:gravity="center_vertical"
android:orientation="horizontal" >
安卓的图片
*/
int[] bgs = new int[] { R.drawable.call_locate_white,
R.drawable.call_locate_orange, R.drawable.call_locate_blue,
R.drawable.call_locate_gray, R.drawable.call_locate_green };
int style = mPref.getInt("address_style", 0);
view.setBackgroundResource(bgs[style]);// 根据存储的样式更新背景
TextView tvText = (TextView) view.findViewById(R.id.tv_number);
tvText.setText(text);
mWM.addView(view, params);// 将view添加在屏幕上(Window,所有的控件都显示在window上,在windoew上面可以显示Activity和fragment,)
}
}