what does handle() in android handlers - android

I am working on an Android project that works well, and it uses Handler methods. But, I can't find this particular method in any tutorials or documentation.
The Handler method looks like this:
private Handler OnName = new Handler() {
#Override
public void handle() {
//do some stuff that needs it's own thread
finish();
}
};
It is overwriting the handle() method, I am not finding it in the documentation
Am I looking in the wrong place, or what is this for?
Can someone a short explanation or a good link of when handle()
triggers?

I am working on an android project where previous coders used Handlers
More importantly, the previous coders didn't use a distinct class name, causing confusion for others encountering the code base.
Please slap the previous coders with a trout for me.
Am I looking in the wrong place, or what is this for?
As you discovered, this isn't android.os.Handler, but something else. My guess is that it is a subclass of android.os.Handler, where (for whatever reason) they are ignoring the Message normally delivered to handleMessage().

Related

Incorrect code in firefox Notifications.jsm and strange behaviour

I am using notifications.jsm to provide notification for downloads from Downloads.jsm. The following code is incorrect in notifications.jsm :
unregisterHandler: function(key, handler) {
let i = _handlersMap[key].indexOf(handler);
if (i > -1) {
_handlersMap.splice(i, 1);
}
},
_handlersMap is an object, so splice is not a function.
Additionally, perhaps because of the unregisterHandler method being incorrect, there are occasions when having uninstalled my addon and create a new Notification in the reinstalled addon I am getting dead objects in the observe method.
My code creates a separate handler for each new notification with a handlerKey incremented by one for each new download. For the different observe event types the code is similar, the handlers for the key are iterated and the corresponding method is called. Since I have registered a different handler for each notification there should be only one handler for that key to call but this is not the case, there are dead objects from the previous addin that have the same key from the new addon. Surely this behaviour is not correct.
Please can someone look at this and the other posts that I have made regarding the firefox sdk. I only thought that Microsoft could provide undocumentation, in fact this is much worse as the documentation is nonsense and the code that is being provided has errors.
Regards

Android. ACRA. Is putCustomData thread safe

I can't seem to find the answer to this question I'm having:
Is the method from the ACRA library ...
ACRA.getErrorReporter().putCustomData(Name, Content);
... thread safe?
I'd like to call it from two different threads and I'm not sure if i should or not.
I've searched through the documentation but I wasn't able to find anything related to this unfortunately, or maybe I'm just a bad Googleler :)
If you're not sure, buy some insurance:
ErrorReporter er = ACRA.getErrorReporter();
synchronized( er ) {
er.putCustomData( ..., .... );
}
So I think I've figure it out.
Looking through the code I've noticed that the putCustomData method is in fact a call to a HashMap's method, more precisely put.
Upon further search I've found that the method put is not synchronized.
So the answer is no, calling putCustomData from two different threads can create problems for you.
For a solution to the problem: see 323go's idea below. In my case, I already had a singleton class which I used, so I just placed a synchronized method in that class in which I called the putCustomData method.
Note: If I am wrong, someone please let me know, but this is what I was able to find out.

Is it good practice to write Intent dispatching as static methods in the class to be launched?

I'm just starting Android development, so I'd like a little advice on code style. It seems nice to me to write Intent dispatchers in methods that are doing the dispatching, like
// in case it's not clear, names are meta-variables
public class MyService...
...
public static void sendMessage(Context ctx, MyArgClass myArg) {
Intent sendIntent = new Intent(ctx, MyService.class);
sendIntent.setAction("send message");
sendIntent.putExtra("my_arg", myArg);
ctx.startService(sendIntent);
}
}
then, any callees just run MyService.sendMessage(ctx, arg), instead of having the Intent creation code in their bodies.
It seems like a win: there's less stuff to remember when you want to e.g. sendMessage, and you don't have to synchronize names, like "send message" and "my_arg" across modules. However, I don't see it that often in Google's music app that they've open sourced, so I'm wondering if there are downsides, and I should stick to convention.
It is good practice. This pattern can be found in android developer guides (sample)
Please allow me to answer your question in a more general context, ie programming approach static vs singletin, since these are the alternatives I would consider.
I found that using static solutions for global access to methods, constants, etc.. seems to be a matter of taste.
One commonly used alternative is a singeton approach where you create only one instance of an obejct and use this to access your method. So also in your case you could use a sigleton instead - I have not looked at the Google code you referring to, but I wouldn't be surprised to see a singelton pattern instead.
You may find several discussions on that - but in general singletons allow you to reuse code and control object state much easier than static. The main difference is that singletons can implement interfaces, so you can pass them around.
However, in cases where I just need easy access to some utility methods I prefer static solutions - since they are easier and faster to implement and use IMHO.
This is like having a Utility method to take care of this.
In the example code for GCM from Android, they have done a similar thing.

Scheduling an event in Android

What is the best way to schedule an event?
I was thinking about the new Calendar API, but it requires an internet connection at least to create a new calendar.
Is there any API for creating a schedule which doesn't require internet connection?
Check out the FAQ You are more likely to keep getting help if you go back to your previous questions and accept answers that were helpful to you. Also see How does accepting an answer work? for more info.
AlarmManager will let you do that.
If you want to schedule your event for relatively soon though Handler is a better choice. You can use it to post a runnable like this:
Runnable r = new Runnable() {
public void run() {
doSomething();
}
}
Handler h = new Handler();
h.postDelayed(r, 1000);
This will execute the run method of your runnable in one second. You can change the second parameter of the postDelayed method to change the interval of time that passes. It is in milliseconds.
EDIT: Also note that the "best" way to do something is subjective, and generally depends on exactly what you are trying to do. If you can be more specific about what you are trying to schedule and how far in advance you'd like to schedule it then perhaps we can help narrow it down to the "best" choice. But without more info there is no way that we can tell you which is "best"

Android: I can't use a CursorAdapter because of concurrency concerns. What should I do instead?

thank you for your time.
Hopefully I can be as clear as possible, apologies in advance to the extent that I fail to do so.
The situation:
I am creating a multithreaded Android application that deals with a local database. For synchronization/concurrency reasons, I want a particular activity to have no dealings at all with a Cursor. Right now, however, it uses a CursorAdapter to show data from the db on the screen.
The question:
How can I replicate the functionality of a SimpleCursorAdapter without using a Cursor (because having an open cursor will introduce concurrency errors)?
(Using an ArrayList of objects would work best, as I already have that lying around ready to play with.)
Thank you. I will stick around and clarify everything as needed.
Something that may be useful:
If you want to deal with Cursors in a threadsafe way on an asynchronous thread (i.e. predictably) try using runOnUiThread: http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)
The code is as follows:
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
//Paste code here
}
});
This code would go in one of the AsyncTask overloads such as onPreExecute or doInBackground.
This may allow you to run non-thread-safe code in a more threadsafe manner.
I hope this helps!

Categories

Resources