안 드 로 이 드 프로젝트 핸드폰 위 사 는 전화 로 번호 귀속 지 를 표시 합 니 다.

어제 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>
Setting ItemView 대상 을 가 져 옵 니 다.사용자 정의 컨트롤,상 태 를 설정 합 니 다.
SettingItemView 대상 의 setOnClickListener()방법 을 호출 하여 클릭 이 벤트 를 설정 하고 onClick 방법 을 다시 씁 니 다.
SettingItemView 대상 의 isChecked()방법 을 호출 하여 현재 선택 여 부 를 가 져 옵 니 다.
상 태 를 판단 하고 SettingItemView 대상 의 setChecked()방법 을 호출 하여 상 태 를 설정 합 니 다.매개 변수:불 값
startService()방법 을 호출 하여 휴대 전화 상 태 를 감청 하 는 서 비 스 를 엽 니 다.매개 변수:Intent 대상,
stopService()방법 을 호출 하여 서 비 스 를 닫 습 니 다.
현재 서비스 가 열 렸 는 지 판단 하고 컨트롤 의 기본 선택 상 태 를 설정 합 니 다.
새 도구 클래스 Services Utils.java
정적 방법 isServiceRunning()을 정의 합 니 다.입력 매개 변수:Context 컨 텍스트,String 서비스 이름
Context 대상 의 getSystemService()방법 을 호출 하여 Activity Manager 대상 을 가 져 옵 니 다.인자:Context.ACTIVITYSERVICE
Activity Manager 대상 의 getRunning Services()방법 을 호출 하여 실행 중인 서비스 List 집합 을 얻 습 니 다.매개 변수:int 최대 값
for 순환 List 집합,각각 Running Service Info 대상
Running Service Info.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;
  }
}

설정 효 과 는 다음 과 같 습 니 다:

이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기