Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.hfad.spaceappswater;
- import android.content.Context;
- import android.content.Intent;
- import android.location.Address;
- import android.location.Geocoder;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.net.wifi.p2p.WifiP2pManager;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.*;
- import android.widget.*;
- import java.io.IOException;
- import java.util.List;
- import java.util.Locale;
- public class MainActivity extends AppCompatActivity{
- private Button location_button = null;
- private TextView location_view = null;
- private Button water_button = null;
- private TextView water_view = null;
- private Spinner water_spinner = null;
- private String latitude = "";
- private String longitude = "";
- private int ltd;
- private int lng;
- private static final String TAG = "MainActivity";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- location_button = (Button)findViewById(R.id.location_button);
- water_button = (Button)findViewById(R.id.water_button);
- location_view = (TextView)findViewById(R.id.location_view);
- }
- private class LocListener implements LocationListener {
- //String elocation = "";
- @Override
- public void onLocationChanged(Location loc) {
- String longitude = "Longitude: " + loc.getLongitude();
- double lng = loc.getLongitude();
- Log.v(TAG, longitude);
- double ltd = loc.getLatitude();
- String latitude = "Latitude: " + loc.getLatitude();
- Log.v(TAG, latitude);
- /*------- To get city name from coordinates -------- */
- String cityName = null;
- Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
- List<Address> addresses;
- try {
- addresses = gcd.getFromLocation(loc.getLatitude(),
- loc.getLongitude(), 1);
- if (addresses.size() > 0) {
- System.out.println(addresses.get(0).getLocality());
- cityName = addresses.get(0).getLocality();
- }
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- String s = longitude + "\n" + latitude + "\n\nMy Current City is: "
- + cityName;
- location_view.setText(s);
- }
- @Override
- public void onProviderDisabled(String provider) {}
- @Override
- public void onProviderEnabled(String provider) {}
- @Override
- public void onStatusChanged(String provider, int status, Bundle extras) {}
- }
- public void locationClick(View view){
- String text = "Checking Location...";
- int duration = Toast.LENGTH_LONG;
- Toast toast = Toast.makeText(this, text, duration);
- toast.show();
- LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
- LocationListener locationListener = new LocListener();
- try{
- locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 10, locationListener);}
- catch (SecurityException e){
- Log.d(TAG, "Access Denied! Please contact our operator");
- }
- }
- public void waterClick(View view){
- if((latitude.equals(""))||(longitude.equals(""))){
- String text = "Error!!! Location not Checked!!!";
- int duration = Toast.LENGTH_SHORT;
- Toast toast = Toast.makeText(this, text, duration);
- }
- else{
- Intent intent = new Intent(MainActivity.this, MapActivity.class);
- intent.putExtra(MapActivity.EXTRA_LATITUDE, ltd);
- intent.putExtra(MapActivity.EXTRA_LONGITUDE, lng);
- startActivity(intent);
- }
- }
- @Override
- protected void onSaveInstanceState(Bundle savedInstanceState){
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement