안 드 로 이 드 프로젝트 핸드폰 위 사 는 전화 로 번호 귀속 지 를 표시 합 니 다.
매우 간단 합 니 다.구체 적 인 실현 코드 는 다음 과 같 습 니 다.
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;
}
}
설정 효 과 는 다음 과 같 습 니 다:이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다. 유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.