Advertisement
luchfilip

LocateMe Source Code

Aug 7th, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.98 KB | None | 0 0
  1. MainActivity.java
  2.  
  3. package com.example.locateme;
  4.  
  5. import android.os.Bundle;
  6. import android.app.Activity;
  7. import android.content.Intent;
  8. import android.view.Menu;
  9. import android.view.MenuInflater;
  10. import android.view.MenuItem;
  11.  
  12. public class MainActivity extends Activity {
  13.  
  14.     @Override
  15.     protected void onCreate(Bundle savedInstanceState) {
  16.         super.onCreate(savedInstanceState);
  17.         setContentView(R.layout.activity_main);
  18.     }
  19.  
  20.     @Override
  21.     public boolean onCreateOptionsMenu(Menu menu) {
  22.         // Inflate the menu; this adds items to the action bar if it is present.
  23.         MenuInflater inflater = getMenuInflater();
  24.         inflater.inflate(R.menu.main, menu);
  25.         return super.onCreateOptionsMenu(menu);
  26.     }
  27.     @Override
  28.     public boolean onOptionsItemSelected(MenuItem item) {
  29.         // Handle presses on the action bar items
  30.         switch (item.getItemId()) {
  31.             case R.id.action_photo:
  32.                 openPhoto();
  33.                 return true;
  34.             case R.id.action_video:
  35.                 openVideo();
  36.                 return true;
  37.             case R.id.action_map:
  38.                 Intent intent = new Intent(this, GPSTracker.class);
  39.                 startActivity(intent);
  40.                 return true;
  41.             default:
  42.                 return super.onOptionsItemSelected(item);
  43.         }
  44.     }
  45.  
  46.     private void openVideo() {
  47.         // TODO Auto-generated method stub
  48.        
  49.     }
  50.  
  51.     private void openPhoto() {
  52.         // TODO Auto-generated method stub
  53.        
  54.     }
  55.  
  56.  
  57. }
  58.  
  59.  
  60. activity_main.xml
  61.  
  62. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  63.     xmlns:tools="http://schemas.android.com/tools"
  64.     android:layout_width="match_parent"
  65.     android:layout_height="match_parent"
  66.     android:paddingBottom="@dimen/activity_vertical_margin"
  67.     android:paddingLeft="@dimen/activity_horizontal_margin"
  68.     android:paddingRight="@dimen/activity_horizontal_margin"
  69.     android:paddingTop="@dimen/activity_vertical_margin"
  70.     tools:context=".MainActivity" >
  71.  
  72.     <TextView
  73.         android:layout_width="wrap_content"
  74.         android:layout_height="wrap_content"
  75.         android:text="@string/hello_world" />
  76.  
  77. </RelativeLayout>
  78.  
  79.  
  80.  
  81. Manifest
  82.  
  83.  
  84.  
  85. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  86.     package="com.example.locateme"
  87.     android:versionCode="1"
  88.     android:versionName="1.0" >
  89.  
  90.     <uses-sdk
  91.         android:minSdkVersion="8"
  92.         android:targetSdkVersion="17" />
  93. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION">
  94. </uses-permission>
  95.     <application
  96.         android:allowBackup="true"
  97.         android:icon="@drawable/ic_launcher"
  98.         android:label="@string/app_name"
  99.         android:theme="@style/AppTheme" >
  100.         <activity
  101.             android:name="com.example.locateme.MainActivity"
  102.             android:label="@string/app_name" >
  103.             <intent-filter>
  104.                 <action android:name="android.intent.action.MAIN" />
  105.  
  106.                 <category android:name="android.intent.category.LAUNCHER" />
  107.             </intent-filter>
  108.            
  109.            
  110.         </activity>
  111.          <activity
  112.             android:name="com.example.locateme.GPSTracker"
  113.             android:label="@string/app_name" >
  114.             <intent-filter>
  115.                 <action android:name="android.intent.action.MAIN" />
  116.                 <category android:name="android.intent.category.LAUNCHER" />
  117.             </intent-filter>
  118.             </activity>
  119.     </application>
  120.  
  121. </manifest>
  122.  
  123. menu/main.xml
  124.  
  125. <menu xmlns:android="http://schemas.android.com/apk/res/android" >
  126.     <item android:id="@+id/action_attach"
  127.           android:title="@string/actiont_attach"
  128.           android:showAsAction="ifRoom"
  129.           >
  130.           <menu>
  131.               <item
  132.                   android:id="@+id/action_photo"
  133.                   android:title="@string/actiont_photo"
  134.                   />
  135.               <item
  136.                   android:id="@+id/action_video"
  137.                   android:title="@string/actiont_video"
  138.                   />
  139.               <item
  140.                   android:id="@+id/action_map"
  141.                   android:title="@string/actiont_map"
  142.                   />
  143.           </menu>
  144.         </item>
  145.    
  146. </menu>
  147.  
  148. GPSTracker.java
  149.  
  150. package com.example.locateme;
  151.  
  152. import android.location.Location;
  153. import android.location.LocationListener;
  154. import android.location.LocationManager;
  155. import android.os.Bundle;
  156. import android.provider.Settings;
  157. import android.app.Activity;
  158. import android.app.AlertDialog;
  159. import android.content.Context;
  160. import android.content.DialogInterface;
  161. import android.content.Intent;
  162. import android.util.Log;
  163. import android.view.Menu;
  164.  
  165. public class GPSTracker extends Activity implements LocationListener {
  166.    
  167.     @Override
  168.     protected void onCreate(Bundle savedInstanceState) {
  169.         super.onCreate(savedInstanceState);
  170.         setContentView(R.layout.activity_gpstracker);
  171.     }
  172.  
  173.  
  174. private final Context mContext;
  175.  
  176. // flag for GPS status
  177. public boolean isGPSEnabled = false;
  178.  
  179. // flag for network status
  180. boolean isNetworkEnabled = false;
  181.  
  182. // flag for GPS status
  183. boolean canGetLocation = false;
  184.  
  185. Location location; // location
  186. double latitude; // latitude
  187. double longitude; // longitude
  188.  
  189. // The minimum distance to change Updates in meters
  190. private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
  191.  
  192. // The minimum time between updates in milliseconds
  193. private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
  194.  
  195. // Declaring a Location Manager
  196. protected LocationManager locationManager;
  197.  
  198. public GPSTracker(Context context) {
  199.     this.mContext = context;
  200.     getLocation();
  201. }
  202.  
  203. /**
  204.  * Function to get the user's current location
  205.  * @return
  206.  */
  207. public Location getLocation() {
  208.     try {
  209.         locationManager = (LocationManager) mContext
  210.                 .getSystemService(Context.LOCATION_SERVICE);
  211.  
  212.         // getting GPS status
  213.         isGPSEnabled = locationManager
  214.                 .isProviderEnabled(LocationManager.GPS_PROVIDER);
  215.  
  216.         Log.v("isGPSEnabled", "=" + isGPSEnabled);
  217.  
  218.         // getting network status
  219.         isNetworkEnabled = locationManager
  220.                 .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  221.  
  222.         Log.v("isNetworkEnabled", "=" + isNetworkEnabled);
  223.  
  224.         if (isGPSEnabled == false && isNetworkEnabled == false) {
  225.             // no network provider is enabled
  226.         } else {
  227.             this.canGetLocation = true;
  228.             if (isNetworkEnabled) {
  229.                 locationManager.requestLocationUpdates(
  230.                         LocationManager.NETWORK_PROVIDER,
  231.                         MIN_TIME_BW_UPDATES,
  232.                         MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
  233.                 Log.d("Network", "Network");
  234.                 if (locationManager != null) {
  235.                     location = locationManager
  236.                             .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  237.                     if (location != null) {
  238.                         latitude = location.getLatitude();
  239.                         longitude = location.getLongitude();
  240.                     }
  241.                 }
  242.             }
  243.             // if GPS Enabled get lat/long using GPS Services
  244.             if (isGPSEnabled) {
  245.                 if (location == null) {
  246.                     locationManager.requestLocationUpdates(
  247.                             LocationManager.GPS_PROVIDER,
  248.                             MIN_TIME_BW_UPDATES,
  249.                             MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
  250.                     Log.d("GPS Enabled", "GPS Enabled");
  251.                     if (locationManager != null) {
  252.                         location = locationManager
  253.                                 .getLastKnownLocation(LocationManager.GPS_PROVIDER);
  254.                         if (location != null) {
  255.                             latitude = location.getLatitude();
  256.                             longitude = location.getLongitude();
  257.                         }
  258.                     }
  259.                 }
  260.             }
  261.         }
  262.  
  263.     } catch (Exception e) {
  264.         e.printStackTrace();
  265.     }
  266.  
  267.     return location;
  268. }
  269.  
  270. /**
  271.  * Stop using GPS listener Calling this function will stop using GPS in your
  272.  * app
  273.  * */
  274. public void stopUsingGPS() {
  275.     if (locationManager != null) {
  276.         locationManager.removeUpdates(GPSTracker.this);
  277.     }
  278. }
  279.  
  280. /**
  281.  * Function to get latitude
  282.  * */
  283. public double getLatitude() {
  284.     if (location != null) {
  285.         latitude = location.getLatitude();
  286.     }
  287.  
  288.     // return latitude
  289.     return latitude;
  290. }
  291.  
  292. /**
  293.  * Function to get longitude
  294.  * */
  295. public double getLongitude() {
  296.     if (location != null) {
  297.         longitude = location.getLongitude();
  298.     }
  299.  
  300.     // return longitude
  301.     return longitude;
  302. }
  303.  
  304. /**
  305.  * Function to check GPS/wifi enabled
  306.  *
  307.  * @return boolean
  308.  * */
  309. public boolean canGetLocation() {
  310.     return this.canGetLocation;
  311. }
  312.  
  313. /**
  314.  * Function to show settings alert dialog On pressing Settings button will
  315.  * lauch Settings Options
  316.  * */
  317. public void showSettingsAlert() {
  318.     AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
  319.  
  320.     // Setting Dialog Title
  321.     alertDialog.setTitle("GPS is settings");
  322.  
  323.     // Setting Dialog Message
  324.     alertDialog
  325.             .setMessage("GPS is not enabled. Do you want to go to settings menu?");
  326.  
  327.     // On pressing Settings button
  328.     alertDialog.setPositiveButton("Settings",
  329.             new DialogInterface.OnClickListener() {
  330.                 public void onClick(DialogInterface dialog, int which) {
  331.                     Intent intent = new Intent(
  332.                             Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  333.                     mContext.startActivity(intent);
  334.                 }
  335.             });
  336.  
  337.     // on pressing cancel button
  338.     alertDialog.setNegativeButton("Cancel",
  339.             new DialogInterface.OnClickListener() {
  340.                 public void onClick(DialogInterface dialog, int which) {
  341.                     dialog.cancel();
  342.                 }
  343.             });
  344.  
  345.     // Showing Alert Message
  346.     alertDialog.show();
  347. }
  348.  
  349. @Override
  350. public void onLocationChanged(Location location) {
  351. }
  352.  
  353. @Override
  354. public void onProviderDisabled(String provider) {
  355. }
  356.  
  357. @Override
  358. public void onProviderEnabled(String provider) {
  359. }
  360.  
  361. @Override
  362. public void onStatusChanged(String provider, int status, Bundle extras) {
  363.     // TODO Auto-generated method stub
  364.    
  365. }
  366.  
  367.  }
  368.  
  369.  
  370. activity_gpstracker.xml
  371.  
  372. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  373.     xmlns:tools="http://schemas.android.com/tools"
  374.     android:layout_width="match_parent"
  375.     android:layout_height="match_parent"
  376.     android:paddingBottom="@dimen/activity_vertical_margin"
  377.     android:paddingLeft="@dimen/activity_horizontal_margin"
  378.     android:paddingRight="@dimen/activity_horizontal_margin"
  379.     android:paddingTop="@dimen/activity_vertical_margin"
  380.     tools:context=".GPSTracker" >
  381.  
  382.     <TextView
  383.         android:layout_width="wrap_content"
  384.         android:layout_height="wrap_content"
  385.         android:text="@string/hello_world" />
  386.  
  387. </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement