应用获得Android Phone ServiceState的方法

应用侦听Android Phone ServiceState的方法这里介绍两种方式

方法一:侦听 Intent  TelephonyIntents.ACTION_SERVICE_STATE_CHANGED, 此Intent 是 sticky 类型,所以应用每次启动都可以获得。

  

[java]  view plain copy print ?
  1. MyPhoneStateReceiver myreceiver = new MyPhoneStateReceiver();  

  1. @Override  
  2. protected void onCreate(Bundle savedInstanceState) {  
  3.     super.onCreate(savedInstanceState);  
  4.     setContentView(R.layout.activity_phone_status);  
  5.     mTextView = (TextView)findViewById(R.id.mytext);  
  6.       
  7.     Log.d(TAG, "Start my process");  
  8.       
  9.     IntentFilter filter = new IntentFilter();  
  10.     filter.addAction(ACTION_SERVICE_STATE_CHANGED);  
  11.     this.registerReceiver(myreceiver, filter);  
  12.       
  13. }  


[html]  view plain copy print ?
  1. private static final String ACTION_SERVICE_STATE_CHANGED = "android.intent.action.SERVICE_STATE";  
  2. /*  
  3.  * Get a sticky Intent.  
  4.  */  
  5. private class MyPhoneStateReceiver extends BroadcastReceiver {  
  6.     @Override  
  7.     public void onReceive(Context context, Intent intent) {  
  8.         if (intent.getAction().equals(ACTION_SERVICE_STATE_CHANGED)) {  
  9.             Log.d(TAG, "Intent Action: ACTION_SERVICE_STATE_CHANGED");  
  10.             //ServiceState mServiceState =  ServiceState.newFromBundle(intent.getExtras());  
  11.             //switch (mServiceState.getState()) {  
  12.             switch(intent.getExtras().getInt("state")){  
  13.                 case 0:  
  14.                     mStringSer = "IN SERVICE ";  
  15.                     break;  
  16.                 case 1:  
  17.                     mStringSer = "OUT OF SERVICE";  
  18.                     break;  
  19.                 case 2:  
  20.                     mStringSer = "EMERGENCY ONLY";  
  21.                     break;  
  22.                 case 3:  
  23.                     mStringSer = "POWER OFF";  
  24.                     break;  
  25.                 default:  
  26.                     mStringSer = "UNKNOWN";  
  27.                     break;  
  28.             }  
  29.               
  30.             //Waiting last info to print  
  31.             printPhoneStatus();  
  32.         }  
  33.     }  
  34. }  

注意在不用的时候取消注册:

[java]  view plain copy print ?
  1. @Override    
  2. protected void onDestroy() {    
  3.       this.unregisterReceiver(myreceiver);    
  4.       super.onDestroy();    
  5. }   

需要增加READ_PHONE_STATE PERMISSION

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.testtools.phonestatus"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="9"  
  9.         android:targetSdkVersion="17" />  
  10.     <uses-permission android:name="android.permission.READ_PHONE_STATE" />  
  11.   
  12.     <application  
  13.         android:allowBackup="true"  
  14.         android:icon="@drawable/ic_launcher"  
  15.         android:label="@string/app_name"  
  16.         android:theme="@style/AppTheme" >  
  17.         <activity  
  18.             android:name="com.testtools.phonestatus.PhoneStatus"  
  19.             android:label="@string/app_name" >  
  20.             <intent-filter>  
  21.                 <action android:name="android.intent.action.MAIN" />  
  22.   
  23.                 <category android:name="android.intent.category.LAUNCHER" />  
  24.             </intent-filter>  
  25.         </activity>  
  26.     </application>  
  27.   
  28. </manifest>  

activity_phone_status.xml

[html]  view plain copy print ?
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".PhoneStatus" >  
  10.   
  11.     <TextView  
  12.         android:id="@+id/mytext"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="@string/hello_world" />  
  16.   
  17. </RelativeLayout>  



方法二: 在TelephonyManager中注册ListenerPhoneStateListener, 不过注意com.android.internal.telephony 是hide的,所以应用无法使用,只能曲线救国。TelephonyManager并不能直接被实例化,要获取它的实例,需要通过Context.getSystemService(),注册Listener通过listen(),mPhoneStateListener这个PhoneStateListener中重写onServiceStateChanged(ServiceState serviceState)方法。

[java]  view plain copy print ?
  1. public String phoneServiceState() {  
  2.     MyPhoneStateListener mPhoneStateListener = new MyPhoneStateListener();  
  3.     TelephonyManager telMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);    
  4.     telMgr.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);  
  5.     return mStringSer;          
  6. }  

[java]  view plain copy print ?
  1. /* 
  2.  * public static final int STATE_IN_SERVICE = 0; 
  3.  * public static final int STATE_OUT_OF_SERVICE = 1; 
  4.  * public static final int STATE_EMERGENCY_ONLY = 2; 
  5.  * public static final int STATE_POWER_OFF = 3; 
  6.  * 有问题,有时候取不到 
  7.  */  
  8. class MyPhoneStateListener extends PhoneStateListener{            
  9.     @Override    
  10.     public void onServiceStateChanged(ServiceState mServiceState) {  
  11.           
  12.         switch(mServiceState.getState()) {  
  13.             case 0:  
  14.                 mStringSer = "IN SERVICE ";   
  15.                 break;  
  16.             case 1:  
  17.                 mStringSer = "OUT OF SERVICE";  
  18.                 break;  
  19.             case 2:  
  20.                 mStringSer = "EMERGENCY ONLY";  
  21.                 break;  
  22.             case 3:  
  23.                 mStringSer = "POWER OFF";   
  24.                 break;                     
  25.         }          
  26.         super.onServiceStateChanged(mServiceState);    
  27.     }              
  28. }   
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值