У меня есть код для создания фонового сервиса, который будет работать всегда.
этот сервис будет работать после уничтожения приложения.
Я тестирую более 20 примеров кода, и этот код работает в симуляторе, но не работает в моем телефонезефир android 6.
Это служебный файл для моего приложения
public class locationService extends Service implements LocationListener {
private LocationManager _locMgr;
com.parse.groupbox.tools.locationTools loc;
Random RND = new Random();
Timer timer = new Timer();
@Override
public void onCreate() {
super.onCreate();
loc = new com.parse.groupbox.tools.locationTools(this);
}
@Override
public void onRebind(Intent intent) {
super.onRebind(intent);
try {
gpsSetup();
timer.cancel();
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
updateGps();
}
}, 5000, 10);
} catch (Exception ex) {
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
try {
gpsSetup();
timer.schedule(new TimerTask() {
@Override
public void run() {
updateGps();
}
}, 5000, 10);
} catch (Exception ex) {
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_SHORT).show();
}
Toast.makeText(this, "start", Toast.LENGTH_SHORT).show();
return Service.START_STICKY;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
try {
gpsSetup();
timer.cancel();
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
updateGps();
}
}, 5000, 10);
} catch (Exception ex) {
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_SHORT).show();
}
Toast.makeText(this, "task service start", Toast.LENGTH_SHORT).show();
}
@Override
public void onDestroy() {
super.onDestroy();
try {
gpsSetup();
timer.cancel();
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
updateGps();
}
}, 5000, 10);
} catch (Exception ex) {
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_SHORT).show();
}
Toast.makeText(this, "service destroyed", Toast.LENGTH_SHORT).show();
}
public void gpsSetup() {
try {
_locMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria locationCriteria = new Criteria();
locationCriteria.setAccuracy(Criteria.ACCURACY_FINE);
locationCriteria.setPowerRequirement(Criteria.POWER_MEDIUM);
String locationProvider = _locMgr.getBestProvider(locationCriteria, true);
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
_locMgr.requestLocationUpdates(locationProvider, 1, 1, this);
Toast.makeText(this, "start service", Toast.LENGTH_SHORT).show();
}
catch (Exception Ex)
{
Toast.makeText(this, Ex.getMessage(), Toast.LENGTH_LONG).show();
}
}
public void updateGps(){
try{
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
String locationProvider = LocationManager.NETWORK_PROVIDER;
_locMgr.requestLocationUpdates(locationProvider, 1, 1, this);
}catch (Exception Ex){
Toast.makeText(this, Ex.getMessage(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onLocationChanged(Location location) {
int result = RND.nextInt(100);
if(result > 30){
try{
loc.LocationSyncToServer(location);
Toast.makeText(this, "location", Toast.LENGTH_LONG).show();
}catch (Exception Ex){
Toast.makeText(this, Ex.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
Пожалуйста, прочитайте мои коды и дайте мне ответ, чтобы их решить.
Это код для запуска службы:
Intent background = new Intent(getApplicationContext(), com.parse.groupbox.services.locationService.class);
startService(background);