Page 99 - 24JAM_Pintar_Pemrograman_Android__1-libre
P. 99
24 JAM!! Pintar Pemrograman Android
18: android:layout_height="wrap_content"
19: android:textStyle="bold"
20: android:text="@string/latText"></TextView>
21: <TextView android:text="unknown"
22: android:id="@+id/latitudeTxt"
23: android:layout_width="wrap_content"
24: android:layout_height="wrap_content"></TextView>
25: </LinearLayout>
26:
4. Sekarang kita ke activity Lokasiku.java, tambahkan kode
berikut
1: package com.gps.sederhana;
2:
3: import android.app.Activity;
4: import android.content.Context;
5: import android.location.Location;
6: import android.location.LocationListener;
7: import android.location.LocationManager;
8: import android.os.Bundle;
9: import android.widget.TextView;
10: import android.widget.Toast;
11:
12: public class Lokasiku extends Activity {
13: private LocationManager lm;
14: private LocationListener locListener;
15: private TextView latTxt,lonTxt;
16:
17: /** Called when the activity is first created. */
18: @Override
19: public void onCreate(Bundle savedInstanceState) {
20: super.onCreate(savedInstanceState);
21: setContentView(R.layout.main);
22:
23: latTxt = (TextView) findViewById(R.id.latitudeTxt);
24: lonTxt = (TextView) findViewById(R.id.longitutdeTxt);
25: lm = (LocationManager)
26: getSystemService(Context.LOCATION_SERVICE);
27: locListener = new MyLocationListener();
28: lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
29: 200, locListener);
30: }
31:
32: private class MyLocationListener implements LocationListener
{
33: @Override
34: public void onLocationChanged(Location loc) {
35: if (loc != null) {
36: latTxt.setText(String.valueOf(loc.getLatitude()));
37: lonTxt.setText(String.valueOf(loc.getLongitude()));
38: Toast.makeText(getBaseContext(),
39: "Location Changed : Lat " + loc.getLatitude() +
40: "lgt: "+loc.getLongitude(), Toast.LENGTH_SHORT).show();
41: }
42: }
43: @Override
44: public void onProviderDisabled(String arg0) {}
45: @Override
Dapatkan materi terbaru di 99
www.omayib.com

