您的位置:首页 > 理论基础 > 计算机网络

利用TelephonyManager(电话管理器)类来去的手机通话状态、网络、位置、Sim卡等信息

2016-11-15 15:21 381 查看

TelephonyManager类作用;

代表手机各种状态的数组;

举例说明获取手机各种数据的实例

利用TelephonyManager和Service实现开机自动监听记录手机通话记录实例

——————-TelephonyManager类的作用———————-

TelephonyManager 顾名思义,他是管理手机各种虚拟状态的类,他可以用来管理手机通话状态,电话网络服务器信息,SIM信息,本机手机版本等信息,只需要借助本类下面的方法就可以很方便的取值;

——————–代表手机各种状态的数组———————–

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array  name="statusNames">
<item>设备编号</item>
<item>软件版本</item>
<item>网络运营商代号</item>
<item>网络运营商名称</item>
<item>手机制式</item>
<item>设备当前位置</item>
<item>SIM卡的国别</item>
<item>SIM卡序列号</item>
<item>SIM卡状态</item>
</string-array>
<string-array  name="simState" >
<item>状态未知</item>
<item>无SIM卡</item>
<item>被PIN加锁</item>
<item>被PUK加锁</item>
<item>被NetWork PIN加锁</item>
<item>已准备好</item>
</string-array>
<string-array name="phoneType">
<item>未知</item>
<item>GSM</item>
<item>CDMA</item>
</string-array>
</resources>


——————–举例说明获取本机各种信息———————–

ListView listView;
String[] statusNames;
ArrayList<String> statusValues = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView);
// 获取TelephonyManager对象
TelephonyManager tManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
// 获取values文件下资源文件array.xml中的资源
statusNames = getResources().getStringArray(R.array.statusNames);// 获取各种状态名称的数组
// 获取各种代表SIM卡状态的数组
String[] simStatus = getResources().getStringArray(R.array.simState);
// 获取代表网络类型的数组
String[] phoneType = getResources().getStringArray(R.array.phoneType);
statusValues.add(tManager.getDeviceId());// 获取设备编号
statusValues.add(tManager.getDeviceSoftwareVersion() != null ? tManager
.getDeviceSoftwareVersion() : "未知");// 获取系统平台版本
statusValues.add(tManager.getNetworkOperator());// 获取网络运行商代号
statusValues.add(tManager.getNetworkOperatorName());// 获取网络运行商名称
statusValues.add(phoneType[tManager.getPhoneType()]);// 获取手机的网络类型,这里
statusValues.add(tManager.getCellLocation() != null ? tManager
.getCellLocation().toString() : "未知位置");// 获取手机所在的地址
statusValues.add(tManager.getSimCountryIso());// 获取SIM卡的国别
statusValues.add(tManager.getSimSerialNumber());// 获取SIM卡序列号
statusValues.add(simStatus[tManager.getSimState()]);// 获取SIM卡状态

ArrayList<Map<String, String>> status = new ArrayList<>();
for (int i = 0; i < statusValues.size(); i++) {
Map<String, String> map = new HashMap<>();
map.put("name", statusNames[i]);
map.put("values", statusValues.get(i));
status.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this, status, R.layout.line,
new String[] { "name", "values" }, new int[] { R.id.name,
R.id.values });
listView.setAdapter(adapter);
}


备注:通过这个实例,可以很清晰的了解TelephonyManager类的一些常用用法,还有回顾了SimpleAdapter类的用法。

最后加上手机位置访问权限和手机状态访问权限

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />


为了把本实例演示完成,还需要下面的xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<TextView
android:id="@+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />

<TextView
android:id="@+id/values"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />

</LinearLayout>


———–利用TelephonyManager和Service实现开机自动监听记录手机通话记录实例——————

AutoService权限和注册receiver:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<receiver android:name=".AutoService" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>


AutoService.class代码:

package com.example.telephonymanager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AutoService extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent receiverIntent = new Intent(context,TelephonyManagerService.class);
context.startService(receiverIntent);
}
}


TelephonyManagerService.class页面代码:

package com.example.telephonymanager;

import java.io.FileNotFoundException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Date;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import androi
ac65
d.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class TelephonyManagerService extends Service {
TelephonyManager tManager;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
tManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener listener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String number) {// 字面理解当电话状态改变的时候触发此方法
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
case TelephonyManager.CALL_STATE_RINGING:
OutputStream os = null;
try{
os=  openFileOutput("phoneList",MODE_APPEND);
}catch(FileNotFoundException e){
e.printStackTrace();
}
PrintStream ps = new PrintStream(os);
ps.println(new Date()+"来电"+number);
ps.close();
break;
default:
break;
}
super.onCallStateChanged(state, number);
}
};
tManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
}


OK,没有了解的同学,通过这个实例就知道怎么用了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  TelephonyM
相关文章推荐