Necessary conditions for AlertDialog to open - android

Many of us have come across the dreaded
E/WindowManager﹕ android.view.WindowLeaked
This can happen for several reasons as is mentioned here and in many other SO questions.
I have tried making sure my dialogs are in a "good place" before showing them, often wrapping them around certain conditions like:
if (!isFinishing() && !isDestroyed()) {
mDialog.show();
}
I'm curious if anyone knows of a "perfect environment" for which an AlertDialog is guaranteed to always display without error. From personal experience, even with the above conditions, the WindowLeaked error still always comes up.

This really is a problem I faced when changing the orientation of the Activity, whenever we try to display a dialog with a closed Activity this error will skyrocket. In a less important case, as in a Activity data recording information, I simply ignore the Dialog and closing with the following code:
#Override
public void onPause() {
super.onPause();
try {
if (myAlertDialog != null && myAlertDialog.isShowing()) {
myAlertDialog.dismiss();
myAlertDialog = null ;
}
} catch (Exception e) {
e.printStackTrace();
}
}
But if you need to keep it on the screen, the only way I found was locking the orientation of Activity.

Related

Android: wait for vibrator to finish

Let's say I have async tasks that when finished, lock the vibrator and send it a pattern. In a nutshell, that's the kind of code I'm dealing with:
lock(vib);
vib.vibrate(pattern);
release(vib);
return;
The problem is, the vibration starts and just immediately stops due to the task's returning and I could use some help in changing that.
I have tried
lock(vib);
long duration = getDuration(pattern);
vib.vibrate(pattern);
synchronized(this){
try {
wait(duration);
} catch (InterruptedException e) {
...
}
}
release(vib);
return;
but that doesn't seem to actually do anything.
Since I have not found a way to determine whether or not the phone is currently vibrating, any suggestions on how I should best resolve the issue?

Clear missed call notification in android

whenever we get a call, we do see missed call notification. Is there a way to remove the missed call notification in android programatically?
We see missed calls numbers & its count. Can we remove them via code?
The only "legal" but extremely ugly and usually useless way to achieve what you want is to show Call Log to user. And I mean literally show (becomes visual, gets focus). In case you want to do this, here's how:
public static boolean showCallLog(Context context)
{
try
{
Intent showCallLog = new Intent();
showCallLog.setAction(Intent.ACTION_VIEW);
showCallLog.setType(android.provider.CallLog.Calls.CONTENT_TYPE);
context.startActivity(showCallLog);
return true;
}
catch (Exception e)
{
Log.d("Couldn't show call log.", e.getMessage());
}
return false;
}
The reason behind this mess is the fact that apps authoritatively responsible for call logging and notifying users about missed calls (stock phone apps) use cached values. Why? Because of overall performance. You need to somehow notify those apps that Call Log has changed (seen means changed, as well) and that it should update it. It would be nice if all such apps on all devices would receive a broadcast in order to refresh, but as far as I know, it's not the case.
I hope someone will find a better way to force refresh on stock phone apps.

Can't reproduce android crash

Android keeps on reporting crashes from users which I can't reproduce on my phone.
I can find the lines which seem to be incorrect:
cursor.moveToFirst();
elechs=cursor.getString(2);
elecls=cursor.getString(3);
gass=cursor.getString(4);
waters=cursor.getString(5);
cursor.close();
if (elechs.length()!=0){
elechdb=Double.valueOf(elechs);
}
else {
elechdb=0.0;
}
if (elecls.length()!=0){
elecldb=Double.valueOf(elecls);}
else {
elecldb=0.0;
}
if (gass.length()!=0){
gasdb=Double.valueOf(gass);
}
else {
gasdb=0.0;
}
if (waters.length()!=0){
waterdb=Double.valueOf(waters);
}
else {
waterdb=0.0;
}
elecldb=Double.valueOf(elecls);
gasdb=Double.valueOf(gass);
waterdb=Double.valueOf(waters);
If I look at the code, it doesn't make any sense.
I think I forgot to delete the last three lines. First I check the string. If the string is empty it will store the value as zero.
The incorrect last three lines will also try to make a double if the cell is empty. This cause a lot of crashes. However not on my machine.
I believe that it shouldn't be possible to make a of an empty cell.
Does anyone know why this error doesn't crash my phone?
Your best solution is probably just to remove the problematic lines that shouldn't be there anyway, preferably add actual error handling around calls to Double.valueOf() in case the input is completely malformed (there may be inconsistent behaviour if the cell is empty, but if it says "hello world", everything will crash), and release an update.
Since the users are getting NumberFormatException, you should catch the exception and perform appropriate action when it happens.
if (elechs.length()!=0) {
try {
elechdb=Double.valueOf(elechs);
} catch (NumberFormatException e) {
// Perform error handling
}
}
If the users are getting NullPointerException, you should check if the strings are null before checking for their lengths.
If the data is put in by the user, you should do both of the above to avoid future problems.

