I have a service
Manifest
<service android:name=".SocketService" android:process=":socket"/>
I connect to it via bindService
and use AIDL
The service is designed to exchange data with the server and the application. The service has no intent filters.
public final class SocketService extends Service { @Override public IBinder onBind(Intent intent) { return new Binder(); } private final class Binder extends ISocketService.Stub { @Override public void on(String event, ISocketEmitterListener listener) throws RemoteException { ... } @Override public int emit(String event, byte[] bytes) throws RemoteException { ... return 0; } } @Override public boolean onUnbind(Intent intent) { boolean stopSelf = DBManager.getBoolean(DBManager.SOCKET_SERVICE_STOP_SELF); if (stopSelf) { stopSelf(); DBManager.save(DBManager.SOCKET_SERVICE_STOP_SELF, false); } Timber.i("SocketService unbinded!"); return super.onUnbind(intent); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Timber.i("SocketService started!"); ... return START_STICKY; } @Override public void onTaskRemoved(Intent rootIntent) { stopSelf(); Timber.i("SocketService task removed!"); super.onTaskRemoved(rootIntent); } @Override public void onDestroy() { DBManager.save(DBManager.SOCKET_SERVICE_IS_RUNNING, false); Timber.i("SocketService destroyed!"); } }
My problem is that when an application receives any error and crashes, or simply closes incorrectly, for example, it is unloaded by the user through the task manager, then the next launch of the service is no longer possible! To do this, you need to reinstall the application, otherwise it writes this: ActivityManager: Unable to start service Intent { cmp=package.name/.SocketService} U=0: not found
How to get rid of this WITHOUT APPLICATION RESET ?
I have already tried many ways: 1) Redefine Thread.UncaughtExceptionHandler
and make there an undind
and stopService
service before System.exit(1)
2) Ovveride onTaskRemoved()
with stopSelf()
inside 3) Itd ...
Now I have made such a method for checking, so that if anything to inform the user that he must reinstall the application because of this error.
public static boolean checkOnNonNull(Context context, Class<?> clazz) { Intent service = new Intent(context, clazz); ResolveInfo rInfo = context.getPackageManager().resolveService(service, PackageManager.GET_SHARED_LIBRARY_FILES); ServiceInfo sInfo = rInfo != null ? rInfo.serviceInfo : null; if (sInfo == null) { Timber.e("Unable to start service " + service + ": not found"); return false; } return true; }
UPD I did some tests and it’s as if the record actually disappears from the manifest, because I started adding numbers to the class name and the service started working with a new name. But all the old names were not found.
<service android:name=".SocketService+[1-10]" android:process=":socket"/> // Затем я подставлял цифры в обратном порядке и .SocketService9 будучи использованным один раз уже not found.
android:process=":socket"
)? Looking at en-SO for this problem - the reasons may be the car. So give more details. The service is inside the application or outside, the manifest does not hurt to show (at least in general terms) how and where the service call occurs? - woesss