Check if service is running not behaving right - android

I created a boolean to check if a service is running, if it is running I want it to show linearlayoutA and if it is not running I want it to show another linearlayoutB.
The problem is this, it shows the right linear layout when I start the activity, if the service is running it shows linearlayoutA if its not running it shows linearlayoutB but when I start the service in the activity it shows linearlayoutB and does not change to A even when the service as stopped until I close the application and open it. method for checking
public boolean isRunning(Class<? extends Service> serviceClass) {
final Intent intent = new Intent(TimerActivity.this, serviceClass);
return (PendingIntent.getService(TimerActivity.this, CODE, intent, PendingIntent.FLAG_NO_CREATE) != null);
}
This is how I call it in onCreate of the activity
if(isRunning(TimerLocationService.class)){
setWaitScreen(true);
}else{
setWaitScreen(false);
}

try this.
private Boolean isServiceRunning(String serviceName) {
ActivityManager activityManager = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo runningServiceInfo : activityManager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceName.equals(runningServiceInfo.service.getClassName())) {
return true;
}
}
return false;
}
I hope this is helpful for you.

Related

OnCreate in Application class called twice

I didn't use android:process in my AndroidManifest. When I launch app oncreate in Application class get called twice. I would like to know what could cause such behaviour.
Probably you call your service when you run your app.
Use this to prevent recalling the service several times:
Intent intent = new Intent(YourActivity.this, YourService.class);
if (!IsServiceRunning(YourService.class)) {
startService(intent);
}
Use this function to check whether the service is running or not:
public boolean IsServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(getApplicationContext().ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}

Push Notification when app is running

I am implementing push notification and so far it works fine. I manage to get push notification and when I click on that able to start activity.
But I don't want to notify user about notification if app is already running. This how I am planning to do this...but not sure is this correct way
Intent actIntent = new Intent(this, MainActivity.class);
actIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, actIntent, 0);
if (!isActivityRunning())
mNotificationManager.notify(0, notification);
public boolean isActivityRunning(Context ctx) {
ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (RunningTaskInfo task : tasks) {
if (ctx.getPackageName().equalsIgnoreCase(task.baseActivity.getPackageName()))
return true;
}
return false;
}
isActivityRunning function will basically checks whether MainActivity is running or not. If it is in running state then won't show notification and will pass information to activity itself to update UI. If activity is not running on click of notification MainActivity will open.
Is this right way of achieving this?
Using the activity manager doesn't exactly work as expected. The activity manager keeps a track of all the running apps on the phone. It doesn't really tell you whether the app is in foreground or background. To check whether the activity is running, set a boolean value in the onResume and onPause method of the activity.
Example:
public void onResume()
{
super.onResume();
isActivityRunning = true;
}
public void onPause()
{
super.onPause();
isActivityRunning = false;
}
You can then use the isActivityRunning to see if you want to throw the notification or not.
Also see this: Checking if an Android application is running in the background
I use this code to know if app is running:
private boolean isActivityRunning() {
if (MainActivity.getInstance() != null) {
return true;
} else {
return false;
}
}
And in MainActivity
public static MainActivity getInstance() {
return mInstance;
}
in onCreate I assign mInstance:
mInstance = this;
in onDestroy:
mInstance = null;
I use checking of mInstance because I use it in different activities for checking or for using some methods from MainActivity, so for me no need to create new boolean var.

Service getting called multiple times?

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

Toggle Service Activity

