0

I have an Android application that works with sms messages. When new sms message arrives Android, will call code from my class that is "NEW_SMS" broadcast receiver. As you know, "onReceive" method in my "sms_recieved_class" gets some context (from the system). Using intents, my sms broadcast receiver calls another class that does something with sms database (context originally got from system via "onReceive" is used to invoke code from other class by sending another broadcast).

That other class works with sms database and it's also a broadcast receiver. It reacts on broadcasts sent by my class mentioned above. If "messing" with database goes wrong, app should show custom error dialog. Now, THERE's the problem!

Custom error dialog code:

public static void setUpError(Context act,String ERRMSG, StackTraceElement[] stack) {

AlertDialog.Builder builder = new AlertDialog.Builder(act);

    final StackTraceElement[] finStack = stack;
    final String errMsg=ERRMSG;
    final Context ctxFin = act;

    builder.setMessage(poruka)...setting up alert buttons...

    AlertDialog alert = builder.create();
    alert.show();

}

Error goes off at "alert.show()" line. I'm confused cause when i try to show this dialog while my app is visible (and pass "someAct.this" as Context) everything is fine. When i try to show alert using Context taken from "broadcastReceiver.onReceive" (called by the system) i got error. What i'm doing wrong?

P.S. after searching for answer on STOVF, every time the solution was "don't use getApplicationContext()". Since both of my classes that are called when phone gets new message AREN'T of type "Activity", what should i do?

guest86
  • 2,894
  • 8
  • 49
  • 72

1 Answers1

1

First of all, Dialog needs activity that is on screen (so without any opened activity, you cannot show Dialog). What you can do is create activity that will look like Dialog. Check this question from SO.

Community
  • 1
  • 1
Seraphis
  • 1,026
  • 2
  • 14
  • 30
  • That's something! :) And how to call that activity and make it host for my AlertDialog? This is totally new approach for me... Or the trick is to create a plain "PopupActivity" with some special attributes in manifest, and call that activity on error (and in PopupActivity.onCreate somehow define alertDialog looks)? – guest86 May 19 '12 at 18:42
  • I haven't done anything like that, but maybe try to use BroadcastReceiver in which you have context (as a parameter in onReceive() method), to start new Activity? – Seraphis May 19 '12 at 18:48
  • Works like a wheel! Thank you! – guest86 May 19 '12 at 19:19