2

Hi Android programmers,

This question is previously asked. But there is no answer. So i need solution for that. Actually i am trying to display alert box of Mainactivity.java calling from test.java(Non-activity).

Thats working fine if their is no UI coding. if that code is dependent with UI means its throw errors.

Activity class :

public class Mainactivity extends Activity
{
    public void message()
    {
        Log.i("Success : Call from non-UI & non activity class");//Upto this line working if i called message() function from other non-activity class
        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener()
        {
        public void onClick(DialogInterface dialog, int which)
        {
             switch (which)
             {
                //Code
            }
         }
     };
     AlertDialog.Builder builder = new AlertDialog.Builder(this);
     builder.setTitle("FMS Status Message :- ");
     builder.setMessage(msg)
     .setPositiveButton("OK",dialogClickListener).show();

    }
}

Non-Activity class :

public class test extends BroadcastReceiver
{
    Mainactvity in = new Mainactvity();   

    @Override   
    public void onReceive(Context context, Intent intent)
    {
       in.message();
    }
}

If it is resolved then my main objective of my project will be completed. Please any one of you help me.

Thanks in advance.

Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96
DillGates
  • 93
  • 2
  • 11
  • If it throws errors- Can you please post the error? – Anukool Feb 14 '13 at 06:53
  • 1
    Why you do't think of passing Context and Activity from your Mainactvity to class test.And also the code you are showing is Bad Programming Practice for Android, – Sree Feb 14 '13 at 06:55

2 Answers2

3

First of All, Bad Programming Practice for Android,

Mainactvity in = new Mainactvity();   

You can not make a Constructor of Activity class.

And second, You are trying to display UI elements in Dialog which has not reference of Activity, in BroadcastReceiver. Use Context of Activity (If it concern with UI) or Application for displaying Dialog in BroadcastReceiver.

user370305
  • 108,599
  • 23
  • 164
  • 151
  • Ok, I understand that. Is there possibility to display alert box in test class..? – DillGates Feb 14 '13 at 06:57
  • I tried this, but its not working : Context mContext = getApplicationContext(); final AlertDialog.Builder alert = new AlertDialog.Builder(mContext); alert.setTitle("Got message, From: " + to); alert.setCancelable(true); alert.show(); Note test class is not an activity. Please reply me. – DillGates Feb 14 '13 at 07:37
  • Actually, I need to display an alert box in test(Non-activity) class. Thats it..! – DillGates Feb 14 '13 at 11:58
  • You can not make a `AlertDialog` in `BroadcastReceiver`. You have to maintain a callback mechanism with your `Activity` and `BroadcastReceiver`. Once you get appropriate action in broadcast receiver which trigger your Activity and from Activity you can call AlertDialog method. Look at http://stackoverflow.com/questions/4844031/alertdialog-from-within-broadcastreceiver-can-it-be-done – user370305 Feb 14 '13 at 12:25
0

Create a Constructor, where you can get Activity. Like this -

Activity activity;
public test (Activity activity){
         this.activity = activity;
}

Now, use this activity as argument -

AlertDialog.Builder adb=new AlertDialog.Builder(activity);

Because dialog can't be shown using just a context. You need to provide an Activity for that.

I answered one here

Community
  • 1
  • 1
Darpan
  • 5,623
  • 3
  • 48
  • 80
  • 1
    This doesn't work. The Dialog is not created with the passed activity, no error but no dialog as well. – Ekta Jun 22 '16 at 13:44
  • You may ask a new question and post your code, so that I can see it. – Darpan Jun 22 '16 at 14:03
  • I get this to say it not work, good idea though: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference – Jonas Dec 08 '16 at 12:33