If you need to open a messagebox from a non view class then you have two solutions :
Or
- From your android view, when you instantiate your class, pass a context to the constructor of your class and then store this context. You'll then be able to use it in your alertboxes.
Be careful of memory leaks with the context passing mecanism
EDIT :
I was writing some code to get you going on how to use handlers when I took a chance and went for the doc. Usually there is nothing of use there but for this particular case, oh miracle, look what I found, complete and easy to understand code example of how to use a handler and its message mecanism. It is hidden under the foldable title ("http://developer.android.com/guide/topics/ui/dialogs.html#ShowingAProgressBar") : http://developer.android.com/guide/topics/ui/dialogs.html#ShowingAProgressBar
EDIT2 AFTER COMMENTS
Because the op wants its object to be reusable in different activities it makes sense not to use handlers but instead to pass the context( a link to the calling activity really) to the object. The object will then be able to use this context in the dialog.builder.
In the oncreate of your activity class called MyActivity :
MyCustomObject myObject = new MyCustomObject(this);
In your object class
Class MyCustomObject {
private MyActivity mContext;
void MyCustomObject(MyActivity context) {
this.mContext = context;
}
private showDialog(String message) {
AlertDialog.Builder alert = new AlertDialog.Builder(mContext);//we use the context
}
}
DO NOT FORGET TO DESTROY AND NULLIFY THE DIALOG BUILDER AND THE mContext when you are done with your object. This could leak memory really fast.