Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks,
- GoogleApiClient.OnConnectionFailedListener,LocationListener{
- private GoogleMap mMap;
- private GoogleApiClient mGoogleApiClient;
- private LocationRequest mLocationRequest;
- private Marker mCurrLocationMarker;
- private TextView mLatitude, mLongitude;
- private Button mNextBtn;
- private Location location;
- private int mInterval = 5000;
- private static final String TAG = "BroadcastTest";
- private Intent intent;
- Handler mHandler;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_maps);
- mLatitude = (TextView) findViewById(R.id.tv_lat);
- mLongitude = (TextView) findViewById(R.id.tv_long);
- mNextBtn = (Button) findViewById(R.id.btn_next);
- intent = new Intent(this, BroadcastService.class);
- mNextBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent intent = new Intent(MapsActivity.this, LatLongListActivity.class);
- startActivity(intent);
- }
- });
- // Obtain the SupportMapFragment and get notified when the map is ready to be used.
- SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
- .findFragmentById(R.id.map);
- mapFragment.getMapAsync(this);
- startRepeatingTask();
- mHandler = new Handler();
- /*Thread t = new Thread() {
- @Override
- public void run() {
- try {
- while (!isInterrupted()) {
- Thread.sleep(10000);
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- // update TextView here!
- Toast.makeText(getApplicationContext(),"Update",Toast.LENGTH_SHORT).show();
- }
- });
- }
- } catch (InterruptedException e) {
- }
- }
- };
- t.start();*/
- }
- @Override
- public void onMapReady(GoogleMap googleMap) {
- mMap = googleMap;
- // Add a marker in Sydney and move the camera
- mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
- //Initialize Google Play Services
- if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- if (ContextCompat.checkSelfPermission(this,
- Manifest.permission.ACCESS_FINE_LOCATION)
- == PackageManager.PERMISSION_GRANTED) {
- buildGoogleApiClient();
- mMap.setMyLocationEnabled(true);
- }
- } else {
- buildGoogleApiClient();
- mMap.setMyLocationEnabled(true);
- }
- }
- Runnable mStatusChecker = new Runnable() {
- @Override
- public void run() {
- try {
- onLocationChanged(location);
- Toast.makeText(getApplicationContext(),"Update",Toast.LENGTH_SHORT).show();
- } finally {
- mHandler.postDelayed(mStatusChecker, mInterval); // set mInterval = 300000
- }
- }
- };
- void startRepeatingTask() {
- mStatusChecker.run();
- }
- void stopRepeatingTask() {
- mHandler.removeCallbacks(mStatusChecker);
- }
- public void onLocationChanged(Location location) {
- // called when the listener is notified with a location update from the GPS
- double currentLatitude = location.getLatitude();
- double currentLogitude = location.getLongitude();
- LatLng latLng = new LatLng(currentLatitude, currentLogitude);
- MarkerOptions markerOptions = new MarkerOptions();
- markerOptions.position(latLng);
- markerOptions.title("Current Position");
- markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
- mCurrLocationMarker = mMap.addMarker(markerOptions);
- //move map camera
- mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
- mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
- //move map camera
- mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
- mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
- //stop location updates
- if (mGoogleApiClient != null) {
- LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, (LocationListener) this);
- String lat = String.valueOf(location.getLatitude());
- String longi = String.valueOf(location.getLongitude());
- mLatitude.setText("Latitude : " + lat);
- mLongitude.setText("Logitude : " + longi);
- SharedPreferences sp = getSharedPreferences("key", 0);
- SharedPreferences.Editor sedt = sp.edit();
- sedt.putString("textvalue", mLatitude.getText().toString());
- sedt.putString("txtopertaive", mLongitude.getText().toString());
- sedt.commit();
- }
- }
- public void onProviderDisabled(String provider) {
- // called when the GPS provider is turned off (user turning off the GPS on the phone)
- }
- public void onProviderEnabled(String provider) {
- // called when the GPS provider is turned on (user turning on the GPS on the phone)
- }
- public void onStatusChanged(String provider, int status, Bundle extras) {
- // called when the status of the GPS provider changes
- }
- private void buildGoogleApiClient() {
- mGoogleApiClient = new GoogleApiClient.Builder(this)
- .addConnectionCallbacks(this)
- .addOnConnectionFailedListener(this)
- .addApi(LocationServices.API)
- .build();
- mGoogleApiClient.connect();
- }
- @Override
- public void onConnected(@Nullable Bundle bundle) {
- mLocationRequest = new LocationRequest();
- mLocationRequest.setInterval(1000);
- mLocationRequest.setFastestInterval(1000);
- mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
- if (ContextCompat.checkSelfPermission(this,
- Manifest.permission.ACCESS_FINE_LOCATION)
- == PackageManager.PERMISSION_GRANTED) {
- LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest,this);
- }
- }
- @Override
- public void onConnectionSuspended(int i) {
- }
- @Override
- public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
- }
- public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
- public boolean checkLocationPermission(){
- if (ContextCompat.checkSelfPermission(this,
- Manifest.permission.ACCESS_FINE_LOCATION)
- != PackageManager.PERMISSION_GRANTED) {
- // Asking user if explanation is needed
- if (ActivityCompat.shouldShowRequestPermissionRationale(this,
- Manifest.permission.ACCESS_FINE_LOCATION)) {
- // Show an expanation to the user *asynchronously* -- don't block
- // this thread waiting for the user's response! After the user
- // sees the explanation, try again to request the permission.
- //Prompt the user once explanation has been shown
- ActivityCompat.requestPermissions(this,
- new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
- MY_PERMISSIONS_REQUEST_LOCATION);
- } else {
- // No explanation needed, we can request the permission.
- ActivityCompat.requestPermissions(this,
- new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
- MY_PERMISSIONS_REQUEST_LOCATION);
- }
- return false;
- } else {
- return true;
- }
- }
- Runnable mStatusChecker = new Runnable() {
- @Override
- public void run() {
- try {
- <call you location fetching method here || simply call location.get... methods here>
- } finally {
- mHandler.postDelayed(mStatusChecker, mInterval); // set mInterval = 300000
- }
- }
- };
- void startRepeatingTask() {
- mStatusChecker.run();
- }
- void stopRepeatingTask() {
- mHandler.removeCallbacks(mStatusChecker);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement