How to get Android Phone ServiceState - APK应用如何获得Android Phone的ServiceState,两种方法
来源:互联网 发布:张根学厉害吗 知乎 编辑:程序博客网 时间:2024/05/17 23:18
应用侦听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); } }
- How to get Android Phone ServiceState - APK应用如何获得Android Phone的ServiceState,两种方法
- 应用获得Android Phone ServiceState的方法
- How to get camped network type name - Android应用如何获得Phone当前注册的网络类型
- developer.Android.com:PhoneStateListener :onServiceStateChanged(ServiceState serviceState)
- 侦听Android手机ServiceState
- 侦听Android手机ServiceState
- 侦听Android手机ServiceState
- Android中Telephony学习笔记(7)-ServiceState
- How to stream video to your android phone 如何将视频远程传输至你的android手机?
- ServiceState学习
- How to Flash a ROM to Your Android Phone
- how to modify host file on android phone
- android - How to make a phone call from webview
- How to Dual Boot Multiple ROMs on Your Android Phone
- 实现相册里面的修剪功能,How To Crop A Picture On Your Android Phone
- 如何让你的APK跑在 com.android.phone 进程
- Android模拟器上开发Phone.apk需要解决的问题
- Android phone apk中对类的简单介绍
- 调用约定的坑
- php 递归错误
- 这就是传说中的神手啊!!
- linux下svn常用指令
- sqlhc脚本
- How to get Android Phone ServiceState - APK应用如何获得Android Phone的ServiceState,两种方法
- 基于C#+51上位机通讯程序
- 【send/sendto/sendmsg系统调用】
- 建立符合搜索抓取习惯的网站
- iOS 7专为女性用户而设计?
- 对于Visual Studio C++ 2008环境中出现运行时MFC对话框中文乱码的解决方案
- Json序列化
- TCP/IP笔记 四.应用层(3)——HTTP
- AOP