Android Open Source - MultiLocation Main Activity






From Project

Back to project page MultiLocation.

License

The source code is released under:

Apache License

If you think the Android project MultiLocation 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.example.owner;
//  w ww . j  a  v a  2s. c o m
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.TextView;

public class MainActivity extends Activity {

  private TextView tvLocation,tvLocationMethod,tvLocationState;
  
  private int locationType;
  private double latitude = 0;
  private double longitude = 0;
  private String method = "???";
  private String state = "??????";
  
  private LocationServiceProvider lsp = null;
  private LagLng lagLng = null;
  
  private ProgressDialog pd = null;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    tvLocation = (TextView)findViewById(R.id.location);
    tvLocationMethod = (TextView)findViewById(R.id.location_method);
    tvLocationState = (TextView)findViewById(R.id.location_state);
    pd = new ProgressDialog(this);
    pd.setMessage("??????");
    pd.setCancelable(true);
    
    lsp = new LocationServiceProvider(this);
  }

  @Override
  protected void onResume() {
    super.onResume();
    if(checkNetWorkState()){
      pd.show();
      getLocation();
    }else{
      showSettingNetwork(this);
      getLocation();
    }
  }
  
  private void updateUI() {
    tvLocation.setText("?????("+latitude+","+longitude+")");
    tvLocationMethod.setText("??????("+method+")");
    tvLocationState.setText("?????("+state+")");
  }
  

  private void getLocation(){
    lagLng = lsp.getCurrentLocation();
    locationType = lagLng.getLocationServiceType();
    state = "??????";
    latitude = lagLng.getLatitude();
    longitude = lagLng.getLongitude();
    switch (locationType) {
    case 1:
      method = "GPS";      
      break;
    case 2:
      method = "WIFI";
      break;
    case 3:
      method = "???????";
      break;
    default:
      method = "???";
      break;
    }
    updateUI();
    pd.dismiss();
          
  }
    
  private  boolean checkNetWorkState(){
    boolean flag = false;
    try {
      // ?????????????????????????wi-fi??????????
      ConnectivityManager connectivity = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
      if (connectivity != null) {
        // ??????????????????
        NetworkInfo info = connectivity.getActiveNetworkInfo();
        if (info != null && info.isConnected()) {
          // ??????????????????
          if (info.getState() == NetworkInfo.State.CONNECTED)
          {
            flag = true;
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    
    return flag;
  }
  
  public void showSettingNetwork(final Context context){
    new AlertDialog.Builder(context)
    .setTitle("???")
    .setMessage("?????????????????????????????????????????")
    .setPositiveButton("???", new OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
        startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
      }
    })
    .setNegativeButton("?????????", null)
    .create().show();
  }
  
}




Java Source Code List

com.example.owner.GPSLocation.java
com.example.owner.LagLng.java
com.example.owner.LocationBase.java
com.example.owner.LocationServiceProvider.java
com.example.owner.LocationService.java
com.example.owner.MainActivity.java
com.example.owner.NetworkLocation.java