<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample.foo.simplelocationapp" >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.sample.foo.simplelocationapp;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.ResultReceiver;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
LocationManager locationManager;
double longitudeBest, latitudeBest;
double longitudeGPS, latitudeGPS;
double longitudeNetwork, latitudeNetwork;
TextView longitudeValueBest, latitudeValueBest;
TextView longitudeValueGPS, latitudeValueGPS;
TextView longitudeValueNetwork, latitudeValueNetwork;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
longitudeValueBest = (TextView) findViewById(R.id.longitudeValueBest);
latitudeValueBest = (TextView) findViewById(R.id.latitudeValueBest);
longitudeValueGPS = (TextView) findViewById(R.id.longitudeValueGPS);
latitudeValueGPS = (TextView) findViewById(R.id.latitudeValueGPS);
longitudeValueNetwork = (TextView) findViewById(R.id.longitudeValueNetwork);
latitudeValueNetwork = (TextView) findViewById(R.id.latitudeValueNetwork);
}
private boolean checkLocation() {
if(!isLocationEnabled())
showAlert();
return isLocationEnabled();
}
private void showAlert() {
final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Enable Location")
.setMessage("Your Locations Settings is set to 'Off'.\nPlease Enable Location to " +
"use this app")
.setPositiveButton("Location Settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(myIntent);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
}
});
dialog.show();
}
private boolean isLocationEnabled() {
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}
public void toggleGPSUpdates(View view) {
if(!checkLocation())
return;
Button button = (Button) view;
if(button.getText().equals(getResources().getString(R.string.pause))) {
locationManager.removeUpdates(locationListenerGPS);
button.setText(R.string.resume);
}
else {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 2 * 60 * 1000, 10, locationListenerGPS);
button.setText(R.string.pause);
}
}
public void toggleBestUpdates(View view) {
if(!checkLocation())
return;
Button button = (Button) view;
if(button.getText().equals(getResources().getString(R.string.pause))) {
locationManager.removeUpdates(locationListenerBest);
button.setText(R.string.resume);
}
else {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
if(provider != null) {
locationManager.requestLocationUpdates(provider, 2 * 60 * 1000, 10, locationListenerBest);
button.setText(R.string.pause);
Toast.makeText(this, "Best Provider is " + provider, Toast.LENGTH_LONG).show();
}
}
}
public void toggleNetworkUpdates(View view) {
if(!checkLocation())
return;
Button button = (Button) view;
if(button.getText().equals(getResources().getString(R.string.pause))) {
locationManager.removeUpdates(locationListenerNetwork);
button.setText(R.string.resume);
}
else {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 60 * 1000, 10, locationListenerNetwork);
Toast.makeText(this, "Network provider started running", Toast.LENGTH_LONG).show();
button.setText(R.string.pause);
}
}
private final LocationListener locationListenerBest = new LocationListener() {
public void onLocationChanged(Location location) {
longitudeBest = location.getLongitude();
latitudeBest = location.getLatitude();
runOnUiThread(new Runnable() {
@Override
public void run() {
longitudeValueBest.setText(longitudeBest + "");
latitudeValueBest.setText(latitudeBest + "");
Toast.makeText(MainActivity.this, "Best Provider update", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
private final LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
longitudeNetwork = location.getLongitude();
latitudeNetwork = location.getLatitude();
runOnUiThread(new Runnable() {
@Override
public void run() {
longitudeValueNetwork.setText(longitudeNetwork + "");
latitudeValueNetwork.setText(latitudeNetwork + "");
Toast.makeText(MainActivity.this, "Network Provider update", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
private final LocationListener locationListenerGPS = new LocationListener() {
public void onLocationChanged(Location location) {
longitudeGPS = location.getLongitude();
latitudeGPS = location.getLatitude();
runOnUiThread(new Runnable() {
@Override
public void run() {
longitudeValueGPS.setText(longitudeGPS + "");
latitudeValueGPS.setText(latitudeGPS + "");
Toast.makeText(MainActivity.this, "GPS Provider update", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
}
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Buat PDF" />
</RelativeLayout>
Tidak ada komentar:
Posting Komentar