TelephonyManager电话管理器、监听手机来电
2016-02-22 19:07
337 查看
由疯狂讲义第三版整理而来;
/*
* TelephoneManager是一个管理手机通话状态、电话网络信息的服务类,该类提供了大量的getXxx()方法来获取电话网络的相关信息;
* 还提供了listen(PhoneStateListener listener, int events) 方法来监听通话状态;
* 添加权限
*
*
*/
/*
* TelephoneManager是一个管理手机通话状态、电话网络信息的服务类,该类提供了大量的getXxx()方法来获取电话网络的相关信息;
* 还提供了listen(PhoneStateListener listener, int events) 方法来监听通话状态;
* 添加权限
*
*
*/
public class MainActivity extends Activity { TelephonyManager tm; ListView listView; String[] statusName; //声明代表状态名的数组; ArrayList<String> statusValues = new ArrayList<String>(); //声明代表手机状态的集合; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); getPhoneStatus(); getListen(); } private void getPhoneStatus() { //获取代表各种状态名称的数组; statusName = getResources().getStringArray(R.array.statusName); //获取代表SIM卡状态的数组; String[] simStatu = getResources().getStringArray(R.array.simState); //获取代表电话网络类型的数组; String[] phoneType = getResources().getStringArray(R.array.phoneType); statusValues.add(tm.getDeviceId()); // 获取设备编号 statusValues.add(tm.getDeviceSoftwareVersion() != null ? tm.getDeviceSoftwareVersion() : "未知"); // 获取系统平台的版本 statusValues.add(tm.getNetworkOperator()); // 获取网络运营商代号 statusValues.add(tm.getNetworkOperatorName()); // 获取网络运营商名称 statusValues.add(phoneType[tm.getPhoneType()]); // 获取手机网络类型 statusValues.add(tm.getCellLocation() != null ? tm .getCellLocation().toString() : "未知位置"); // 获取设备所在位置 statusValues.add(tm.getSimCountryIso()); // 获取SIM卡的国别 statusValues.add(tm.getSimSerialNumber()); // 获取SIM卡序列号 statusValues.add(simStatu[tm.getSimState()]); // 获取SIM卡状态 listView = (ListView)findViewById(R.id.listView); // 获得ListView对象 ArrayList<Map<String, String>> status = new ArrayList<Map<String, String>>(); for(int i=0; i<statusValues.size(); i++) { HashMap<String, String> map = new HashMap<String, String>(); map.put("name", statusName[i]); map.put("value", statusValues.get(i)); status.add(map); } //使用SimpleAdapter封装list数据; SimpleAdapter sa = new SimpleAdapter(this, status, R.layout.list, new String[] {"name", "value"}, new int[]{R.id.name, R.id.value}); listView.setAdapter(sa); //设置Adapter; } //通话状态监听; //<uses-permission android:name="android.permission.READ_PHONE_STATE"/> //phoneList文件在data/data/com.lu.telephone/files目录下; //把这段代码放在后台执行的service中运行,并设置service组件随系统开机自动运行,这种监听就可以"神不知鬼不觉"了; private void getListen() { PhoneStateListener phone = new PhoneStateListener() { @Override public void onCallStateChanged(int state, String incomingNumber) { 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() + "来电:" + incomingNumber); //将来电号码记录到文件当中; break; } super.onCallStateChanged(state, incomingNumber); } }; tm.listen(phone, PhoneStateListener.LISTEN_CALL_STATE); //监听电话通话状态的改变; } } res/values/array数组文件: <?xml version="1.0" encoding="utf-8"?> <resources> <!-- 声明一个名为statusNames的字符串数组 --> <string-array name="statusName"> <item>设备编号</item> <item>软件版本</item> <item>网络运营商代号</item> <item>网络运营商名称</item> <item>手机制式</item> <item>设备当前位置</item> <item>SIM卡的国别</item> <item>SIM卡序列号</item> <item>SIM卡状态</item> </string-array> <!-- 声明一个名为simState的字符串数组 --> <string-array name="simState"> <item>状态未知</item> <item>无SIM卡</item> <item>被PIN加锁</item> <item>被PUK加锁</item> <item>被NetWork PIN加锁</item> <item>已准备好</item> </string-array> <!-- 声明一个名为phoneType的字符串数组 --> <string-array name="phoneType"> <item>未知</item> <item>GSM</item> <item>CDMA</item> </string-array> </resources> xml布局文件: <LinearLayout 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" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="获取网络和SIM卡信息" /> <ListView android:id="@+id/listView" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"/> </LinearLayout> listView布局文件: <?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="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/value" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
相关文章推荐
- 界面布局与常用资源使用
- iOS蓝牙开发(二):iOS连接外设的代码实现
- 经典dfs(depth-first search)
- java 四舍五入保留小数
- Candy
- [BZOJ 1492]货币兑换Cash
- c++ 排序算法总结
- Android 启动时间优化 <22>
- HDOJ-1081 To The Max
- iOS蓝牙开发(一)蓝牙相关基础知识
- c++ 排序算法总结
- ps切图(5)——图层原理及其操作
- database export import
- uva 10720 Graph Construction
- dstat系统分析工具的使用
- LeetCode 94. Binary Tree Inorder Traversal
- jQuery和ExtJS的timeOut超时问题和event事件处理问题
- install wine on ubuntu
- 谁有(爱普生R290维修程序)
- 给大一学生:让积累说话