Service getting called multiple times? - android

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;
}

Related

Android - How to check my app is open when receive a push-notification?

I need best solution. When I received a push notification, I want to know if my app is open or not, because if my application is open when the user onClick the notification I want to call specific method, but if the application is close I want to open the application.
Basically what you need is way to check if one of your activity is in Foreground. May be you should check this How to determine if one of my activities is in the foreground
//Use this method to check your app is running or not
public boolean isAppRunning(){
String packageName="Your package name";
ActivityManager activityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
List<ActivityManager.RunningAppProcessInfo> procInfoslist = activityManager.getRunningAppProcesses();
for(int i = 0; i < procInfoslist.size(); i++)
{
if(procInfoslist.get(i).processName.equals(packageName))
{
return true;
}
}
return false;
}
`
//Use this method to check any activity is running or not
public boolean isActivityRunning(Context ctx) {
ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (ActivityManager.RunningTaskInfo task : tasks) {
if (ctx.getPackageName().equalsIgnoreCase(task.baseActivity.getPackageName()))
return true;
}
return false;
}

android how to check if service is running

I have an app that gets GPS co-ordinates. It works fine but i want to test from within the calling activity if the service is running. I have the following code that i thought would check but it is returning false when the GPS icon in the notification bar is still flashing. Has anyone any ideas why? Thanks in advance
private boolean isGpsServiceRunning() {
ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("LocationService".equals(service.service.getClassName())) {
return true;
}
}
return false;
}
.
LocalBroadcastManager.getInstance(getApplicationContext())
.registerReceiver(
locationChangereceiver,
new IntentFilter(
LocationChangeIntent.ACTION_LOCATION_CHANGE));
startService(new Intent(this, LocationService.class));
You should compare against LocationService.class.getName(), instead of using a hard coded value. Also, the value that you are using corresponds to the Class#getSimpleName() which is probably not what you want here.
public boolean check(){
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE))
{
if ("com.example.yourpackagename.YourserviceName"
.equals(service.service.getClassName()))
{
return true;
}
}
return false;
}
this method works fine for me and is easy to use:
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
Log.i(TAG,"MyService.class.getName() = "+ButtonOnBootService.class.getName());
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
Log.i(TAG,"service.getClassName() = " +service.service.getClassName());
if (ButtonOnBootService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}

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...

How do we know if a service has stopped in a bound and unbound service?

How do we know if a service has stopped in a bound and unbound service?
I want to check constantly if a service has stopped and what is its state/status?
Thanks
Sneha
try this :
public boolean isServiceRunning(String serviceName) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<RunningServiceInfo> infos = manager.getRunningServices(30);
for (RunningServiceInfo info : infos) {
if (info.service.getClassName().equals(serviceName)) {
return true;
}
}
return false;
}

Android check for running services in application

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.

Categories

Resources