android項(xiàng)目手機(jī)衛(wèi)士來電顯示號碼歸屬地
昨日實(shí)現(xiàn)了360手機(jī)衛(wèi)士的來電顯示歸屬地的功能,具體的功能就是當(dāng)來電的時候,顯示當(dāng)前號碼的歸屬地,學(xué)習(xí)之后發(fā)現(xiàn)操作
非常的簡單,具體實(shí)現(xiàn)代碼如下:
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; } /** * 服務(wù)創(chuàng)建 */ @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; } } } /** * 服務(wù)銷毀 */ @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); //取消監(jiān)聽 tm.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE); phoneStateListener=null; } }
設(shè)置中心,配置是否開啟來電歸屬地顯示
直接使用我們之前定義好的組合控件
<com.qingguow.mobilesafe.ui.SettingItemView tsh:title="設(shè)置顯示號碼歸屬地" tsh:desc_on="設(shè)置顯示號碼歸屬地已開啟" tsh:desc_off="設(shè)置顯示號碼歸屬地已關(guān)閉" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/siv_show_address"> </com.qingguow.mobilesafe.ui.SettingItemView>
獲取到SettingItemView對象,我們自定義的控件,設(shè)置狀態(tài)
調(diào)用SettingItemView對象的setOnClickListener()方法,設(shè)置點(diǎn)擊事件,重寫onClick方法
調(diào)用SettingItemView對象的isChecked()方法,得到當(dāng)前是否選中
判斷狀態(tài),調(diào)用SettingItemView對象的setChecked()方法,設(shè)置狀態(tài),參數(shù):布爾值
調(diào)用startService()方法,開啟監(jiān)聽手機(jī)狀態(tài)的服務(wù),參數(shù):Intent對象,
調(diào)用stopService()方法,關(guān)閉服務(wù)
判斷當(dāng)前服務(wù)是否開啟,設(shè)置控件的默認(rèn)選中狀態(tài)
新建一個工具類ServicesUtils.java
定義一個靜態(tài)方法isServiceRunning(),傳入?yún)?shù):Context上下文,String服務(wù)名
調(diào)用Context對象的getSystemService()方法,獲取ActivityManager對象,參數(shù):Context.ACTIVITY_SERVICE
調(diào)用ActivityManager對象的getRunningServices()方法,得到運(yùn)行的服務(wù)List集合,參數(shù):int最大值
for循環(huán)List集合,每條是個RunningServiceInfo對象
調(diào)用RunningServiceInfo.servie.getClassName(),獲取到String服務(wù)類名,判斷一下如果相等返回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; // 設(shè)置是否開啟號碼歸屬地 private SettingItemView showAddressBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_setting); // 設(shè)置號碼歸屬地 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); // 根據(jù)保存的數(shù)據(jù)設(shè)置狀態(tài) boolean update = sp.getBoolean("update", false); if (update) { siv_item.setChecked(true); } else { siv_item.setChecked(false); } // 自動更新的點(diǎn)擊事件 siv_item.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Editor editor = sp.edit(); if (siv_item.isChecked()) { // 設(shè)置不選中 siv_item.setChecked(false); editor.putBoolean("update", false); } else { // 設(shè)置選中 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; /** * 服務(wù)工具類 * @author taoshihan * */ public class ServiceUtils { /** * 判斷某服務(wù)是否開啟 * @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; } }
設(shè)置效果如下:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android監(jiān)聽來電和去電的實(shí)現(xiàn)方法
- Android6.0來電號碼與電話薄聯(lián)系人進(jìn)行匹配
- Android監(jiān)聽手機(jī)電話狀態(tài)與發(fā)送郵件通知來電號碼的方法(基于PhoneStateListene實(shí)現(xiàn))
- Android中監(jiān)聽未接來電的2種方法
- Android實(shí)現(xiàn)獲取未接來電和未讀短信數(shù)量的方法
- Android實(shí)現(xiàn)判斷手機(jī)未接來電及處理方法
- android在root模式下接聽來電的方法
- android實(shí)現(xiàn)來電靜音示例(監(jiān)聽來電)
- android 電話狀態(tài)監(jiān)聽(來電和去電)實(shí)現(xiàn)代碼
- Android來電攔截的實(shí)現(xiàn)方法
相關(guān)文章
Android 使用【AIDL】調(diào)用外部服務(wù)的解決方法
本篇文章是對Android中使用AIDL調(diào)用外部服務(wù)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06Android粒子線條效果實(shí)現(xiàn)過程與代碼
這篇文章主要介紹了Android粒子線條效果的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-02-02Android實(shí)現(xiàn)微信朋友圈發(fā)本地視頻功能
這篇文章主要介紹了Android實(shí)現(xiàn)微信朋友圈發(fā)本地視頻功能的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-11-11Android中關(guān)于相對布局RelativeLayout的技巧匯總
RelativeLayout是相對布局控件,以控件之間相對位置或相對父容器位置進(jìn)行排列。下面這篇文章主要給大家介紹了關(guān)于Android中相對布局RelativeLayout的一些技巧,需要的朋友可以參考借鑒,下面來一起看看吧。2017-02-02Android實(shí)現(xiàn)分享長圖并且添加全圖水印
這篇文章主要介紹了Android實(shí)現(xiàn)分享長圖并且添加全圖水印的相關(guān)資料,需要的朋友可以參考下2017-03-03Android開發(fā)之ViewSwitcher用法實(shí)例
這篇文章主要介紹了Android開發(fā)之ViewSwitcher用法,結(jié)合實(shí)例形式分析了ViewSwitcher的功能、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-02-02