OnCreate in Application class called twice - android

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

Related

check service is running from an activity class

I want to know whether the following method will work or not, I've got this method in stack-overflow itself but it doesn't seem to work.
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 have a Job Service, which will be executed every 15 minutes for this the I am using Job scheduler to trigger the service with whatever time is mentioned.
I have a method as executeService() as follows:
public JobScheduler initJob(Context context){
if(jobScheduler == null){
jobScheduler = (JobScheduler)context.getSystemService(JOB_SCHEDULER_SERVICE);
}
return jobScheduler;
}
public void executeService(){
initJob(this);
ComponentName jobService = new ComponentName(this.getPackageName(), OfflineService.class.getName());
JobInfo jobInfo = new JobInfo.Builder(2,jobService).
setPersisted(true).
setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY).
setPeriodic(millisecondsQueue).
build();
int jobId = jobScheduler.schedule(jobInfo);
}
Now this executeService() is called every time the user comes to MainActivity, onResume of MainActivity I've put a check as below
if(!isMyServiceRunning(OfflineService.class)){
executeService();
}
If my service is already triggered then after I come back to MainActivity the method isMyServiceRunning() returns false always.
I've gone through all the questions-answers related to this but I ended up writing this question again I am using Android OS Nougat.Any help would be great.

Check if service is running not behaving right

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.

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

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

How to check if an IntentService was started

I'd like to get to know if an Activity successfully started an IntentService.
As it's possible to bind an IntentService via bindService() to keep it running, perhaps an approach would be to check if invoking startService(intent) results in a call of onStartCommand(..) or onHandleIntent(..) in the service object.
But how can I check that in the Activity?
Here's the method I use to check if my service is running. The Sercive class is DroidUptimeService.
private boolean isServiceRunning() {
ActivityManager activityManager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> serviceList = activityManager.getRunningServices(Integer.MAX_VALUE);
if (serviceList.size() <= 0) {
return false;
}
for (int i = 0; i < serviceList.size(); i++) {
RunningServiceInfo serviceInfo = serviceList.get(i);
ComponentName serviceName = serviceInfo.service;
if (serviceName.getClassName().equals(DroidUptimeService.class.getName())) {
return true;
}
}
return false;
}
You can add a flag when constructing the PendingIntent, if the returned value was null, your service is not started. The mentioned flag is PendingIntent.FLAG_NO_CREATE.
Intent intent = new Intent(yourContext,YourService.class);
PendingIntent pendingIntent = PendingIntent.getService(yourContext,0,intent,PendingIntent.FLAG_NO_CREATE);
if (pendingIntent == null){
return "service is not created yet";
} else {
return "service is already running!";
}
I'd like to get to know if an Activity successfully started an IntentService.
If you don't get an exception in either the activity or the service when you call startService(), then the IntentService was started.
As it's possible to bind an IntentService via bindService() to keep it running
Why?
Here's the method I use to check if my service is running:
public static boolean isMyServiceRunning(Class<?> serviceClass, Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return service.started;
}
}
return false;
}

Categories

Resources