应用侦听Android Phone ServiceState的方法这里介绍两种方式
方法一:侦听 Intent TelephonyIntents.ACTION_SERVICE_STATE_CHANGED, 此Intent 是 sticky 类型,所以应用每次启动都可以获得。
- MyPhoneStateReceiver myreceiver = new MyPhoneStateReceiver();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_phone_status);
- mTextView = (TextView)findViewById(R.id.mytext);
- Log.d(TAG, "Start my process");
- IntentFilter filter = new IntentFilter();
- filter.addAction(ACTION_SERVICE_STATE_CHANGED);
- this.registerReceiver(myreceiver, filter);
- }
- private static final String ACTION_SERVICE_STATE_CHANGED = "android.intent.action.SERVICE_STATE";
- /*
- * Get a sticky Intent.
- */
- private class MyPhoneStateReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- if (intent.getAction().equals(ACTION_SERVICE_STATE_CHANGED)) {
- Log.d(TAG, "Intent Action: ACTION_SERVICE_STATE_CHANGED");
- //ServiceState mServiceState = ServiceState.newFromBundle(intent.getExtras());
- //switch (mServiceState.getState()) {
- switch(intent.getExtras().getInt("state")){
- case 0:
- mStringSer = "IN SERVICE ";
- break;
- case 1:
- mStringSer = "OUT OF SERVICE";
- break;
- case 2:
- mStringSer = "EMERGENCY ONLY";
- break;
- case 3:
- mStringSer = "POWER OFF";
- break;
- default:
- mStringSer = "UNKNOWN";
- break;
- }
- //Waiting last info to print
- printPhoneStatus();
- }
- }
- }
注意在不用的时候取消注册:
- @Override
- protected void onDestroy() {
- this.unregisterReceiver(myreceiver);
- super.onDestroy();
- }
需要增加READ_PHONE_STATE PERMISSION
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.testtools.phonestatus"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="9"
- android:targetSdkVersion="17" />
- <uses-permission android:name="android.permission.READ_PHONE_STATE" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.testtools.phonestatus.PhoneStatus"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
activity_phone_status.xml
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".PhoneStatus" >
- <TextView
- android:id="@+id/mytext"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/hello_world" />
- </RelativeLayout>
方法二: 在TelephonyManager中注册ListenerPhoneStateListener, 不过注意com.android.internal.telephony 是hide的,所以应用无法使用,只能曲线救国。TelephonyManager并不能直接被实例化,要获取它的实例,需要通过Context.getSystemService(),注册Listener通过listen(),mPhoneStateListener这个PhoneStateListener中重写onServiceStateChanged(ServiceState serviceState)方法。
- public String phoneServiceState() {
- MyPhoneStateListener mPhoneStateListener = new MyPhoneStateListener();
- TelephonyManager telMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
- telMgr.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
- return mStringSer;
- }
- /*
- * public static final int STATE_IN_SERVICE = 0;
- * public static final int STATE_OUT_OF_SERVICE = 1;
- * public static final int STATE_EMERGENCY_ONLY = 2;
- * public static final int STATE_POWER_OFF = 3;
- * 有问题,有时候取不到
- */
- class MyPhoneStateListener extends PhoneStateListener{
- @Override
- public void onServiceStateChanged(ServiceState mServiceState) {
- switch(mServiceState.getState()) {
- case 0:
- mStringSer = "IN SERVICE ";
- break;
- case 1:
- mStringSer = "OUT OF SERVICE";
- break;
- case 2:
- mStringSer = "EMERGENCY ONLY";
- break;
- case 3:
- mStringSer = "POWER OFF";
- break;
- }
- super.onServiceStateChanged(mServiceState);
- }
- }