我有一项用于将套接字连接到服务器的服务,我想在我的应用终止时重新启动该服务。
重新启动服务在android 5上工作正常,但在奥利奥(Oreo)上不工作。
我尝试了很多方法来做到这一点,例如:boardCastReceivers和更改服务的清单设置,但不起作用
我的代码: 在清单中
<service
android:name="com.jeen.jeen.service.ChatService"
android:enabled="true" >
</service>
<receiver
android:name="com.jeen.jeen.service.ChatReceiver"
android:enabled="true"
android:exported="true"
android:label="RestartServiceWhenStopped"
>
</receiver>
in MainActivity:
@Override
protected void onDestroy() {
stopService(mServiceIntent);
Log.i("MAINACT", "onDestroy!");
super.onDestroy();
}
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
Log.i ("isMyServiceRunning?", true+"");
return true;
}
}
Log.i ("isMyServiceRunning?", false+"");
return false;
}
ChatService chatService;
Intent mServiceIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
chatService = new ChatService(MainActivity.this);
mServiceIntent = new Intent(MainActivity.this, chatService.getClass());
if (!isMyServiceRunning(chatService.getClass())) {
startService(mServiceIntent);
}
setContentView(R.layout.activity_main);
}
receiver class:
public class ChatReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(ChatReceiver.class.getSimpleName(), "Service fucked up");
context.startService(new Intent(context, ChatService.class));
}
}
and service class:
public class ChatService extends Service{
public static Socket socket;
@Override
public void onCreate() {
super.onCreate();
Log.e("onCreate", "onCreate");
try {
socket = IO.socket("http://192.168.174.1:8000");
} catch (URISyntaxException e) {
e.printStackTrace();
}
// Toast.makeText(AppController.getInstance(),"Created",Toast.LENGTH_SHORT).show();
}
public ChatService(Context applicationContext) {
super();
Log.i("HERE", "here I am!");
}
public ChatService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
onTaskRemoved(intent);
}
super.onStartCommand(intent, flags, startId);
Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
if (!socket.connected()) {
socket.connect();
}
}
});
}
},
0,
5000);
return START_STICKY;
}
@Override
public void onDestroy() {
Log.i("EXIT", "ondestroy!");
super.onDestroy();
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
Intent restartServiceIntent = new Intent(getApplicationContext(), ChatService.class);
restartServiceIntent.setPackage(getPackageName());
PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmService.set(
AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + 4000,
restartServicePendingIntent);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
反正这样做吗?
答案 0 :(得分:0)
如果您使用的是vivo / honor / oppo等,则在您的应用关闭后,您的服务将被终止。