How to check whether TtsService (or an Android service) is stopped? - android

For reasons beyond my app's control, TtsService is stopped by the system, providing only Log.i() hints in LogCat:
05-01 12:01:55.662: INFO/TtsService(1791): Stopping
05-01 12:01:55.662: INFO/TtsService(1791): Stopped
I would like to be able to handle this situation from within my app.
Is there a way (a callback or a system call) to detect when this happens?
If not, is there a way to check whether the TTS service is running?

You can use following method to check whether service is running or not.
ServiceName = "package.service".\\service name
public static boolean isServiceRunning(Context ctx, String serviceName)
{
ActivityManager manager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceName.equalsIgnoreCase(service.service.getClassName())) {
Log.v(TAG,serviceName+": Service running");
return true;
}
}
return false;
}

Related

Activity Manager can't find my Service - why?

I have a service which returns START_STICKY on #onStartCommand() and it is being started in my MainActivity's #onCreate() method. When i close my app, Service is being destroyed and it starts itself again. However, when it starts itself, ActivityManager can't find it. So there are 2 instance of my service running..
I use this code to check is service dead, it returns true everytime even if it lives after service starts itself.
public static boolean isServiceDead(Context context) {
final ActivityManager am = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo runningServiceInfo : am.getRunningServices(Integer.MAX_VALUE))
if (runningServiceInfo.service.getClassName().equals(LockerService.class.getName()))
return false;
return true;
}

Controlling background service from another instance of an app

I made an application that fires a long-lived background service. Sometimes the application itself is closed, and the service keeps running, which is the way I want it.
The problem is that I don't know how to control that service after the caller application is closed. For example if the users re-opened the app, I'd like to give them the option whether to kill that process or keep it running.
I am able to detect if the service is running by using the following function (taken from this solution):
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service :manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
I tried then calling this line from the activity, but that didn't do anything:
if(isMyServiceRunning(MyService.class)) {
stopService(new Intent(this, MyService.class));
}

How to execute some event if RN has been killed?

Not sure if I understand correctly but according to this link
Deliver silent notifications and wake up your app in the background on the user's device.
It sounded to me that it's possible to perform some action even if the app has been killed.
Currently I'm using OneSignal as below:
OneSignal.addEventListener('received', this.onReceived);
onReceived(store, notification) {
store.dispatch(receivedNotification({ notification }));
}
However the above will only be able to dispatch action if the app is in background or foreground, but once the app been killed, despite receiving notification successfully, onReceived event will not be fired.
So my question is whether is it possible to "wake" my RN app in the background and dispatch a redux action?
There is no event that you can catch before app is killed. You also cannot prevent the user from killing your app. And once it is killed you cannot 'wake' it up, as your code is not running. You can only do such tasks when the app is in the background/foreground.
Yes, it is possible you can execute some event if RN has been killed.
now the question is how basically I am also using react-native and I have got some challenges also, I also want to execute some event if RN has been killed, but I did not get any answer actually my concern is to open an app which can and show a call screen when calling notification is received so I dig a lot and I found a solution,
first I created a bridge connection between javascript to java and then write a wakeful service which gets called every time when we receive notification and then I call my background intent service and in this service I wake up my activity and set some flags which help me to open screen when screen is lock depends on the conditions
// receiver Service //
public class MessagingService extends WakefulBroadcastReceiver {
private static final String TAG = "FirebaseService";
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, String.valueOf(!isAppOnForeground(context)));
if (intent.getExtras() != null) {
if (!isAppOnForeground((context))) {
//This get called every time you receive notification
}
}
}
private boolean isAppOnForeground(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses == null) {
return false;
}
final String packageName = context.getPackageName();
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {
return true;
}
}
return false;
}
}

How to implement a background service that will run when the app is closed and stop when the app is open

How to implement a background service that will run when the app is closed and stop when the app is open.I have a singleton websocket connection if app is closed i should be able to notify the user via notification if app is open then i should be able to update ui from single websocket communication.
onStop() of your MainActivity you can start the service, in onCreate() you can check if service is running, if it is running you can stop it.
public static boolean isServiceRunning(Class<?> serviceClass, Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
This method will tell you weather service is running or not

Android: check if my Service is running in the background

I am running a Service to get Notifications every 1 hour in my application.
The service definitely runs every 1 hr; I checked it using the Logcat.
But since I am starting this Service in the onResume() of my MainActivity, I want to check whether the Service is already running, and start it only if it's not running.
I used this code:
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("com.android.MyApp.MyServices.NotificationsService".equals(service.service.getClassName())) {
return true;
}
}
return false;
}
But I always get false.
So I checked what output service.service.getClassName() gives. And my Service is not there in the list.
Is it because my application is unknown? or something else?
I know its bit late but this answer might help others who are looking for the same.
Have you started your service by using context.startService()?
Make sure that the manifest file contains following:
<service android:name="com.android.MyApp.MyServices.NotificationsService" />
private boolean isMyServiceRunning(Context mContext) {
ActivityManager manager = (ActivityManager) mContext
.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager
.getRunningServices(Integer.MAX_VALUE)) {
if (**HERE_YOUR_SERVICE'S_CLASS_NAME**.class.getName().equals(
service.service.getClassName())) {
return true;
}
}
return false;
}
// i have background GPS service and i have successfully checked that my GPS service is either running or not.
// Happy coding...

Categories

Resources