package com.qingguow.mobilesafe.service;
import com.qingguow.mobilesafe.utils.NumberQueryAddressUtil;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;
/**
* 来电显示
*
* @author taoshihan
*
*/
public class AddressService extends Service {
private TelephonyManager tm;
private MyPhoneStateListener phoneStateListener;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
/**
* 服务创建
*/
@Override
public void onCreate() {
super.onCreate();
tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
phoneStateListener = new MyPhoneStateListener();
tm.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
private class MyPhoneStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
String info = NumberQueryAddressUtil
.queryAddress(incomingNumber);
Toast.makeText(getApplicationContext(), info, 1).show();
break;
default:
break;
}
}
}
/**
* 服务销毁
*/
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
//取消监听
tm.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
phoneStateListener=null;
}
}
<com.qingguow.mobilesafe.ui.SettingItemView
tsh:title="设置显示号码归属地"
tsh:desc_on="设置显示号码归属地已开启"
tsh:desc_off="设置显示号码归属地已关闭"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/siv_show_address">
</com.qingguow.mobilesafe.ui.SettingItemView>
package com.qingguow.mobilesafe;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import com.qingguow.mobilesafe.service.AddressService;
import com.qingguow.mobilesafe.ui.SettingItemView;
import com.qingguow.mobilesafe.utils.ServiceUtils;
public class SettingActivity extends Activity {
private SettingItemView siv_item;
private SharedPreferences sp;
// 设置是否开启号码归属地
private SettingItemView showAddressBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
// 设置号码归属地
showAddressBtn = (SettingItemView) findViewById(R.id.siv_show_address);
if (ServiceUtils.isRunningService(this,
"com.qingguow.mobilesafe.service.AddressService")) {
showAddressBtn.setChecked(true);
} else {
showAddressBtn.setChecked(false);
}
showAddressBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (showAddressBtn.isChecked()) {
showAddressBtn.setChecked(false);
stopService(new Intent(getApplicationContext(),
AddressService.class));
} else {
showAddressBtn.setChecked(true);
startService(new Intent(getApplicationContext(),
AddressService.class));
}
}
});
siv_item = (SettingItemView) findViewById(R.id.siv_item);
sp = getSharedPreferences("config", MODE_PRIVATE);
// 根据保存的数据设置状态
boolean update = sp.getBoolean("update", false);
if (update) {
siv_item.setChecked(true);
} else {
siv_item.setChecked(false);
}
// 自动更新的点击事件
siv_item.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Editor editor = sp.edit();
if (siv_item.isChecked()) {
// 设置不选中
siv_item.setChecked(false);
editor.putBoolean("update", false);
} else {
// 设置选中
siv_item.setChecked(true);
editor.putBoolean("update", true);
}
editor.commit();
}
});
}
}
package com.qingguow.mobilesafe.utils;
import java.util.List;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
/**
* 服务工具类
* @author taoshihan
*
*/
public class ServiceUtils {
/**
* 判断某服务是否开启
* @param context
* @param serviceName
* @return
*/
public static boolean isRunningService(Context context,String serviceName){
ActivityManager am=(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningServiceInfo> infos=am.getRunningServices(100);
for(RunningServiceInfo info:infos){
String name=info.service.getClassName();
if(name.equals(serviceName)){
return true;
}
}
return false;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有