AsyncTask doesn't stop

I've created an AsyncTask that loads messaging history from a database and then shows it on the device screen:
private void loadHistoryFromDB(Date lastUpdateDate)
{
final class DBAsyncTask extends AsyncTask<Void, Void, List<XMPPMessage>>
{
#Override
protected List<XMPPMessage> doInBackground(Void... arg0)
{
List<XMPPMessage> messages = null;
try
{
messages = PersistenceManager.getXMPPMessagesFromDB(userInfo, 0, messagingActivity);
}
catch (SQLException e)
{
e.printStackTrace();
}
catch (LetsDatabaseException e)
{
e.printStackTrace();
}
return messages;
}
It seems to work fine, but after being executed, it leaves 2 running threads and I can't finish the activity because of that. How can I fix it?
As long as your tasks are executing properly (exits from onPostExecute), this shouldn't be something you have to worry about. Once executed, AsyncTask thread(s) will stick around for possible reuse in the form of a thread pool or single thread, depending on platform version. This is normal behaviour - they will eventually be cleaned-up/reused.
First off, make sure you are calling super.doInBackGround() at the top of your overridden method call.
If that isn't it, it's likely because you are maintaining the connecting to the database.
That is, you still have a lock established on the database.
See if you can explicitly unlock the database, that may fix your problem.
You could put it in the onPostExecute() method.
This problem is most likely due to confusion surrounding the cancel method of AsyncTask.
You need to break down your background task into loopable segments, then Before each loop iteration starts doing your task,you need to check if the task is cancelled and if it is you need to break the loop. There doesn't seem to be any other way to stop an AsyncTask from executing.
I've posted a detailed guide to this problem with code examples here:
http://tpbapp.com/android-development/android-asynctask-stop-running-cancel-method/

How to implement the javascript setTimeout function in Java

I am currently trying to integrate a live search functionality in android. I use a customized Autocomplete widget. The widget itself provides me with a threshold to only start a query after a certain amount of characters have been typed in. But what I also want is that a query only starts, say if a user has stopped typing for 2 seconds.
As I load my contents with a AsyncTask I tried blocking the doInBackground function by calling Thread.sleep() right at the beginning. If the user would then continue typing the program would look after an existing task, cancel it and start a new one. This was my idea. But it doesn't quite work the way I expected. Sometimes it sends out queries even if the user is still typing, sometimes queries are canceled after the users stopped typing.
Do you have any ideas on this or maybe a better way to solve this?
Here are the code snippets:
1. The function that is called on text changes:
public void afterTextChanged(Editable s) {
if(mWidget.enoughToFilter()) {
if(mTask != null && mTask.getStatus() != Status.FINISHED) {
mTask.cancel(true);
}
mTask = new KeywordSearchLoader(mActivity,
mItems);
mTask.execute(s.toString());
}
}
2. doInBrackground
try {
Thread.sleep(Properties.ASYNC_SEARCH_DELAY);
} catch (InterruptedException e) {
Log.e(TAG, "the process was interrupted while sleeping");
Log.w(TAG, e);
}
Thanks
Phil
Create a Handler and use .postDelayed(..) to run a background task after some delay.
If user presses the key then call handler.removeCallback(..) and then again .postDelayed(..) to add a new delayed callback.

Categories

Resources