I have a service that I use for listening to a socket service. I only need this service to be running whilst the app is in the foreground. However, in production I am seeing several crashes:
IllegalStateException
Not allowed to start service Intent { cmp=com.app.app/.app.sockets.EventHandlingService }: app is in background
Initially, I was calling this in the onCreate of my Activity, and according to some other SO questions, technically the app isn't foregrounded then. So I moved it into the onResume, and yet I still see it happening. This is how it is started:
#Override
protected void onResume() {
super.onResume();
Intent serviceIntent = new Intent(this, EventHandlingService.class);
startService(serviceIntent);
bindService(serviceIntent, serviceConnection, 0);
}
I am unable to reproduce this crash locally.
Related
On my Samsung S9 with Android 8.1 (Oreo). I am trying to start an activity on my service's onTaskRemoved(), so I can reinitialize my notifications (WorkManager) and prevent the app from force closing (saw this solution somewhere on Stack Overflow but don't remember where :(, sorry). The code would occasionally work (the application does not disconnect from Android studio, and scheduled notifications are successfully received on time). However, the activity usually does not start. I tried, with no avail, to put startActivity() in a loop to retry upon failure. Why is the activity not starting?
I know for sure that both the service and activities are properly registered in the AndroidManifest. In addition, I know that onCreate is not being called in the activity, because the logger message in the onCreate is not printing.
public void onTaskRemoved(Intent rootIntent) {
Log.d("Logtag", "Application has been closed by user");
Intent intent = new Intent(this, LaunchActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
this.startActivity(intent);
}```
I am currently having a really weird issue, and I don't know what could be causing it. Every time I close the app by swiping it away, and then start it again, the app seems to be duplicating its output to logcat, or in other words, it appears to be running multiple times.
This doesn't happen when I am connected to Android Studio, but without a cable connection, it does it without fail.
In my main activity I start my service like this:
Intent intent = new Intent(this, BluetoothService.class);
startService(intent);
bindService(intent, bluetoothServiceConnection, Context.BIND_AUTO_CREATE);
Stopping the service:
unbindService(bluetoothServiceConnection);
Intent intent = new Intent(MainActivity.this, BluetoothService.class);
stopService(intent);
EDIT I have made some changes in the way I stop and start my service, which seems to have solved my problem on Android 5.1.1, but not on Android 4.4.4, sadly.
I was thinking that maybe my logging process could be the problem, but destroying that process in my activity's onDestroy()-method didn't solve the issue either.
Starting the logging process:
logger = Runtime.getRuntime().exec(new String[]{"logcat", "-v", "time", "-f", logcatFile.getAbsolutePath(), " *:E"});
Stopping it:
logger.destroy();
If you start a service, depends how you define your service in the AndroidManifest.xml, if you have android:process=":yourProcess", if the process name start with ":" it will create a new process, that probably your log output multiple times.
Your code doesn't contains any logging information, nor did you provide a stacktrace with it. Your splash screen doesn't seem to be the issue, but you should replace the new thread with a delayed post to a handler to the looper thread (main thread) you're running in:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
finish();
}
}, 1500);
Instead of getApplicationContext() you could also use SplashActivity.this directly.
In your MainActivity you then need to make sure to unbind the service. Maybe also stop it, depending on what you need it for. Refer to Bound Services for more documentation and examples.
For making the Service terminate the documentation states
A service can be both started and have connections bound to it. In such a case, the system will keep the service running as long as either it is started or there are one or more connections to it with the Context.BIND_AUTO_CREATE flag. Once neither of these situations hold, the service's onDestroy() method is called and the service is effectively terminated.
That's why it works when the service isn't started at all. You try to stop the service in onDestroy() which is only called if the service is stopped. Your code looks like the service doesn't need to be started at all. But to do it correctly, just stop the service in the acivity's onDestroy() or utilize
onUnbind() or any other useful lifecycle event of the activity or service.
I have an activity that has:
#Override
protected void onStart() {
super.onStart();
// Bind to LocalService
exampleService = new Intent(this, exampleService.class);
bindService(exampleService, mConnection, Context.BIND_AUTO_CREATE);
startService(exampleService);
}
Now If I go to Manage Applications and check running services and kill the service, the activity is still running as a background process.
How can I have it so the activity that is bound to the service is killed also on destroy?
This is happening because in android when you start a service then you need to manually stop that service before you kill bound any activity. So try to stop that service inside onStop() or Onpause() method.
Visit this http://androidtutorial4u.blogspot.in/p/forum.html
We have started new forum in which you can ask your questions regarding android as well as answer the questions.
I have the following code in each of the 5 activities of my app. I wanted to keep the service running that is bound to these five activities. It will play music in the background. However when the user navigates away from the any of these five activities the Service should be killed. Using the code below I am able to get it so that the music plays in the background when navigating between the activities. but the Service keeps running after leaving the application. What is the best way to solve this problem? How about some creative ideas.
I put this Toast message in the onDestroy method of the service so I can tell when the service is stopped.
Toast.makeText(this, "My Service Destroyed", Toast.LENGTH_LONG).show();
I never see any Toast message pop up when I leave the application. The other toast messages do show to indicate that the service has started.
#Override
public void onPause() {
super.onPause();
unbindService(serviceConnection);
}
#Override
public void onResume() {
//After a pause OR at startup
super.onResume();
//add this to the onResume of the activity
// startService(new Intent(this, AudioService.class));
bindService(new Intent(this, AudioService.class),
serviceConnection, Context.BIND_AUTO_CREATE);
}
According to the documentation here:
Multiple clients can bind to the service at once. When a client is done interacting with the service, it calls unbindService() to unbind. Once there are no clients bound to the service, the system destroys the service.
So maybe you're missing some unbind in one of your activities. You can check this by printing a log in the onBind and onUnbind methods of your service.
I'm developing an application which I need to start to background services when the application is started.
Initially I added the startup procedure of the first service on the Application.onCreate() method.
Afterward, I realize that I need to re-start the service if for any reason the user stopped the background thread. Therefore I moved service startup code from the Application.onCreate() to the main Activivity.onResume().
According to the documentation, calling startService() on a already running service has no problem.
Until then, everything was fine. My first background service was implemented just fine. When it starts, it creates a new thread to run the background processing without no major issue.
After implementing the first service, I moved to second one. No problems here. But after implementing it and added the service start call on the Activity.onResume() method.
private void startServices() {
startTrackingService();
startBacklogService();
}
private void startTrackingService() {
if (Z10.DEBUG) {
Log.i(LOG_TAG, "REQUESTING TRACKING SERVICE START");
}
Intent serviceLauncher = new Intent(Main.this, TrackingService.class);
startService(serviceLauncher);
if (Z10.DEBUG) {
Log.i(LOG_TAG, "TRACKING SERVICE START REQUESTED");
}
}
private void startBacklogService() {
if (Z10.DEBUG) {
Log.i(LOG_TAG, "REQUESTING BACKLOG SERVICE START");
}
Intent serviceLauncher = new Intent(Main.this, BacklogService.class);
startService(serviceLauncher);
if (Z10.DEBUG) {
Log.i(LOG_TAG, "BACKLOG SERVICE START REQUESTED");
}
}
This code, blocks the main method and the application must be killed.
Trying to debug the application (it is quite difficult without the android source code!!!), I realized that the startService does not really start the service, it simply adds the operation on the Looper queue to be processed.
If, I execute the two startService operation in sequence, the main thread blocks, but if after doing other stuff (UI-related) on the main thread and just then start the second service, it works just fine.
The problem is that I really would need to start both services on the application startup, or restart any of them on the Main activity resume.