Ich versuche GPS-Ort zu erhalten, im Hintergrund mit Service zu laufen. Wenn ich auf die Schaltfläche Start klicke, wird der GPS-Standort alle X Zeitintervalle an den Bildschirm gesendet und stoppt, wenn ich auf die Schaltfläche Stop klicke. Es funktionierte das erste Mal, als ich das Projekt ausführte, aber sobald ich die App geschlossen und wieder geöffnet habe, funktioniert es nicht mehr. Ich habe das Projekt gereinigt und erneut ausgeführt, und nichts wird angezeigt. Was vermisse ich? DieseGoogle GPS als Dienst funktioniert nur einmal
ist der Aktivitätscode
import android.Manifest;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.location.FusedLocationProviderApi;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.ConnectionResult;
import java.text.DateFormat;
import java.util.Date;
public class testLocationActivity extends AppCompatActivity
{
protected LocationHelper locationHelper;
protected TextView textView;
protected Button btn_start;
protected Button btn_stop;
protected BroadcastReceiver broadcastReceiver;
@Override
protected void onResume() {
super.onResume();
if(broadcastReceiver == null)
{
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
textView.append("\n" + intent.getExtras().get("coordinates"));
}
};
}
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_test_location);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Test Location");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
textView = (TextView) findViewById(R.id.textView);
btn_start = (Button) findViewById(R.id.button);
btn_stop = (Button) findViewById(R.id.button2);
if(!runtime_permissions())
{
enable_buttons();
}
}
private void enable_buttons()
{
btn_start.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), LocationHelper.class);
startService(intent);
}
});
btn_stop.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), LocationHelper.class);
stopService(intent);
}
});
}
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 true;
}
@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();
}
}
}
}
Dies ist die Klasse, die LocationHelper.java Service erweitert
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.util.Log;
import android.location.LocationListener;
import com.firebase.client.Firebase;
import com.google.firebase.auth.FirebaseAuth;
public class LocationHelper 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 intent = new Intent("location_update");
intent.putExtra("coordinates", location.getLongitude()+ " " + location.getLatitude());
sendBroadcast(intent);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider)
{
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
};
locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000,0, listener);
//locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,3000,0, listener);
}
@Override
public void onDestroy() {
super.onDestroy();
if(locationManager != null)
{
locationManager.removeUpdates(listener);
}
}
}
Jede Hilfe oder Zeiger geschätzt wird!