当我杀死我的应用程序时,Java android服务并没有被破坏
这是我的服务,此服务检查WiFi连接:当我杀死我的应用程序时,Java android服务并没有被破坏,java,android,Java,Android,这是我的服务,此服务检查WiFi连接: 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();
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);
}
}
我在类应用程序扩展应用程序中启动了此服务
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
startService(new Intent(this, WiFiService.class));
}
}
但是当我终止我的应用程序时,我发现这个服务一直在工作。我怎样才能完成这项服务?并再次启动,当我运行我的应用程序时当您终止应用程序时,将调用服务的方法onTaskRemoved() 您可以通过该方法停止服务任务。 您可以再次从启动活动启动您的服务 我希望有帮助。
谢谢。当您终止应用程序时,会调用服务的方法onTaskRemoved() 您可以通过该方法停止服务任务。 您可以再次从启动活动启动您的服务 我希望有帮助。
谢谢。这是按预期工作的,
onDestroy()
不保证被调用。如果服务被显式停止,它将被调用,但是如果进程被突然终止(可能由于许多原因而发生),它将不会被调用
如前所述,您可以覆盖onTaskRemoved()以检测最近活动中的刷卡行为,但这并不是您的服务可能在没有警告的情况下被终止的唯一情况
如果希望重新启动服务,则需要在int中返回
START\u STICKY
,这是正常工作的,onDestroy()
不保证调用。如果服务被显式停止,它将被调用,但是如果进程被突然终止(可能由于许多原因而发生),它将不会被调用
如前所述,您可以覆盖onTaskRemoved()以检测最近活动中的刷卡行为,但这并不是您的服务可能在没有警告的情况下被终止的唯一情况
如果希望重新启动服务,则需要在int中返回
START\u STICKY
,您可以将已销毁活动的意图发送给服务以关闭它:
在您的活动中:
@Override
void onDestroy(){
super.onDestroy();
Intent intent = new Intent(this, YourService.class);
intent.setAction("STOP");
startService(intent);
}
为您服务:
@Override
void onStartCommand(Intent intent, int flag, int id){
if(intent.getAction().equals("STOP")){
stopSelf();
}
// code..
}
您可以将销毁活动的意图发送到服务以关闭它: 在您的活动中:
@Override
void onDestroy(){
super.onDestroy();
Intent intent = new Intent(this, YourService.class);
intent.setAction("STOP");
startService(intent);
}
为您服务:
@Override
void onStartCommand(Intent intent, int flag, int id){
if(intent.getAction().equals("STOP")){
stopSelf();
}
// code..
}
//你可以这样破坏你的服务
@Override
public void onDestroy() {
super.onDestroy();
// stop location updates
if (mLocationRequest != null && mLocationClient != null) {
mLocationClient.removeLocationUpdates(mLocationCallback);
}
Log.e("Rajneesh", "Destroy ... ");
}
//你可以这样破坏你的服务
@Override
public void onDestroy() {
super.onDestroy();
// stop location updates
if (mLocationRequest != null && mLocationClient != null) {
mLocationClient.removeLocationUpdates(mLocationCallback);
}
Log.e("Rajneesh", "Destroy ... ");
}
在活动的
onStop()
或onDestroy()
块中编写停止服务器的逻辑。这里是活动,而不是手动启动服务-绑定到它。检查此问题:@RafalMalek我不明白我必须做什么?在活动的onStop()
或onDestroy()
块中编写停止服务器的逻辑。这里是活动,而不是手动启动服务-绑定到它。检查这个问题:@RafalMalek我不明白我必须做什么?我怎么能做到?我怎么能做到?