我尝试过使用IntentService,但是从不调用侦听器(活动中的相同代码可以进行罚款)
public class ISSubProcesoGps extends IntentService {
// TODO: Rename actions, choose action names that describe tasks that this
// IntentService can perform, e.g. ACTION_FETCH_NEW_ITEMS
public static int hacambiado = 0;
LocationManager lm;
double longitude;
double latitude;
Location location = null;
Criteria crit = new Criteria();
public static volatile boolean shouldContinue = true;
int contador = 0;
// TODO: Rename parameters
public ISSubProcesoGps() {
super("ISSubProcesoGps");
}
/**
* Starts this service to perform action Foo with the given parameters. If
* the service is already performing a task this action will be queued.
*
* @see IntentService
*/
@Override
protected void onHandleIntent(Intent mIntent) {
LocationManager lm;
double longitude1;
double latitude1;
Location location = null;
lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LocationListener listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
latitude=location.getLatitude();
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
String x =s;
}
};
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener );
latitude = 0;
longitude = 0;
Intent intent = new Intent(getBaseContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_menu_gallery) // the status icon
.setTicker("hola muy myintentservice") // the status text
.setWhen(System.currentTimeMillis()) // the time stamp
.setContentTitle("content title") // the label of the entry
.setContentText("content teext") // the contents of the entry
.setContentIntent(pendIntent) // The intent to send when the entry is clicked
.build();
startForeground(1, notification);
while(true)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
此代码位于一个按钮中(在一个片段中,它可以正常工作)
LocationManager lm;
Location location = null;
lm = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LocationListener listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
latitude=location.getLatitude();
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
String x =s;
}
};
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener );
问题是我需要用户可以关闭应用程序并继续保存位置(电池不是问题,应用程序虽然一直在连接)
答案 0 :(得分:0)
请勿使用IntentService
,使用Service
并为Instance variable
创建LocationListener
,因为LocationListener需要时间来安顿并开始收听位置。