android项目手机卫士来电显示号码归属地
昨日实现了360手机卫士的来电显示归属地的功能,具体的功能就是当来电的时候,显示当前号码的归属地,学习之后发现操作
非常的简单,具体实现代码如下:
addressservice.java
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>
获取到settingitemview对象,我们自定义的控件,设置状态
调用settingitemview对象的setonclicklistener()方法,设置点击事件,重写onclick方法
调用settingitemview对象的ischecked()方法,得到当前是否选中
判断状态,调用settingitemview对象的setchecked()方法,设置状态,参数:布尔值
调用startservice()方法,开启监听手机状态的服务,参数:intent对象,
调用stopservice()方法,关闭服务
判断当前服务是否开启,设置控件的默认选中状态
新建一个工具类servicesutils.java
定义一个静态方法isservicerunning(),传入参数:context上下文,string服务名
调用context对象的getsystemservice()方法,获取activitymanager对象,参数:context.activity_service
调用activitymanager对象的getrunningservices()方法,得到运行的服务list集合,参数:int最大值
for循环list集合,每条是个runningserviceinfo对象
调用runningserviceinfo.servie.getclassname(),获取到string服务类名,判断一下如果相等返回true
settingactivity.java
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(); } }); } }
servicesutils.java
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; } }
设置效果如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 关于java I/O流的总结之字节流