Jangan Terlalu Diajadikan Beban pikiran, ini hanyalah sekedar postingan percobaan untuk guru-guru SD N Gedongkuning Yogyakarta tempat kami melakukan pelatihan.

langsung saja ke langkah kerjanya...

1. buatlah project baru.
2. buka file xml nya. (layout)




<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

   
    <LinearLayout
        android:id="@+id/l1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dip"
        android:orientation="horizontal">
       
        <TextView
            android:id="@+id/tv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="5dip"
            android:text="Latitude :"
            android:textSize="20dip"/>
       
        <TextView
            android:id="@+id/tv2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Unknow"
            android:textSize="20dip"/>
     
    </LinearLayout>
   
    <LinearLayout
        android:id="@+id/l1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
       
        <TextView
            android:id="@+id/tv3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
                     android:layout_marginLeft="10dip"
            android:layout_marginRight="5dip"
            android:text="Longitude :"
            android:textSize="20dip"/>
       
        <TextView
            android:id="@+id/tv4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Unknow"
            android:textSize="20dip"/>
    
    </LinearLayout>
 

</LinearLayout>




3. buka file javanya. (mainactivity.java)



package com.lukmanrheza10;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity implements LocationListener{
              private TextView latituteField;
              private TextView longitudeField;
              private LocationManager locationManager;
              private String provider;
             
       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
                     setContentView(R.layout.activity_main);
                    
                     latituteField = (TextView) findViewById(R.id.tv2);
                     longitudeField = (TextView) findViewById(R.id.tv4);
                     // Get the location manager
                     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                     // Define the criteria how to select the locatioin provider -> use
                     // default
                     Criteria criteria = new Criteria();
                           provider = locationManager.getBestProvider(criteria, false);
                           Location location = locationManager.getLastKnownLocation(provider);
                     // Initialize the location fields
                                  if (location != null) {
                                         System.out.println("Provider " + provider + " has been selected.");
                                         onLocationChanged(location);
                                  }
                                  else {
                                         latituteField.setText("Location not available");
                                         longitudeField.setText("Location not available");
              }
       }
       /* Request updates at startup */
      
      
       @Override
       protected void onResume() {
              super.onResume();
                     locationManager.requestLocationUpdates(provider, 400, 1, this);
       }
       /* Remove the locationlistener updates when Activity is paused */
      
      
       @Override
       protected void onPause() {
              super.onPause();
                     locationManager.removeUpdates(this);
       }
      
      
       @Override
       public void onLocationChanged(Location location) {
             
                     double lat = (double) (location.getLatitude());
                     double lng = (double) (location.getLongitude());
                    
                           latituteField.setText(String.valueOf(lat));
                           longitudeField.setText(String.valueOf(lng));
       }
      
      
       @Override
       public void onStatusChanged(String provider, int status, Bundle extras) {
       // TODO Auto-generated method stub
       }
      
      
       @Override
       public void onProviderEnabled(String provider) {
              Toast.makeText(this, "Enabled new provider " + provider,
              Toast.LENGTH_SHORT).show();
       }
      
      
       @Override
       public void onProviderDisabled(String provider) {
              Toast.makeText(this, "Disabled provider " + provider,
              Toast.LENGTH_SHORT).show();
       }
      
      
       @Override
       public boolean onCreateOptionsMenu(Menu menu) {
       // Inflate the menu; this adds items to the action bar if it is present.
              getMenuInflater().inflate(R.menu.main, menu);
              return true;
       }
       }

 
4. Buka androidmanifestnya

 Cukup denagn menambahkan 3 buah permission. yaitu:




<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET"/>



 copykan source code tersebut dibawah uses sdk....

5. monggo di running...
kalau awalnya masih not avaliable...
selanjutnya buka DDMS dan pilih send pada tab menu emulator control

 

kalau sudah bener semua hasil jadinya kaya begindang...


, , , ,