0
我試圖與IntentService但聽衆從來沒有被調用(在活動工作的罰款相同的代碼)如何在android中獲取前景的當前位置?
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);
問題是我需要的用戶可以關閉應用程序,並繼續保存位置(電池是不是有問題,該應用程序是雖然可以連接所有的時間)