Android Open Source - MobHere Main Activity






From Project

Back to project page MobHere.

License

The source code is released under:

GNU General Public License

If you think the Android project MobHere 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 cl.mobiquos.mobhere;
//from w  ww  .j a  v a 2s  . co  m
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;



public class MainActivity extends Activity implements LocationListener {
  
  public Location dataLocation;
  public LocationManager locationManager;

  private final static String TAGNAME = "MainActivityHereIam";
  
  private TextView latitude;
  private TextView longitude;
  private TextView altitude;
  private TextView speed;
  private TextView provider;
  private TextView gps;
  private TextView accuracity;
  private ToggleButton toggle;
  
  private LinearLayout layout;
  //private List<HerePoint> values;  
  
  
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        
        setContentView(R.layout.activity_main);
        //ActionBar action_bar = getActionBar();
        
       // values = new ArrayList<HerePoint>();
        
        /*Widgets*/
        latitude = (TextView)findViewById(R.id.latitude);
      longitude = (TextView)findViewById(R.id.longitude);
      altitude = (TextView)findViewById(R.id.altitude);
      speed = (TextView)findViewById(R.id.speed);
      provider = (TextView)findViewById(R.id.provider);
      gps = (TextView)findViewById(R.id.gps);
      accuracity = (TextView)findViewById(R.id.accuracity);
             
