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 ?
Related
How can I call a method while my application is closed on Android?
I've tried:
Onpause(), OnDestroy(), and OnStop(). With no luck.
What I want is receiving something from the database and do my reaction
based upon when the application is closed.
I think you can use broadcast or service to do it.
You could create a base activity (BaseActivity class) from which all your activities would have to be derived. Then inside BaseActivity.onCreate - you would increase some SharedPreferences counter, and inside BaseActivity.onDestroy you would decrease it. Now when that counter is equal to zero, you might assume your application is closed - but the process might still run in the background.
What I want is receiving something from the database
now I assume you already know how to "receive something from database". This could be background Service, where you could check SharedPreferences and do your processing.
What is the trigger?
Part 1 : Intent -
By example, you can set an alarm, that launch an Intent.
Part 2 : Broadcast Receiver - You must create a Broadcast Receiver (which reacts to an intent).
Part 3 : Service - The Broadcast Intent will launch a service that will execute when the application is not open.
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.
I am creating an android service using this code:
var intent = Ti.Android.createServiceIntent({
url : 'service.js'
});
Ti.Android.startService(intent);
As you can see, this service is started only once, it is not called in an interval every X milliseconds. Now when the application is exited (not put to background) and started up again, I would like to retrieve the reference of the service and stop it if the user clicks the stop button. Is there any way of achieving this? Thank you
Stop a Service in app.js by
Identifying the stop button click event and call this to stop service
if(Ti.Android.isServiceRunning(intent){
Titanium.Android.stopService(intent);
}
I have a simple setup:
CrashHandler - a class which implements Thread.UncaughtExceptionHandler;
CrashActivity - an activity which can send user reports;
MainActivity - the main app with which the user should interact.
When there is an uncaught exception within the MainActivity or any of it's threads, the CrashHandler intercepts it and creates a notification with an intent to start the CrashActivity:
Intent it = new Intent("CrashReporter" + SystemClock.currentThreadTimeMillis());
it.setClass(context, CrashActivity.class);
it.setFlags(it.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
In the mean time Android shows an "Application crashed" message, the user clicks OK, the application is closed, then the user might click the notification. If the notification is clicked, the CrashActivity starts and shows.
This code has been working for a long time in many different situations (crash on main thread, crash in a handler, crash on a background thread...). However, I recently discovered that it DOES NOT WORK if the exception is thrown in the OnClickListener.onClick method in a listener attached to a button in the MainActivity. The situation is as follows:
I execute code that deliberately throws a NullPointerException;
CrashHandler intercepts it and creates a notification (which is shown);
Android DOES NOT show any messages (for example, there is no "Application crashed", which should be visible);
The MainActivity is frozen;
IF the user clicks on the notification to launch the CrashActivity, a black screen is shown and everything freezes (the desired activity is not shown).
Logcat shows that there is a timeout on launch, even before OnCreate or any of my code:
I/ActivityManager(11826): START u0 {act=CrashHandler1196 flg=0x14000000 cmp=mycompany.myapp/.CrashActivity bnds=[0,102][720,230] (has extras)} from pid -1
W/KeyguardViewMediator(11826): verifyUnlock called when not externally disabled
W/ActivityManager(11826): Activity pause timeout for ActivityRecord{41f4d988 u0 mycompany.myapp/.MainActivity}
W/ActivityManager(11826): Launch timeout has expired, giving up wake lock!
W/ActivityManager(11826): Activity idle timeout for ActivityRecord{4225eeb8 u0 mycompany.myapp/.CrashActivity}
If before clicking the notification I kill the app from ADB, the notification works perfectly.
If before clicking the notification I make some clicks and gestures on the frozen app, after a few seconds I receive a message about an ANR:
E/ActivityManager(11826): ANR in mycompany.myapp (mycompany.myapp/.MainActivity)
E/ActivityManager(11826): Reason: keyDispatchingTimedOut
E/ActivityManager(11826): Load: 0.63 / 0.57 / 0.49
If I click "yes, kill it", and then click the notification, it works perfectly.
If I add System.exit(-1) in the CrashHandler just after creating the notification, the app immediately quits and the notification works perfectly (unfortunately, I can't go with this solution in production).
I have two questions:
Why a NullPointer exception in OnClickListener.onClick doesn't cause the app to crash, instead freezing it together with the OS and preventing other activities from starting?
What to do to avoid it in general, or at least, how to make the CrashActivity start in these conditions?
Without seeing your code, you should debug you CrashHandler Class. The JVM will ignore all exceptions in this
void uncaughtException(Thread t, Throwable e)
"Any exception thrown by this method will be ignored by the Java Virtual Machine." (JavaDoc).
I say not that this is the answer but it possible could the answer, if this method thwows an exceptions.
Why can't you kill your own app process with
https://developer.android.com/reference/android/os/Process.html#sendSignal(int, int)?
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.