Tuesday, March 2, 2010

Android Indicates whether the specified service is already started

/**
* Indicates whether the specified service is already started.
* This method queries the activity manager for launched services that
* can respond to an binding with an specific service name.
* If no existed service is found, this method returns null.
*
* @param context The context of the activity
* @param className The service full name to check for availability.
*
* @return ComponentName if the service is already existed, NULL otherwise.
*/
public static ComponentName isServiceExisted(Context context, String className)
{
ActivityManager activityManager = (ActivityManager)context.getSystemService(ACTIVITY_SERVICE);
List serviceList =activityManager.getRunningServices(Integer.MAX_VALUE);

if(!(serviceList.size() > 0))
{
return null;
}

for(int i = 0; i < serviceList.size(); i++)
{
RunningServiceInfo serviceInfo = serviceList.get(i);
ComponentName serviceName = serviceInfo.service;

if(serviceName.getClassName().equals(className))
{
return serviceName;
}
}

return null;
}

No comments: