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

service--取得手机网络信息

2014-02-25 21:38 351 查看
package org.lxh.demo;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

public class MyTelephoneManagerDemo extends Activity {
private ListView infolist = null; 					// ListView列表
private TelephonyManager manager = null; 			// 手机管理器
private ListAdapter adapter = null; 				// 适配器组件
private List<String> all = new ArrayList<String>(); // 保存信息

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main); 			// 调用布局管理器
this.infolist = (ListView) super
.findViewById(R.id.infolist); 				// 取得组件
this.manager = (TelephonyManager) super
.getSystemService(Context.TELEPHONY_SERVICE); // 取得手机服务
this.list();									// 列表显示
}

private void list() { 								// 执行列表显示操作
this.all.add(this.manager.getLine1Number() == null ? "没有手机号码" : "手机号码:"
+ this.manager.getLine1Number());		// 取得手机号码
this.all.add(this.manager.getNetworkOperatorName() == null ?
"没有移动服务商" : "移动服务商:" +
this.manager.getNetworkOperatorName());	// 取得移动商名称
if (this.manager.getPhoneType()
== TelephonyManager.NETWORK_TYPE_CDMA) {// 判断网络类型
this.all.add("移动网络:CDMA");
} else if (this.manager.getPhoneType() == TelephonyManager.NETWORK_TYPE_GPRS) {
this.all.add("移动网络:GPRS");
} else {
this.all.add("移动网络:未知");
}
if (this.manager.getNetworkType() ==
TelephonyManager.PHONE_TYPE_GSM) {			// 判断电话网络类型
this.all.add("网络类型:GSM");
} else if (this.manager.getNetworkType() == TelephonyManager.PHONE_TYPE_CDMA) {
this.all.add("网络类型:CDMA");
} else {
this.all.add("网络类型:未知");
}
this.all.add("是否漫游:" + (this.manager.isNetworkRoaming()
? "漫游" : "非漫游"));					// 是否是漫游
this.adapter = new ArrayAdapter<String>(this, 	// 实例化ArrayAdapter
android.R.layout.simple_list_item_1, 	// 定义布局文件
this.all); 								// 定义显示数据
this.infolist.setAdapter(this.adapter); 		// 设置数据
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/infolist"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.lxh.demo" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MyTelephoneManagerDemo"
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: