2016-12-01 15 views
0

GPSを使用して電話位置を取得しようとしていますが、このコードを試しましたが、何も表示されません(シミュレータ上で実行しています)ここではそれも場所の変更方法を入力していないことを...GPSを使用して電話位置を取得

は、アクティビティコードです:

public class GPS_Service extends Service { 



    private LocationListener listener; 

    private LocationManager locationManager; 



    @Nullable 

    @Override 

    public IBinder onBind(Intent intent) { 

     return null; 

    } 



    @Override 

    public void onCreate() { 



     listener = new LocationListener() { 

      @Override 

      public void onLocationChanged(Location location) { 

       Intent i = new Intent("location_update"); 

       i.putExtra("coordinates",location.getLongitude()+" "+location.getLatitude()); 

       sendBroadcast(i); 

      } 



      @Override 

      public void onStatusChanged(String s, int i, Bundle bundle) { 



      } 



      @Override 

      public void onProviderEnabled(String s) { 



      } 



      @Override 

      public void onProviderDisabled(String s) { 

       Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 

       i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

       startActivity(i); 

      } 

     }; 



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



     //noinspection MissingPermission 

     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000,0,listener); 



    } 



    @Override 

    public void onDestroy() { 

     super.onDestroy(); 

     if(locationManager != null){ 

      //noinspection MissingPermission 

      locationManager.removeUpdates(listener); 

     } 

    } 

    } 

private BroadcastReceiver broadcastReceiver; 

@Override 
protected void onResume() { 
    super.onResume(); 
    if(broadcastReceiver==null) 
    { 

     broadcastReceiver = new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       Toast.makeText(getApplicationContext(),"cor",Toast.LENGTH_LONG).show(); 
      // Log.d("cors:",""+intent.getExtras().get("cordinates")); 
      } 
     }; 
    } 
    registerReceiver(broadcastReceiver,new IntentFilter("location_update")); 
} 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    if(broadcastReceiver!=null) 
     unregisterReceiver(broadcastReceiver); 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_settings); 
    Log.d("oncraet","oncreate"); 
    setSize(); 
    if(!runtime_permissions()) 
    { 
     enable_buttons(); 
    } 


} 
private boolean runtime_permissions() { 
    if (Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

     requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION} 
       ,100); 
     return true; 
    } 
    return false; 
} 

public void enable_buttons() 
{ 
    Intent i =new Intent(getApplicationContext(), GPS_Service.class); 
    startService(i); 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    if (requestCode == 100) { 

     if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) { 

      enable_buttons(); 

     } else { 

      runtime_permissions(); 

     } 
    } 
} 

、ここでは、サービスクラスのコードです

マニフェストはすべていいです。 何が問題ですか?最初に移動しなくてもアプリを最初の場所に移動させるにはどうすればよいですか?

答えて

0

ここに私の例を見

private LocationManager locationManager; 
private double lastKnownStartLatitude, lastKnownStartLongtitude; 

    locationManager = (LocationManager) getSystemService(serviceString); 


      String serviceString = Context.LOCATION_SERVICE; 
         locationManager = (LocationManager) getSystemService(serviceString); 

         String provider = LocationManager.GPS_PROVIDER; 
         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) { 
          return; 
         } 

      // if lastKnownLocation is not null 
         lastKnownLocation = locationManager.getLastKnownLocation(provider); 
         if (lastKnownLocation != null) { 
          lastKnownStartLatitude = lastKnownLocation.getLatitude(); 
          lastKnownStartLongtitude = lastKnownLocation.getLongitude(); 
         } 

       // if lastKnownLocation is null 
         LocationListener myLocationListener = new LocationListener() { 

          public void onLocationChanged(Location locationListener) { 


           Location location; 


          // using gps 
           if (isGPSEnabled(YourClassName.this)) { 
            if (locationListener != null) { 
             if (ActivityCompat.checkSelfPermission(YourClassName.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(TestWithLocationListener.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

              return; 
             } 
             System.out.println("Hello isGPSEnabled"); 
             if (locationManager != null) { 
              location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
              if (location != null) { 
               latitude = location.getLatitude(); 
               longitude = location.getLongitude(); 
              } 
             } 
            } 
           } else if (isInternetConnected(TestWithLocationListener.this)) { 
            if (locationManager != null) { 
             location = locationManager 
               .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
             if (location != null) { 
              latitude = location.getLatitude(); 
              longitude = location.getLongitude(); 
             } 
            } 
           } 

希望これは

0

は、今の場所を取得するための新たなベストプラクティスであるとして、Googleのfused location provider APIを使用してみてください役立ちます。

ロケーションのリクエストに応じて、すべてのソースから入手できる最適なロケーションのアップデートが入手できます。

This開発者向けサイトのチュートリアルは本当に良いです。

関連する問題