Guest User

Untitled

a guest
Dec 17th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.21 KB | None | 0 0
  1. public class MainActivity extends Activity {
  2. Button btn_start;
  3. private static final int REQUEST_PERMISSIONS = 100;
  4. boolean boolean_permission;
  5. TextView tv_latitude, tv_longitude, tv_address,tv_area,tv_locality;
  6. SharedPreferences mPref;
  7. SharedPreferences.Editor medit;
  8. Double latitude,longitude;
  9. Geocoder geocoder;
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. btn_start = (Button) findViewById(R.id.btn_start);
  15. tv_address = (TextView) findViewById(R.id.tv_address);
  16. tv_latitude = (TextView) findViewById(R.id.tv_latitude);
  17. tv_longitude = (TextView) findViewById(R.id.tv_longitude);
  18. tv_area = (TextView)findViewById(R.id.tv_area);
  19. tv_locality = (TextView)findViewById(R.id.tv_locality);
  20. geocoder = new Geocoder(this, Locale.getDefault());
  21. mPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  22. medit = mPref.edit();
  23.  
  24.  
  25. btn_start.setOnClickListener(new View.OnClickListener() {
  26. @Override
  27. public void onClick(View v) {
  28. if (boolean_permission) {
  29.  
  30. if (mPref.getString("service", "").matches("")) {
  31. medit.putString("service", "service").commit();
  32.  
  33. Intent intent = new Intent(getApplicationContext(), GoogleService.class);
  34. startService(intent);
  35.  
  36. } else {
  37. Toast.makeText(getApplicationContext(), "Service is already running", Toast.LENGTH_SHORT).show();
  38. }
  39. } else {
  40. Toast.makeText(getApplicationContext(), "Please enable the gps", Toast.LENGTH_SHORT).show();
  41. }
  42.  
  43. }
  44. });
  45.  
  46. fn_permission();
  47. }
  48.  
  49. private void fn_permission() {
  50. if ((ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
  51.  
  52. if ((ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION))) {
  53.  
  54.  
  55. } else {
  56. ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION
  57.  
  58. },
  59. REQUEST_PERMISSIONS);
  60.  
  61. }
  62. } else {
  63. boolean_permission = true;
  64. }
  65. }
  66.  
  67. @Override
  68. public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
  69. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  70.  
  71. switch (requestCode) {
  72. case REQUEST_PERMISSIONS: {
  73. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  74. boolean_permission = true;
  75.  
  76. } else {
  77. Toast.makeText(getApplicationContext(), "Please allow the permission", Toast.LENGTH_LONG).show();
  78.  
  79. }
  80. }
  81. }
  82. }
  83.  
  84. private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
  85. @Override
  86. public void onReceive(Context context, Intent intent) {
  87.  
  88. latitude = Double.valueOf(intent.getStringExtra("latitude"));
  89. longitude = Double.valueOf(intent.getStringExtra("longitude"));
  90.  
  91. List<Address> addresses = null;
  92.  
  93. try {
  94. addresses = geocoder.getFromLocation(latitude, longitude, 1);
  95. String cityName = addresses.get(0).getAddressLine(0);
  96. String stateName = addresses.get(0).getAddressLine(1);
  97. String countryName = addresses.get(0).getAddressLine(2);
  98.  
  99. tv_area.setText(addresses.get(0).getAdminArea());
  100. tv_locality.setText(stateName);
  101. tv_address.setText(countryName);
  102.  
  103.  
  104.  
  105. } catch (IOException e1) {
  106. e1.printStackTrace();
  107. }
  108.  
  109.  
  110. tv_latitude.setText(latitude+"");
  111. tv_longitude.setText(longitude+"");
  112. tv_address.getText();
  113.  
  114.  
  115. }
  116. };
  117.  
  118. @Override
  119. protected void onResume() {
  120. super.onResume();
  121. registerReceiver(broadcastReceiver, new IntentFilter(GoogleService.str_receiver));
  122.  
  123. }
  124.  
  125. @Override
  126. protected void onPause() {
  127. super.onPause();
  128. unregisterReceiver(broadcastReceiver);
  129. }
  130.  
  131.  
  132. }
  133.  
  134. public class GoogleService extends Service implements LocationListener{
  135.  
  136. boolean isGPSEnable = false;
  137. boolean isNetworkEnable = false;
  138. double latitude,longitude;
  139. LocationManager locationManager;
  140. Location location;
  141. private Handler mHandler = new Handler();
  142. private Timer mTimer = null;
  143. long notify_interval = 1000;
  144. public static String str_receiver =
  145. "com.findmyelderly.findmyelderly.receiver";
  146. Intent intent;
  147.  
  148.  
  149.  
  150.  
  151. public GoogleService() {
  152.  
  153. }
  154.  
  155. @Nullable
  156. @Override
  157. public IBinder onBind(Intent intent) {
  158. return null;
  159. }
  160.  
  161. @Override
  162. public void onCreate() {
  163. super.onCreate();
  164.  
  165. mTimer = new Timer();
  166. mTimer.schedule(new TimerTaskToGetLocation(),5,notify_interval);
  167. intent = new Intent(str_receiver);
  168. // fn_getlocation();
  169. }
  170.  
  171. @Override
  172. public void onLocationChanged(Location location) {
  173.  
  174. }
  175.  
  176. @Override
  177. public void onStatusChanged(String provider, int status, Bundle extras) {
  178.  
  179. }
  180.  
  181. @Override
  182. public void onProviderEnabled(String provider) {
  183.  
  184. }
  185.  
  186. @Override
  187. public void onProviderDisabled(String provider) {
  188.  
  189. }
  190.  
  191. private void fn_getlocation() {
  192. locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
  193. isGPSEnable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
  194. isNetworkEnable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  195.  
  196. if (!isGPSEnable && !isNetworkEnable) {
  197.  
  198. } else {
  199.  
  200. if (isNetworkEnable) {
  201. location = null;
  202. try {
  203. locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, this);
  204. if (locationManager != null) {
  205. location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  206. if (location != null) {
  207.  
  208. Log.e("latitude", location.getLatitude() + "");
  209. Log.e("longitude", location.getLongitude() + "");
  210.  
  211. latitude = location.getLatitude();
  212. longitude = location.getLongitude();
  213. fn_update(location);
  214. }
  215. }
  216. } catch (SecurityException e) {
  217. e.printStackTrace();
  218. }
  219. }
  220.  
  221.  
  222. if (isGPSEnable) {
  223. location = null;
  224. try {
  225. locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
  226. if (locationManager != null) {
  227. location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  228. if (location != null) {
  229. Log.e("latitude", location.getLatitude() + "");
  230. Log.e("longitude", location.getLongitude() + "");
  231. latitude = location.getLatitude();
  232. longitude = location.getLongitude();
  233. fn_update(location);
  234. }
  235. }
  236. } catch (SecurityException e) {
  237. e.printStackTrace();
  238. }
  239. }
  240.  
  241.  
  242. }
  243.  
  244. }
  245.  
  246. private class TimerTaskToGetLocation extends TimerTask {
  247. @Override
  248. public void run() {
  249.  
  250. mHandler.post(new Runnable() {
  251. @Override
  252. public void run() {
  253. fn_getlocation();
  254. }
  255. });
  256.  
  257. }
  258. }
  259.  
  260.  
  261. private void fn_update(Location location){
  262.  
  263. intent.putExtra("latutide",location.getLatitude()+"");
  264. intent.putExtra("longitude",location.getLongitude()+"");
  265. sendBroadcast(intent);
  266. }
  267.  
  268.  
  269. }
  270.  
  271. .........
  272. compile 'com.android.support:appcompat-v7:25.0.0'
  273. compile 'com.google.android.gms:play-services-location:10.0.1'
  274. compile 'com.android.support:multidex:1.0.0'
  275.  
  276. ..........
  277.  
  278. <uses-permission android:name="android.permission.INTERNET"></uses-permission>
  279. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
  280. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
  281.  
  282. .........
  283.  
  284. <service android:name=".GoogleService" android:exported="false" android:enabled="true"/>
  285. </application>
  286.  
  287. </manifest>
Add Comment
Please, Sign In to add comment