国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

android項(xiàng)目手機(jī)衛(wèi)士來(lái)電顯示號(hào)碼歸屬地

2019-12-12 04:59:46
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

昨日實(shí)現(xiàn)了360手機(jī)衛(wèi)士的來(lái)電顯示歸屬地的功能,具體的功能就是當(dāng)來(lái)電的時(shí)候,顯示當(dāng)前號(hào)碼的歸屬地,學(xué)習(xí)之后發(fā)現(xiàn)操作

非常的簡(jiǎ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;/** * 來(lái)電顯示 *  * @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ù)銷(xiāo)毀   */  @Override  public void onDestroy() {    // TODO Auto-generated method stub    super.onDestroy();    //取消監(jiān)聽(tīng)    tm.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);    phoneStateListener=null;  }}

設(shè)置中心,配置是否開(kāi)啟來(lái)電歸屬地顯示

直接使用我們之前定義好的組合控件

<com.qingguow.mobilesafe.ui.SettingItemView    tsh:title="設(shè)置顯示號(hào)碼歸屬地"    tsh:desc_on="設(shè)置顯示號(hào)碼歸屬地已開(kāi)啟"    tsh:desc_off="設(shè)置顯示號(hào)碼歸屬地已關(guān)閉"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:id="@+id/siv_show_address">  </com.qingguow.mobilesafe.ui.SettingItemView>

獲取到SettingItemView對(duì)象,我們自定義的控件,設(shè)置狀態(tài)

調(diào)用SettingItemView對(duì)象的setOnClickListener()方法,設(shè)置點(diǎn)擊事件,重寫(xiě)onClick方法

調(diào)用SettingItemView對(duì)象的isChecked()方法,得到當(dāng)前是否選中

判斷狀態(tài),調(diào)用SettingItemView對(duì)象的setChecked()方法,設(shè)置狀態(tài),參數(shù):布爾值

調(diào)用startService()方法,開(kāi)啟監(jiān)聽(tīng)手機(jī)狀態(tài)的服務(wù),參數(shù):Intent對(duì)象,

調(diào)用stopService()方法,關(guān)閉服務(wù)

判斷當(dāng)前服務(wù)是否開(kāi)啟,設(shè)置控件的默認(rèn)選中狀態(tài)

新建一個(gè)工具類ServicesUtils.java

定義一個(gè)靜態(tài)方法isServiceRunning(),傳入?yún)?shù):Context上下文,String服務(wù)名

調(diào)用Context對(duì)象的getSystemService()方法,獲取ActivityManager對(duì)象,參數(shù):Context.ACTIVITY_SERVICE

調(diào)用ActivityManager對(duì)象的getRunningServices()方法,得到運(yùn)行的服務(wù)List集合,參數(shù):int最大值

for循環(huán)List集合,每條是個(gè)RunningServiceInfo對(duì)象

調(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è)置是否開(kāi)啟號(hào)碼歸屬地  private SettingItemView showAddressBtn;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_setting);    // 設(shè)置號(hào)碼歸屬地    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);    }    // 自動(dòng)更新的點(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ù)是否開(kāi)啟   * @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è)置效果如下:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 漳州市| 晋城| 佛学| 宜兰县| 左云县| 那坡县| 中卫市| 梧州市| 大洼县| 青州市| 和平县| 高阳县| 阿图什市| 台南市| 财经| 思南县| 永城市| 凯里市| 团风县| 孝义市| 巴青县| 禹城市| 浑源县| 汤阴县| 江达县| 湟源县| 吉水县| 定西市| 萍乡市| 仁布县| 葵青区| 苏尼特左旗| 讷河市| 黑山县| 恭城| 龙胜| 丰县| 乌兰县| 通化县| 汨罗市| 西盟|