      toggle = (ToggleButton)findViewById(R.id.toggleButtonOptions);
      layout = (LinearLayout)findViewById(R.id.additionalLayout);

      
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0,
                this);

        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            Toast.makeText(this,getResources().getString(R.string.gpsok)
                ,Toast.LENGTH_SHORT).show();
            gettingGPSLocation();
            
        }else{
            showGPSDisabled();
        }
        
        if (toggle.isChecked()) {
        this.layout.setVisibility(LinearLayout.VISIBLE);
      }
      else {
        this.layout.setVisibility(LinearLayout.GONE);
      }
        
    }
   
    public void gettingGPSLocation(){
      final ProgressDialog ringProgressDialog = ProgressDialog.show(MainActivity.this,
          getResources().getString(R.string.loading),
          getResources().getString(R.string.loading_gps), true);
      
              ringProgressDialog.setCancelable(true);
              new Thread(new Runnable() {
                  @Override
                  public void run() {
                      try {
                          // Here you should write your time consuming task...
                          // Let the progress ring for 10 seconds...
                          while (latitude.getText().toString().equals(getResources().getString(R.string.app_name))){
                            
                          }  
                          
                      } catch (Exception e) {
                      }
                      ringProgressDialog.dismiss();
                  }
              }).start();

    }
    
    public void showGPSDisabled(){
      AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setMessage(R.string.error_gps_disabled)
           .setTitle(R.string.title_alertdialog)
           .setPositiveButton(R.string.ok,new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog,int id) {
               Intent GPSSett = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
               startActivity(GPSSett);
             }
           })
           .setNegativeButton(R.string.cancel,new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog,int id) {
               Toast.makeText(MainActivity.this,getResources().getString(R.string.gpsoff)
                       ,Toast.LENGTH_SHORT).show();
             }
           });
    AlertDialog dialog = builder.create();
    dialog.show();

    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.menu_action_bar, menu);
      return true;
    }   
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      // action with ID action_refresh was selected
      case R.id.action_email:
        sendMail();
        break;
      // action with ID action_settings was selected
      case R.id.action_map:
        openUrlMap();
        break;
      case R.id.action_mobiquos:
        dialogMobiquos();
        break;
      case R.id.action_contact:
        contactMobiquos();
        break;
      default:
        break;
      }

      return true;
    } 
    public void shareContent(View view){
      Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
      sharingIntent.setType("text/plain");
      sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
      sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "GOLA");
      startActivity(sharingIntent);
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
      super.onSaveInstanceState(savedInstanceState);
      // Save UI state changes to the savedInstanceState.
      // This bundle will be passed to onCreate if the process is
      // killed and restarted.
      Log.i(TAGNAME, "In the onSaveInstanceState method");
      //savedInstanceState.putBoolean("togglebutton",this.toggle.isChecked());
    }
    
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
      super.onRestoreInstanceState(savedInstanceState);
      // Restore UI state from the savedInstanceState.
      // This bundle has also been passed to onCreate.
      
      Log.i(TAGNAME, "In the onRestoreInstanceState method");
      if ( savedInstanceState.getBoolean("togglebutton") == true ){
        toggle.setChecked(true);
      }
      else {
        toggle.setChecked(false);
      }
      
    }   
    
    /**
     * 
     * @param view
     */
    public void viewDetail(View view){
      if (toggle.isChecked()) {
        this.layout.setVisibility(LinearLayout.VISIBLE);
      }
      else {
        this.layout.setVisibility(LinearLayout.GONE);
      }
    }
    
    
    public void OpenWeb(View view){
      final Intent intent = new Intent(Intent.ACTION_VIEW)
        .setData(Uri.parse(getResources().getString(R.string.company_url)));
        startActivity(intent);
    }
    
    public void contactMobiquos(){

      
      Intent email = new Intent(Intent.ACTION_SEND);
      email.putExtra(Intent.EXTRA_EMAIL, new String[]{"info@mobiquos.cl"});      
      email.putExtra(Intent.EXTRA_SUBJECT, "Contacto desde - App");
      //-message/rfc822
      email.setType("plain/text");
      startActivity(Intent.createChooser(email, "Choose an Email client :"));
    }
    
    /**
     * 
     * @return String
     */
    private String createUrl(){
      String onlylat,onlylon;
      onlylat = latitude.getText().toString().replace(" ","");
      onlylon = longitude.getText().toString().replace(" ","");
      return "http://maps.google.com/maps?q=loc:"+ onlylat +"," + onlylon;
    }
    
    /**
     * 
     * @param view
     */
    public void openUrlMap(){
      
      String onlylat,onlylon;
      onlylat = latitude.getText().toString().replace(" ","");
      onlylon = longitude.getText().toString().replace(" ","");
      Intent navigation = new Intent(Intent.ACTION_VIEW, Uri
              .parse("http://maps.google.com/maps?q=loc:"
                  + onlylat +"," + onlylon));
      startActivity(navigation);
      
    }

    /**
     * 
     * @param view
     */
    public void sendMail(){
      String message;
      
      message = "Localizacion:";
      message = message + "Latitud:" + latitude.getText().toString() + "\n";
      message = message + "Longitud:" + longitude.getText().toString() + "\n";
      message = message + "Precision:" + accuracity.getText().toString() + "\n";
      message = message + "URL:"+createUrl();
      message = message + "\n--------------------------------------\n";
      
      Intent email = new Intent(Intent.ACTION_SEND);
      //email.putExtra(Intent.EXTRA_EMAIL, new String[]{"manuelmoscosod@gmail.com"});      
      email.putExtra(Intent.EXTRA_SUBJECT, "App: Aqui estoy");
      email.putExtra(Intent.EXTRA_TEXT,message);
      email.setType("message/rfc822");
      startActivity(Intent.createChooser(email, "Choose an Email client :"));
    }
   
    public void dialogMobiquos(){
      AlertDialog.Builder builder = new AlertDialog.Builder(this);

      LayoutInflater inflater = this.getLayoutInflater();

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        builder.setView(inflater.inflate(R.layout.mobiquos_layout, null));
    builder.setTitle(R.string.company_name)
           .setPositiveButton(R.string.ok,new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog,int id) {
             }
           });
    AlertDialog dialog = builder.create();
    dialog.show();
    }
    
    
  @Override
  public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    
    
    latitude.setText(String.format("%9.6f",location.getLatitude()));
      longitude.setText(String.format("%9.6f",location.getLongitude()));
      altitude.setText(String.format("%9.6f",location.getAltitude()));
      speed.setText(String.format("%9.6f",location.getSpeed()));
      provider.setText(location.getProvider());
      accuracity.setText(String.format("%9.6f",location.getAccuracy()));
      gps.setText(Integer.toString(location.getExtras().getInt("satellites")));
  }


  @Override
  public void onProviderDisabled(String arg0) {
    // 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
    Log.i(TAGNAME, "On Status Changed method");
  }
  
  @Override
  protected void onResume() {
      super.onResume();
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
              3000, 0, this);
      Log.i(TAGNAME, "On Resumen method");
 
  }
  
  @Override
  protected void onPause() {
      super.onPause();
      locationManager.removeUpdates(this);
      Log.i(TAGNAME, "On Pause method");
  }
  
  @Override
  protected void onDestroy() {
      super.onDestroy();
      locationManager.removeUpdates(this);
      Log.i(TAGNAME, "On Destroy method");
  }    
}




Java Source Code List

cl.mobiquos.mobhere.MainActivity.java
cl.mobiquos.mobhere.MobHereMainActivity.java