当我终止使用task killer
投放的所有应用(包括我的应用)时,该服务会长时间显示Restarting
。
我该如何改进?
最好的情况就是,只要应用程序/服务被杀死,服务就会立即出现,或者在最轻微的延迟时间内出现。
WLANSrvice.java
public class WLANService extends Service {
String username, password, ssid, url;
private static final String CREDENTIALS = "Credentials";
private final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences sharedPreferences = getSharedPreferences(CREDENTIALS, 0);
if(sharedPreferences.contains("username")) {
username = sharedPreferences.getString("username", "UNDEFINED");
}
if(sharedPreferences.contains("password")) {
password = sharedPreferences.getString("password", "UNDEFINED");
}
if(sharedPreferences.contains("ssid")) {
ssid = sharedPreferences.getString("ssid", "UNDEFINED");
}
if(sharedPreferences.contains("url")) {
url = sharedPreferences.getString("url", "UNDEFINED");
}
NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
boolean connected = info.isConnected();
if(connected) {
Toast.makeText(context, "WIFI CONNECTED!", Toast.LENGTH_LONG).show();
Log.i("Wi-Fi-State", "Wi-Fi is On!");
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if(wifiInfo.getSSID().contains(ssid) == true) {
try {
String output = new Connection().execute().get().toString();
Log.i("LoginState", new Connection().execute().get().toString());
if(output.contains("Address")) {
Toast.makeText(WLANService.this, "Login Success!", Toast.LENGTH_SHORT).show();
Intent account_info_intent = new Intent(WLANService.this, AccountInfo.class);
account_info_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(account_info_intent);
}else {
if(output.contains("n/a")) {
Toast.makeText(WLANService.this, "Login Failed!", Toast.LENGTH_SHORT).show();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
} else {
Toast.makeText(context, "WIFI DISCONNECTED!", Toast.LENGTH_SHORT).show();
//Log.i("Wi-Fi-State", "Wi-Fi is Off!");
}
}
};
public WLANService() {
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Auto-Login Enabled!", Toast.LENGTH_SHORT).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// registering your receiver
registerReceiver(receiver, new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION));
return START_STICKY;
}
@Override
public void onDestroy() {
Toast.makeText(this, "Auto-Login Disabled!", Toast.LENGTH_SHORT).show();
unregisterReceiver(receiver);
super.onDestroy();
}
private class Connection extends AsyncTask {
@Override
protected Object doInBackground(Object[] objects) {
String formatted_url = url.replace("http://", "");
String true_url;
if(formatted_url.charAt((formatted_url.length()-1)) != '/') {
true_url = formatted_url.concat("/");
}else {
true_url = formatted_url;
}
Log.i("formatted_url", formatted_url);
Log.i("true_url", true_url);
return LoginHelper.doLogin(username, password, "http://".concat(true_url));
}
}
}