2016-09-06 18 views
1

ボタン(mapbttn)をクリックすると、Googleマップアプリから5秒ごとに座標が送信されます。私はこれらの座標の送信を停止する別のボタンを作成したい。どうやってやるの?ここでGPS座標送信の停止Android

は私の.javaファイルです:

private GoogleMap mMap; 
    Button mapbttn; 
    private LocationManager locationManager; 
    private LocationListener locationListener; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 

     SupportMapFragment mapFragment = 
       (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 

     mapbttn = (Button) findViewById(R.id.setRangeButton); 

     mapbttn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       btncoord(); 
      } 
     }); 

     locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 

     locationListener = new LocationListener() { 
      @Override 
      public void onLocationChanged(Location location) { 
       Toast.makeText(getApplicationContext(), "" + location.getLatitude() + '\n' + location.getLongitude(), Toast.LENGTH_LONG).show(); 
      } 
      @Override 
      public void onStatusChanged(String s, int i, Bundle bundle) { 

      } 
      @Override 
      public void onProviderEnabled(String s) { 

      } 

      public void onProviderDisabled(String s) { 
       Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
       startActivity(intent); 
      } 
     }; 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 

      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) { 
       requestPermissions(new String[]{ 
         Manifest.permission.ACCESS_FINE_LOCATION, 
         Manifest.permission.ACCESS_COARSE_LOCATION, 
         Manifest.permission.INTERNET 
       }, 10); 
       return; 
      } else { 
       btncoord(); 
      } 
     } 
     Bundle save = getIntent().getExtras(); 

     if (save != null) color = save.getString("marker color"); 
    } 

    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
     switch (requestCode) { 
      case 10: 
       if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) 
        btncoord(); 
       return; 
     } 
    } 

    public void btncoord() { 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      return; 
     } 
     locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 5000, 0, locationListener); 
    } 

答えて

1

単に

locationManager.removeUpdates(ActivityName.this);

docs

+0

注:また、それ以外の場合は、 –

+0

感謝を止めるつもりはない、あなたの活動のあなたのonDestroyでこれを行う必要があり、それは動作します:)あなたはすでにここにある答えを投稿する理由 –

+0

私の喜びは –

1

のようなあなたの場所の管理者は、この

public void stopUpdates(){ 
    locationManager.removeUpdates(context); 
} 
を試してみてください登録解除

停止ボタンのstopUpdate()メソッドを呼び出します。

+0

を@AndreeaMateescu。多くの未回答の質問があります。倫理的です。 –

+0

@PavneetSinghあなたがそうであるように、1つの質問に複数の答えがあります。私が答えを書いていたとき、あなたはレースに勝った。そして私は著者の必要に応じて答えを詳述します。 – Amy

+0

あなたは他の非倫理的な例を取って倫理的になりたくないということを意味します。かなり面白いのであまり精緻化があります –

関連する問題