I start a timer in activity1. And in the timer task, I run some code continuously for a long time. Then I sometimes need to start activity2 like below.
Intent intent = new Intent();
intent.setClass(Tomato.this, History.class);
startActivity(intent);
But then error occurs:
FATAL EXCEPTION: timer-1
Could anyone help?
Further:
I tested again, and found that this error didn't occur every time. Now I cannot even repeat it.
Thanks for all of you!
"I run some code continuously for a long time"
This is usually when you decide that this code should run in a service. Check out the Service SDK documentation and you'll get an example of local Service.
If the timer is just something you need to access accross activities, you could implement a singleton that will hold one instance of it.
Related
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 was playing around with services and dialogs, and I got a doubt. Within a dialog, I am starting a service like this:
Intent lock = new Intent(getActivity(),AppLockService.class);
getActivity().stopService(lock);
getActivity().startService(lock);
Now the first time I call the dialog through
dialog_name.show(getFragmentManager(), "dropbox");
Upon pressing the OK button, the intent is launched. Now later, during the same app execution, the dialog is triggered again ( which is according to my code logic -- nothing wrong here). The code in the dialog then stops the previously triggered intent and starts the new intent.
My question is this:
lock is a local intent variable as per my definition. So how does it know that it has to stop that particular service I have triggered here the first time? Would someone please explain this to me?
You don't have to keep track of the service in a variable because Android does it for you.
The way that the OS treats a service is that it will not allow more than one instance of the service be to running at any time.
So at any moment there are 0 or 1 instances of your service. If there are 0, no problem, the OS will ignore the call to StopService. If there is 1 instance, it must be the instance you started previously - so it will be stop that one.
Application Cycle :
I have a mainActivity, which starts a service, bind with it, and finish() after X seconds, the bind should be broken but the service should keep running.
The service with a specific trigger will start the MainActivity again (startService will not be called , i check if the service already exists).
So on the first time mainActivity finishes, the warning doesn't appear, while on the second time and on it does.
I tried removing all relevant 'binding' calls so no bind will be made and the warning still appears, it leaves us with a single option : is it because i start the service and not stopping it ? if so , how should I do it without getting this message ?
I start the Service using the following code :
Intent intent = new Intent(getApplicationContext(), LockerService.class);
intent.addCategory("LockerServiceTag");
startService(intent);
The stop is from another activity and not relevant since its not the problem for sure.
I DONT want to stop service when exiting.
Any insights why this warning message happens ?
I 'm writing a backgroud app only can use service.
I'use BroadcastReceiver start service. Sometimes it works well, but some sometimes it doesn't work.
I know the reason is BroadcastReceiver exec liftcycle is 10 seconds, so use it start a long time task will not be end.
Intent i = new Intent();
i.setClass(context, RegisterService.class);
context.startService(i);
What should I do if I want to execute a long time task only use BroadcastReceiver and service?
Make sure your service is not executing on the main thread. Consider using an IntentService, it does the right thing out of the box.
Im trying to make an scheduled activity go off every hour or so, all working in the background.
Right now i have a BroadcastReceiver that picks up when the device is booted.
The BroadcastReceiver creates a PendingIntent to an activity (Called AlarmController) that creates has all necessary methods that i need for making the scheduled activity to go off.
How ever, this doesnt seem to work.
This is how my BroadcastReciever class onReceive{} looks like and is indentical to my main activity onCreate{}(Only for testing)
Intent intent = new Intent(serviceactivirt.this, AlarmController.class);
PendingIntent sender = PendingIntent.getActivity(serviceactivirt.this, 0, intent, 0);
try {
sender.send();
} catch (CanceledException e) {
Toast.makeText(getApplicationContext(), "FEJLSAN", Toast.LENGTH_LONG).show();
}
This actually works, except that my app crashes at launch, but the scheduled activity is working...
Any ideas? Is this "The way to do it" or is there a more recommended way?
Cheers!
Solution:
Instead of having a BroadcastReciever calling an Activity, i made the BroadcastReciever starting a Service. And changed my Activity to a Service, programmaticly and in manifest.
Works great!
Im trying to make an scheduled activity go off every hour or so, all working in the background.
Please allow users to configure other options, such as using a Notification, rather than being interrupted by an activity taking over the foreground.
Right now i have a BroadcastReceiver that picks up when the device is booted.
You would only need that to set up an AlarmManager schedule for your hourly events. Your PendingIntent for the AlarmManager could be one you obtain via getActivity().
How ever, this doesnt seem to work.
If you want to start an activity, call startActivity(). Do not create a PendingIntent, then immediately send() the PendingIntent.
Also, get rid of getApplicationContext() and simply use this.
except that my app crashes at launch
Use adb logcat, DDMS, or the DDMS perspective in Eclipse to examine LogCat and look at the stack trace associated with your crash.