Is there a way to programmatically check if an Android device is running Google Services? I have an application that utilizes C2DM and want to disable menu options including this if the device (such as Kindle Fire and Nook) do not have the Google Services required.
public static boolean doesContainGsfPackage(Context context) {
PackageManager pm = context.getPackageManager();
List<PackageInfo> list = pm.getInstalledPackages(0);
for (PackageInfo pi : list) {
if(pi.packageName.equals(ACCUWX.GSF_PACKAGE)) return true; // ACCUWX.GSF_PACKAGE = com.google.android.gsf
}
return false;
}
I am not sure exactly what "Google Services" is however in the past I have found this function reliable when testing if a service is running. By replacing "some.package.name.MyService" with the Google Services package name then you should be able to check if it is running or not.
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("some.package.name.MyService".equals(service.service.getClassName())) {
return true;
}
}
return false;
}
Related
I want to check whether my service is running in or not,
I was using below code which was working fine till nougat but not working in OREO
private boolean isMyServiceRunning(Class<?> serviceClass, Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
Log.d(TAG, "isMyServiceRunning: " + service.service.getClassName());
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
But above code is not working for foreground services.
I have referred this but it is also not working..
The ActivityManager.getRunningServices() API has been deprecated since API 26 and is no longer available as of Oreo. It was intended to only be used for debugging, not production apps. You should not rely on this type of API for your app.
I have two applications. One just contains Service and from another app I start this Service app. Like,
Intent intent = new Intent();
intent.setClassName(context.getString(R.string.package_location_service),
context.getString(R.string.package_location_service) + ".GetLocationService"); // package name and class name
serviceIntent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
startService(serviceIntent);
But I want to check whether that Service is already running or not! If it's a service inside the same app then I can check whether it's running or not by
public static boolean isServiceRunning(String serviceClassName){
final ActivityManager activityManager = (ActivityManager)Application.getContext().getSystemService(Context.ACTIVITY_SERVICE);
final List<RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);
for (RunningServiceInfo runningServiceInfo : services) {
if (runningServiceInfo.service.getClassName().equals(serviceClassName)){
return true;
}
}
return false;
}
But how to check in my scenario? Any help?
I am developing an application where I am running some background operations and displaying notification to the user when certain condition meets .When the user clicks on the notification it should take him to the main activity , with out restarting the service .
My problem is when I am clicking the notification it takes me to the main activity and starting my service again.I dont want to restart my service again .How can I achieve this .
I have used the following code to check weather a service running or not lease look at it
public static boolean isServiceRunning(String serviceName,Context context){
boolean serviceRunning = false;
ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> l = am.getRunningServices(50);
Iterator<ActivityManager.RunningServiceInfo> i = l.iterator();
while (i.hasNext()) {
ActivityManager.RunningServiceInfo runningServiceInfo = (ActivityManager.RunningServiceInfo) i
.next();
if(runningServiceInfo.service.getClassName().equals(serviceName)){
serviceRunning = true;
}
}
return serviceRunning;
}
I have used this piece of code and it's working fine :)
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE))
{
if (MyService.class.getName().equals(service.service.getClassName()))
{
found = true;
}
}
you could check getIntent() on the activity and put some extra on the intent bundle of the notification that you check to start or not the service.
Note:
if a service is already running, it is not restarted.
Finally I have rectified my code.as follows
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("com.example.sevicedemosolution.Myservice".equals(service.service.getClassName().toString())) {
return true;
}
}
return false;
}
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...
I have 3 services running in the background in my application. How do I check if they are running or not.
You can iterate through all services and check if one of them matches name of your service.
private boolean isSomeOfMyServicesRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("com.domain.myapplication".equals(service.service.getClassName()))
return true;
}
return false;
Use a static field in your service to toggle a boolean flag. Then you can check that flag by binding the service to your activities.