Get cell location on a GSM phone, getCellLocation()
TelephonyManager.getCellLocation() return the current location of the device.
We need the following permission in this example:
We need the following permission in this example:
- android.permission.ACCESS_COARSE_LOCATION
- android.permission.ACCESS_FINE_LOCATION
- android.permission.READ_PHONE_STATE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| package com.AndroidTelephonyManager; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.telephony.TelephonyManager; import android.telephony.gsm.GsmCellLocation; import android.widget.TextView; public class AndroidTelephonyManager extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); TextView textGsmCellLocation = (TextView)findViewById(R.id.gsmcelllocation); TextView textCID = (TextView)findViewById(R.id.cid); TextView textLAC = (TextView)findViewById(R.id.lac); //retrieve a reference to an instance of TelephonyManager TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); GsmCellLocation cellLocation = (GsmCellLocation)telephonyManager.getCellLocation(); int cid = cellLocation.getCid(); int lac = cellLocation.getLac(); textGsmCellLocation.setText(cellLocation.toString()); textCID.setText( "gsm cell id: " + String.valueOf(cid)); textLAC.setText( "gsm location area code: " + String.valueOf(lac)); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| <? xml version = "1.0" encoding = "utf-8" ?> android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent" > < TextView android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "@string/hello" /> < TextView android:id = "@+id/gsmcelllocation" android:layout_width = "fill_parent" android:layout_height = "wrap_content" /> < TextView android:id = "@+id/cid" android:layout_width = "fill_parent" android:layout_height = "wrap_content" /> < TextView android:id = "@+id/lac" android:layout_width = "fill_parent" android:layout_height = "wrap_content" /> </ LinearLayout > |
No comments:
Post a Comment