I'm developing a service that need to run foreground, and users can toggle it on/off through an activity. So basically, the activity MAY be killed, but the service is safe as long as it is not stopped by user.
However, I'm getting this trouble: how to turn off the service if it is on? That mean, if my activity was killed, then restarted, so I get no reference of the started service intent to call stopService.
Below is my current code. It works fine if the user call deactivate after the service is started by the same activity. The button status is always correct, but when my activity is restarted by the OS, this.serviceIntent is null.
protected boolean isServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (SynchronizationService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
#Override
protected void onResume() {
super.onResume();
this.togglingByCode = true;
this.butActivate.setChecked(this.isServiceRunning());
this.togglingByCode = false;
}
public void onActivateButtonClick(final boolean pIsChecked) {
if (this.togglingByCode) { return; }
if (pIsChecked) {
this.saveSettings();
this.serviceIntent = new Intent(this, SynchronizationService.class);
this.serviceIntent.putExtra(KEY_WEBSERVICE, this.txtWebService.getText().toString());
this.serviceIntent.putExtra(KEY_PASSWORD, this.txtPassword.getText().toString());
this.serviceIntent.putExtra(KEY_INTERVAL, Integer.parseInt(this.txtRefreshInterval.getText().toString()));
this.serviceIntent.putExtra(KEY_TIMEOUT, this.preferences.getInt(KEY_TIMEOUT, DEFAULT_TIMEOUT));
this.serviceIntent.putExtra(KEY_RETRY, this.preferences.getInt(KEY_RETRY, DEFAULT_RETRY));
this.startService(this.serviceIntent);
} else {
this.stopService(this.serviceIntent);
this.serviceIntent = null;
}
}
Please tell me how to stop the service correctly. Thank you.
P.s: I know a trick that make serviceIntent static, but I don't feel safe about it. If there is no any other way, then I will use it.
Simply initialize your serviceIntent in the activity's onCreate... You can start your service, kill your activity, reopen it and stop the service with the same serviceIntent.

Stopping service explicitly

I have an app, which contains two activities and a service and a reciever, service is used to get the location updates all the time, i will start the service in the reciever for the first time, my requirement is, i need to stop the service when my application is in foreground (running), and i need to start the service when i application is stopped. among two activities initActivity is the first activity that get launched when application starts, and the homegridActivity id the second activity, i am giving the option in homegridactivity to exit from the application. below is the code where i am starting the service and stoping the service.
class initActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app);
System.out.println("VANDROID service status inside oncreate of init" );
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("com.vaayoo.android.service.VLocationFetchService".equals(service.service.getClassName())) {
Intent intent = new Intent( VUiHelper.getInstance().getApplicationContext(), VLocationFetchService.class);
boolean result = ((Vandroid)VUiHelper.getInstance().getApplicationContext()).stopService(intent);
System.out.println("VANDROID service status" + result);
manager = null;
}
}
}
}
//second Activity
Class HomeGridActivity extends Activity
{
protected void onDestroy() {
super.onDestroy();
System.out.println("Homegrid activity onDestroy called");
VUiHelper.getInstance().clearControlCache();
ActivityManager manager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("com.vaayoo.android.service.VLocationFetchService".equals(service.service.getClassName())) {
Intent intent = new Intent( VUiHelper.getInstance().getApplicationContext(), VLocationFetchService.class);
ComponentName compname =((Vandroid) VUiHelper.getInstance().getApplicationContext()).startService(intent);
System.out.println("COMPONENT NAME" + compname.toString() );
manager = null;
}
}
}
}
I am stoping the service in oncreate on the initactivity and starting the same service in ondestroy of homegridActivity, the problem i am facing is, this is working only for the first time, if i close and launch the app multiple times, service is not stoping and starting, i found that, the service is running all the time. i have made that service as start_sticky so that is should not be killed by android runtime at any point of time, i should have full control on that service to stop and start. not able to find out why it is not working all the time, why it is working only for the first time. what i am doing wrong ? how to troubleshoot this? is it because of making service as start_sticky?
This method is return true or false means.when service is ruuning than reture true otherwise false. so you can use this method when r you use just like this.
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager
.getRunningServices(Integer.MAX_VALUE)) {
if ("packagename".equals(service.service
.getClassName())) {
return true;
}
}
return false;
}
and check like that.
if (isMyServiceRunning()) {
stopService(new Intent(A.this, MusicService.class));
}

Categories

Resources