Algorithm:
1.) Create a new project by File-> New -> Android Project name it GetGSMPhoneLocation.
2.) Write following into main.xml:
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
| <? 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 = "GSM Phone Location" /> < 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 > |
3.) Write following permissions into your manifest file:
1
2
3
| < uses-permission android:name = "android.permission.ACCESS_COARSE_LOCATION" /> < uses-permission android:name = "android.permission.ACCESS_FINE_LOCATION" /> < uses-permission android:name = "android.permission.READ_PHONE_STATE" /> |
4.) Run for output.
Steps:
1.) Create a project named GetGSMPhoneLocation and set the information as stated in the image.
Build Target: Android 4.4
Application Name: GetGSMPhoneLocation
Package Name: com.example. GetGSMPhoneLocation
Activity Name: GetGSMPhoneLocationActivity
Application Name: GetGSMPhoneLocation
Package Name: com.example. GetGSMPhoneLocation
Activity Name: GetGSMPhoneLocationActivity
2.) Open GetGSMPhoneLocationActivity.java file and write following code there:
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
| package com.example.getgsmphonelocation; 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 GetGSMPhoneLocationActivity 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( "GSM Cell Location: " +cellLocation.toString()); textCID.setText( "GSM Cell id: " + String.valueOf(cid)); textLAC.setText( "GSM Location area code: " + String.valueOf(lac)); } } |
3.) Compile and build the project.
Output
No comments:
Post a Comment