Reputation: 1
This is my service, this service check a WiFi connections :
public class WiFiService extends Service {
private final static int INTERVAL = 1000 * 60 * 2;
// public Handler handler = null;
public static Runnable runnable = null;
final Handler handler = new Handler();
final long delay = 2 * 10 * 1000;
@Override
public void onCreate() {
super.onCreate();
executeSomething();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
handler.removeCallbacks(runnable);
Log.e("destroyed " , " kill ");
}
void executeSomething() {
handler.postDelayed(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "a" , Toast.LENGTH_LONG).show();
WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled()) {
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifiManager.getConnectionInfo();
String ssid = info.getSSID();
Toast.makeText(getApplicationContext(), ssid, Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext() , "Nie jest właczone" , Toast.LENGTH_LONG).show();
String ssid = "AndroidAP";
String key = "iawu7483";
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = String.format("\"%s\"", ssid);
wifiConfig.preSharedKey = String.format("\"%s\"", key);
WifiManager wifiManager = (WifiManager)getSystemService(WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
int netId = wifiManager.addNetwork(wifiConfig);
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();
}
executeSomething();
}
}, delay);
}
}
I started this service in class App extends Application
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
startService(new Intent(this, WiFiService.class));
}
}
But when I killed my application I see that this service worked all the time. How I can finish this service? And started one more time , when I run my application
Upvotes: 1
Views: 1448
Reputation: 1382
// You can destroy your service like this
@Override
public void onDestroy() {
super.onDestroy();
// stop location updates
if (mLocationRequest != null && mLocationClient != null) {
mLocationClient.removeLocationUpdates(mLocationCallback);
}
Log.e("Rajneesh", "Destroy ... ");
}
Upvotes: 0
Reputation: 2962
You can send an intent from the destroyed activity to the service to close it :
in your activity :
@Override
void onDestroy(){
super.onDestroy();
Intent intent = new Intent(this, YourService.class);
intent.setAction("STOP");
startService(intent);
}
in your service :
@Override
void onStartCommand(Intent intent, int flag, int id){
if(intent.getAction().equals("STOP")){
stopSelf();
}
// code..
}
Upvotes: 2
Reputation: 14173
This is working as intended, onDestroy()
is not guaranteed to be called. It will be called if the Service is explicitly stopped, but if the process is killed abruptly (it could happen for many reasons) it won't be called.
As it's been pointed out, you could override onTaskRemoved() in order to detect a swipe from recent activities, but this is not the only scenario in which your Service might be killed with no warning.
If you want your Service to be restarted you need to return START_STICKY
in int onStartCommand (Intent intent, int flags, int startId)
Upvotes: 1
Reputation: 316
When you kill your application the method onTaskRemoved() of Service is getting called.
You can stop your service task from that method. And you can again start your service from the launching activity.
I hope it helps. Thanks.
Upvotes: 2