Android Open Source - firstcodeandroid Main Activity






From Project

Back to project page firstcodeandroid.

License

The source code is released under:

MIT License

If you think the Android project firstcodeandroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.vjia.locationtest;
/*w  w w  .  java2  s .co  m*/
import java.util.List;

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

public class MainActivity extends Activity {
  
  private TextView positionTextView;
  private LocationManager locationManager;
  private String provider;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    positionTextView = (TextView)this.findViewById(R.id.position_text_view);
    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    // get all the available Position Provider
    List<String> providerList=locationManager.getProviders(true);
    if(providerList.contains(LocationManager.GPS_PROVIDER)){
      provider=LocationManager.GPS_PROVIDER;
    }else if(providerList.contains(LocationManager.NETWORK_PROVIDER)){
      provider=LocationManager.NETWORK_PROVIDER;
    }else{
      // no available Postion Provider, show Toast to user
      Toast.makeText(this, "No location provider to use", Toast.LENGTH_SHORT);
      return;
    }
    
    Location location = locationManager.getLastKnownLocation(provider);
    if(location==null){
      // show current device's location info
      showLocation(location);
    }
    //(String provider, long minTime, float minDistance, LocationListener listener)
    locationManager.requestLocationUpdates(provider, 5000, 1, locationListener);
  }
  
  protected void onDestroy(){
    super.onDestroy();
    if(locationManager!=null){
      // review the Listener when shutdown the APP
      locationManager.removeUpdates(locationListener);
    }
  }
  
  LocationListener locationListener = new LocationListener(){

    @Override
    public void onLocationChanged(Location location) {
      // TODO Auto-generated method stub
      showLocation(location);
    }

    @Override
    public void onProviderDisabled(String provider) {
      // TODO Auto-generated method stub
      
    }

    @Override
    public void onProviderEnabled(String arg0) {
      // TODO Auto-generated method stub
      
    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
      // TODO Auto-generated method stub
      
    }
    
  };

  private void showLocation(Location location) {
    // TODO Auto-generated method stub
    String currentPosition= String.format("latitude is %s %n logitude is %s", 
        location.getLatitude(), location.getLongitude());
    positionTextView.setText(currentPosition);
  }
}




Java Source Code List

com.example.activitylifecycletest.DialogActivity.java
com.example.activitylifecycletest.MainActivity.java
com.example.activitylifecycletest.NormalActivity.java
com.example.activitytest.FirstActivity.java
com.example.listviewtest.FruitAdapter.java
com.example.listviewtest.Fruit.java
com.example.listviewtest.MainActivity.java
com.jikexueyuan.counttime.MainActivity.java
com.jikexueyuan.getmyphonenumber.GetNumber.java
com.jikexueyuan.getmyphonenumber.MainActivity.java
com.jikexueyuan.getmyphonenumber.MyAdapter.java
com.jikexueyuan.getmyphonenumber.PhoneInfo.java
com.vjia.bookcollector.MainActivity.java
com.vjia.coolweather.MainActivity.java
com.vjia.coolweather.activity.ChooseAreaActivity.java
com.vjia.coolweather.activity.WeatherActivity.java
com.vjia.coolweather.db.CoolWeatherDB.java
com.vjia.coolweather.db.CoolWeatherOpenHelper.java
com.vjia.coolweather.model.City.java
com.vjia.coolweather.model.County.java
com.vjia.coolweather.model.Province.java
com.vjia.coolweather.util.HttpCallbackListener.java
com.vjia.coolweather.util.HttpUtil.java
com.vjia.coolweather.util.Utility.java
com.vjia.helloandroid.FirstActivity.java
com.vjia.helloandroid.HelloAndroidActivity.java
com.vjia.hellonote.AddContent.java
com.vjia.hellonote.MainActivity.java
com.vjia.hellonote.MyAdapter.java
com.vjia.hellonote.NotesDB.java
com.vjia.hellonote.SelectAct.java
com.vjia.jokeking.GetJoke.java
com.vjia.jokeking.HttpCallbackListener.java
com.vjia.jokeking.HttpUtil.java
com.vjia.jokeking.Joke.java
com.vjia.jokeking.MainActivity.java
com.vjia.jokeking.MyAdapter.java
com.vjia.locationtest.MainActivity.java