Android-x86
Fork
Faire un don

  • R/O
  • HTTP
  • SSH
  • HTTPS

development: Commit

development


Commit MetaInfo

Révisionbc4ea2d9326f24097b0fdd8ca7090688c0f6743b (tree)
l'heure2011-06-29 18:16:23
AuteurAnirudh Dewani <anirudhd@goog...>
CommiterAndroid Git Automerger

Message de Log

am fea911d5: Network detection sample using TelephonyManager

* commit 'fea911d5359f1eb42ff8b9c70cd6f51500f539d4':

Network detection sample using TelephonyManager

Change Summary

Modification

--- a/samples/ApiDemos/AndroidManifest.xml
+++ b/samples/ApiDemos/AndroidManifest.xml
@@ -36,6 +36,8 @@
3636 <!-- For android.media.audiofx.Visualizer -->
3737 <uses-permission android:name="android.permission.RECORD_AUDIO" />
3838
39+ <!-- For NetworkDetector sample: listen for phone state changes -->
40+ <uses-permission android:name="android.permission.READ_PHONE_STATE" />
3941 <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="11" />
4042
4143 <!-- The smallest screen this app works on is a phone. The app will
@@ -2507,6 +2509,17 @@
25072509 </receiver>
25082510
25092511 <!-- ************************************* -->
2512+ <!-- TELEPHONY SAMPLES -->
2513+ <!-- ************************************* -->
2514+
2515+ <activity android:name=".telephony.NetworkDetector" android:label="Telephony/NetworkDetector">
2516+ <intent-filter>
2517+ <action android:name="android.intent.action.MAIN" />
2518+ <category android:name="android.intent.category.SAMPLE_CODE" />
2519+ </intent-filter>
2520+ </activity>
2521+
2522+ <!-- ************************************* -->
25102523 <!-- OTHER SAMPLES -->
25112524 <!-- ************************************* -->
25122525
--- a/samples/ApiDemos/_index.html
+++ b/samples/ApiDemos/_index.html
@@ -36,6 +36,7 @@ document.write(""+
3636 " <li><a href='src/com/example/android/apis/graphics/index.html'>Graphics</a></li>"+
3737 " <li><a href='src/com/example/android/apis/media/index.html'>Media</a></li>"+
3838 " <li><a href='src/com/example/android/apis/os/index.html'>OS</a></li>"+
39+" <li><a href='src/com/example/android/apis/telephony/index.html'>Telephony</a></li>"+
3940 " <li><a href='src/com/example/android/apis/text/index.html'>Text</a></li>"+
4041 " <li><a href='src/com/example/android/apis/view/index.html'>Views</a></li></ul>");
4142
--- /dev/null
+++ b/samples/ApiDemos/res/layout/network_detector.xml
@@ -0,0 +1,19 @@
1+<?xml version="1.0" encoding="utf-8"?>
2+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+ android:orientation="vertical"
4+ android:layout_width="fill_parent"
5+ android:layout_height="fill_parent"
6+ >
7+ <TextView android:id="@+id/phone_type"
8+ android:layout_width="fill_parent"
9+ android:layout_height="wrap_content" />
10+ <TextView android:id="@+id/network_name"
11+ android:layout_width="fill_parent"
12+ android:layout_height="wrap_content" />
13+ <TextView android:id="@+id/sim_state"
14+ android:layout_width="fill_parent"
15+ android:layout_height="wrap_content" />
16+ <TextView android:id="@+id/network_type"
17+ android:layout_width="fill_parent"
18+ android:layout_height="wrap_content" />
19+</LinearLayout>
--- /dev/null
+++ b/samples/ApiDemos/src/com/example/android/apis/telephony/NetworkDetector.java
@@ -0,0 +1,221 @@
1+/*
2+ * Copyright (C) 2011 The Android Open Source Project
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+package com.example.android.apis.telephony;
18+
19+import android.app.Activity;
20+import android.os.Bundle;
21+import android.telephony.PhoneStateListener;
22+import android.telephony.ServiceState;
23+import android.telephony.TelephonyManager;
24+import android.widget.TextView;
25+
26+// Need the following import to get access to the app resources, since this
27+// class is in a sub-package.
28+import com.example.android.apis.R;
29+
30+/**
31+ * Activity that uses {@link android.telephony.TelephonyManager} to obtain
32+ * telephony parameters like network state, phone type and SIM state.
33+ */
34+public class NetworkDetector extends Activity {
35+
36+ /*
37+ * SIM state constants
38+ */
39+ public static final String SIM_ABSENT = "Absent";
40+ public static final String SIM_READY = "Ready";
41+ public static final String SIM_PIN_REQUIRED = "PIN required";
42+ public static final String SIM_PUK_REQUIRED = "PUK required";
43+ public static final String SIM_NETWORK_LOCKED = "Network locked";
44+ public static final String SIM_UNKNOWN = "Unknown";
45+
46+ /*
47+ * Network type constants
48+ */
49+ public static final String NETWORK_CDMA = "CDMA: Either IS95A or IS95B (2G)";
50+ public static final String NETWORK_EDGE = "EDGE (2.75G)";
51+ public static final String NETWORK_GPRS = "GPRS (2.5G)";
52+ public static final String NETWORK_UMTS = "UMTS (3G)";
53+ public static final String NETWORK_EVDO_0 = "EVDO revision 0 (3G)";
54+ public static final String NETWORK_EVDO_A = "EVDO revision A (3G - Transitional)";
55+ public static final String NETWORK_EVDO_B = "EVDO revision B (3G - Transitional)";
56+ public static final String NETWORK_1X_RTT = "1xRTT (2G - Transitional)";
57+ public static final String NETWORK_HSDPA = "HSDPA (3G - Transitional)";
58+ public static final String NETWORK_HSUPA = "HSUPA (3G - Transitional)";
59+ public static final String NETWORK_HSPA = "HSPA (3G - Transitional)";
60+ public static final String NETWORK_IDEN = "iDen (2G)";
61+ public static final String NETWORK_LTE = "LTE (4G)";
62+ public static final String NETWORK_EHRPD = "EHRPD (3G)";
63+ public static final String NETWORK_HSPAP = "HSPAP (3G)";
64+ public static final String NETWORK_UNKOWN = "Unknown";
65+
66+ /*
67+ * Phone type constants
68+ */
69+ public static final String PHONE_CDMA = "CDMA";
70+ public static final String PHONE_GSM = "GSM";
71+ public static final String PHONE_SIP = "SIP";
72+ public static final String PHONE_NONE = "No radio";
73+
74+ @Override
75+ public void onCreate(Bundle savedInstanceState) {
76+ super.onCreate(savedInstanceState);
77+ setContentView(R.layout.network_detector);
78+
79+ // Get the telephony system service to find out network details
80+ final TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
81+
82+ // Update text views with readable values.
83+ updateViews(tm);
84+
85+ // Since these attributes can change, we will register a
86+ // {@code PhoneStateListener} to listen for these changes and
87+ // update the view.
88+ tm.listen(new PhoneStateListener() {
89+ @Override
90+ public void onServiceStateChanged(ServiceState serviceState) {
91+ // Update our TextViews
92+ updateViews(tm);
93+ }
94+
95+ @Override
96+ public void onDataConnectionStateChanged(int state) {
97+ // A change in data connection state may be due to availability
98+ // of a different network type
99+ updateViews(tm);
100+ }
101+
102+ }, PhoneStateListener.LISTEN_SERVICE_STATE
103+ | PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
104+ }
105+
106+ @Override
107+ protected void onResume() {
108+ super.onResume();
109+ final TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
110+
111+ // Update text views with readable values.
112+ updateViews(tm);
113+ }
114+
115+ /**
116+ * Update text views with telephony attributes.
117+ */
118+ private final void updateViews(TelephonyManager tm) {
119+
120+ // The telephony system service returns integer constants for various
121+ // telephony attributes.
122+ TextView view = null;
123+
124+ view = (TextView) findViewById(R.id.sim_state);
125+ view.setText("SIM State: " + mapSimStateToName(tm.getSimState()));
126+
127+ view = (TextView) findViewById(R.id.network_type);
128+ view.setText("Network Type: " + mapNetworkTypeToName(tm.getNetworkType()));
129+
130+ view = (TextView) findViewById(R.id.phone_type);
131+ view.setText("Phone Type: " + mapDeviceTypeToName(tm.getPhoneType()));
132+
133+ view = (TextView) findViewById(R.id.network_name);
134+ view.setText("Network Operator: " + tm.getNetworkOperatorName());
135+ }
136+
137+ /**
138+ * Returns a string describing the current SIM state.
139+ */
140+ private static String mapSimStateToName(int simState) {
141+ switch (simState) {
142+ case TelephonyManager.SIM_STATE_ABSENT:
143+ return SIM_ABSENT;
144+ case TelephonyManager.SIM_STATE_READY:
145+ return SIM_READY;
146+ case TelephonyManager.SIM_STATE_PIN_REQUIRED:
147+ return SIM_PIN_REQUIRED;
148+ case TelephonyManager.SIM_STATE_PUK_REQUIRED:
149+ return SIM_PUK_REQUIRED;
150+ case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
151+ return SIM_NETWORK_LOCKED;
152+ case TelephonyManager.SIM_STATE_UNKNOWN:
153+ return SIM_UNKNOWN;
154+ default:
155+ // shouldn't happen.
156+ return null;
157+ }
158+ }
159+
160+ /**
161+ * Returns a string indicating the phone radio type.
162+ */
163+ private static String mapDeviceTypeToName(int device) {
164+
165+ switch (device) {
166+ case TelephonyManager.PHONE_TYPE_CDMA:
167+ return PHONE_CDMA;
168+ case TelephonyManager.PHONE_TYPE_GSM:
169+ return PHONE_GSM;
170+ case TelephonyManager.PHONE_TYPE_SIP:
171+ return PHONE_SIP;
172+ case TelephonyManager.PHONE_TYPE_NONE:
173+ return PHONE_NONE;
174+ default:
175+ // shouldn't happen.
176+ return null;
177+ }
178+ }
179+
180+ /**
181+ * Returns a string describing the network type.
182+ */
183+ public static String mapNetworkTypeToName(int networkType) {
184+
185+ switch (networkType) {
186+ case TelephonyManager.NETWORK_TYPE_CDMA:
187+ return NETWORK_CDMA;
188+ case TelephonyManager.NETWORK_TYPE_EDGE:
189+ return NETWORK_EDGE;
190+ case TelephonyManager.NETWORK_TYPE_GPRS:
191+ return NETWORK_EDGE;
192+ case TelephonyManager.NETWORK_TYPE_UMTS:
193+ return NETWORK_UMTS;
194+ case TelephonyManager.NETWORK_TYPE_EVDO_0:
195+ return NETWORK_EVDO_0;
196+ case TelephonyManager.NETWORK_TYPE_EVDO_A:
197+ return NETWORK_EVDO_A;
198+ case TelephonyManager.NETWORK_TYPE_EVDO_B:
199+ return NETWORK_EVDO_B;
200+ case TelephonyManager.NETWORK_TYPE_1xRTT:
201+ return NETWORK_1X_RTT;
202+ case TelephonyManager.NETWORK_TYPE_HSDPA:
203+ return NETWORK_HSDPA;
204+ case TelephonyManager.NETWORK_TYPE_HSPA:
205+ return NETWORK_HSPA;
206+ case TelephonyManager.NETWORK_TYPE_HSUPA:
207+ return NETWORK_HSUPA;
208+ case TelephonyManager.NETWORK_TYPE_IDEN:
209+ return NETWORK_IDEN;
210+ case TelephonyManager.NETWORK_TYPE_LTE:
211+ return NETWORK_LTE;
212+ case TelephonyManager.NETWORK_TYPE_EHRPD:
213+ return NETWORK_EHRPD;
214+ case TelephonyManager.NETWORK_TYPE_HSPAP:
215+ return NETWORK_HSPAP;
216+ case TelephonyManager.NETWORK_TYPE_UNKNOWN:
217+ default:
218+ return NETWORK_UNKOWN;
219+ }
220+ }
221+}
Afficher sur ancien navigateur de dépôt.