我正在尝试使用 Service 让 GPS 位置在后台运行。当我单击“开始”按钮时,GPS 位置每隔 X 时间间隔发送到屏幕,并在单击“停止”按钮时停止。我第一次运行项目时它工作了,但是一旦我关闭应用程序并再次打开它,它就不再工作了。我已经清理了项目并再次运行它,但没有显示任何内容。我错过了什么?
这是活动代码
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();
}
}
}
}
这是扩展 Service 的 LocationHelper.java 类
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);
}
}
}
任何帮助或指针表示赞赏!