Guest User

Untitled

a guest
Sep 26th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. LocationManager locmngr; // Location manager declaration
  2.  
  3. locationUpdate(); // Initializing the method in onCreate()
  4.  
  5. public void locationUpdate()
  6. {
  7. locmngr = (LocationManager) getSystemService(LOCATION_SERVICE);
  8. if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  9. ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION}, 12);
  10. return;
  11. }
  12.  
  13. boolean gps_enabled = false;
  14. boolean network_enabled = false;
  15.  
  16. try {
  17. gps_enabled = locmngr.isProviderEnabled(LocationManager.GPS_PROVIDER);
  18. } catch (Exception ex) {
  19. Toast.makeText(TabsActivity.this, ""+ex, Toast.LENGTH_SHORT).show();
  20. }
  21.  
  22. try {
  23. network_enabled = locmngr.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  24. } catch (Exception ex) {
  25. Toast.makeText(TabsActivity.this, ""+ex, Toast.LENGTH_SHORT).show();
  26. }
  27.  
  28. if (!gps_enabled && !network_enabled) {
  29. // notify user
  30. AlertDialog.Builder dialog = new AlertDialog.Builder(this);
  31. dialog.setMessage("GPS network not enabled");
  32. dialog.setPositiveButton("Open Location Settings", new DialogInterface.OnClickListener() {
  33. @Override
  34. public void onClick(DialogInterface paramDialogInterface, int paramInt) {
  35. // TODO Auto-generated method stub
  36. Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  37. startActivity(myIntent);
  38. //get gps
  39. }
  40. });
  41. dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  42.  
  43. @Override
  44. public void onClick(DialogInterface paramDialogInterface, int paramInt) {
  45. // TODO Auto-generated method stub
  46.  
  47. }
  48. });
  49. dialog.show();
  50. }
  51.  
  52. try {
  53. locmngr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 7, 30, new LocationListener() {
  54. @Override
  55. public void onLocationChanged(final Location location) {
  56.  
  57. // Add a marker at your current location and move the camera
  58. //Log.e("location", location.toString());
  59.  
  60. if(!user_id.isEmpty()){
  61.  
  62. // code to write lat longs into firestore
  63. }
  64.  
  65. }
  66.  
  67. @Override
  68. public void onStatusChanged(String provider, int status, Bundle extras) {
  69.  
  70. }
  71.  
  72. @Override
  73. public void onProviderEnabled(String provider) {
  74.  
  75. }
  76.  
  77. @Override
  78. public void onProviderDisabled(String provider) {
  79. ActivityCompat.requestPermissions(TabsActivity.this, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 12);
  80. }
  81. });
  82. } catch (Exception ex) {
  83. Toast.makeText(TabsActivity.this, ""+ex, Toast.LENGTH_SHORT).show();
  84. }
  85. }
  86.  
  87. private final Runnable m_Runnable = new Runnable()
  88. {
  89. public void run()
  90.  
  91. {
  92. try {
  93.  
  94. locationUpdate();
  95. TabsActivity.this.mHandler.postDelayed(m_Runnable, 1000);
  96.  
  97. }
  98. catch (Exception ex)
  99. {
  100. Toast.makeText(TabsActivity.this, ""+ex, Toast.LENGTH_SHORT).show();
  101. }
  102. }
  103.  
  104. };
Add Comment
Please, Sign In to add comment