aneela
aneela

Reputation: 1619

DialogBox Display in Android Application : How to set Context out of Activity

I am creating a backup app in android using BackupManagerbut as backup actually occur at schedule time not promptly, I just want to know how can I show a message in dialogbox that onBackup()is get called and now your data is saved on google servers. The main problem is what the context of dialogbox should be?? so far I have tried this but when onBackup()was called it didnot display

    AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
    builder.setTitle("BACKUP ALERT");
    builder.setMessage("Your Backup has been processed");
    builder.setPositiveButton("OK", null);
    AlertDialog dialog = builder.show();

I wrote this in onBackup()method but it didnot display on my application. I want to show this message whenever backup occur and user opens my application or if he has already opened I want to display it no matter which activity is foreground that time or can someone give me the idea how to make it a notification in android notification bar.

There is an error on Logcat at the line where I created and showed the dialog..

12-28 13:22:52.722: E/JavaBinder(16540): *** Uncaught remote exception!  (Exceptions are not yet supported across processes.)
12-28 13:22:52.722: E/JavaBinder(16540): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
12-28 13:22:52.722: E/JavaBinder(16540):    at android.os.Handler.<init>(Handler.java:121)
12-28 13:22:52.722: E/JavaBinder(16540):    at android.app.Dialog.<init>(Dialog.java:107)
12-28 13:22:52.722: E/JavaBinder(16540):    at android.app.AlertDialog.<init>(AlertDialog.java:118)
12-28 13:22:52.722: E/JavaBinder(16540):    at android.app.AlertDialog$Builder.create(AlertDialog.java:971)
12-28 13:22:52.722: E/JavaBinder(16540):    at com.vahzay.android.smstrove.MySmsBackupAgent1.onBackup(MySmsBackupAgent1.java:233)
12-28 13:22:52.722: E/JavaBinder(16540):    at android.app.backup.BackupAgent$BackupServiceBinder.doBackup(BackupAgent.java:490)
12-28 13:22:52.722: E/JavaBinder(16540):    at android.app.IBackupAgent$Stub.onTransact(IBackupAgent.java:80)
12-28 13:22:52.722: E/JavaBinder(16540):    at android.os.Binder.execTransact(Binder.java:338)
12-28 13:22:52.722: E/JavaBinder(16540):    at dalvik.system.NativeStart.run(Native Method)

and my code for the dialogbox is:

 new AlertDialog.Builder(getApplicationContext())
    .setMessage("Backup Alert")
    .setPositiveButton("Ok", new DialogInterface.OnClickListener() 
    {                   
        public void onClick(DialogInterface arg0, int arg1) 
        {
 Toast.makeText(getApplicationContext(), "Your SMS Record that you requested to backup earlier is now processed", Toast.LENGTH_LONG).show(); //-->233
        }
    }).create().show();
}

Upvotes: 1

Views: 1707

Answers (3)

Sahil Mahajan Mj
Sahil Mahajan Mj

Reputation: 11141

Just a thought, if it worked. I have observed that we need to use both .create() and .show() to show an alertDialiog.

So try to use builder.create().show();

Edit- I have the follwoing working code for me,

new AlertDialog.Builder(getApplicationContext())
        .setMessage("Network is in Roaming")
        .setPositiveButton("Ok", new DialogInterface.OnClickListener() 
        {                   
            public void onClick(DialogInterface arg0, int arg1) 
            {

            }
        }).create().show(); 

If at all it doesn't works, then you can call an activity and show the same dialog here in the activty. You can make the activity as transparent, so that only dialog is visible.

Upvotes: 1

Pratik Sharma
Pratik Sharma

Reputation: 13415

Try to change,

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

With,

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

EDIT :

Another Option is Static Context from Application Class,

public class MyApplication extends Application{

    private static Context context;

    public void onCreate(){
        super.onCreate();
        MyApplication.context = getApplicationContext();
    }

    public static Context getAppContext() {
        return MyApplication.context;
    }
}

Now call from anywhere,

MyApplication.getAppContext() to get your application context statically.

Thanks.

Upvotes: 1

Bhavdip Sagar
Bhavdip Sagar

Reputation: 1971

try this way might be help

new AlertDialog.Builder(youractivity.this);

ok let try in that way

  • create one static Context mContext;
  • then in your activity onCreate write like mContext = youractivity.this;
  • that's it just simple call static variable like mContext;

Upvotes: 0

Related Questions