How to show Show Alert Dialog from System Service?

3,754 views
Skip to first unread message

shridutt kothari

unread,
Oct 24, 2013, 9:00:43 AM10/24/13
to android-...@googlegroups.com
Hi folks,

I am trying to show an AlertDialog from my SystemService, from /framework/base/service/../  through following code:
 
private void showDialog(CharSequence messageId) {
mDialog = new AlertDialog.Builder(getApplicationContext())
             .setMessage(messageId)
             .setPositiveButton(android.R.string.ok,
                 new DialogInterface.OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialog, int whichButton) {
                    mDialog.dismiss();
                     }
                 }
             )
             .create();
     mDialog.show();
 }

But getting following error:
A/SystemServer(2093): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

I think it is because i am trying to show dialog from SystemService, But eventually i am trying to that only.

So please can anyone tell me How can we show AlertDialog from SystemService ???

Thanks,
Shridutt Kothari,
Impetus Infotech Limited

Durgadoss Ramanathan

unread,
Oct 24, 2013, 11:28:46 AM10/24/13
to android-...@googlegroups.com
Firstly, You need a reference to Context.NOTIFICATION_SERVICE.

Then create a class extending AsyncTask and in this class,
you will be able to show a Toast or a progressDialog or whatever.

Thanks,
Durga



--
You received this message because you are subscribed to the Google Groups "android-platform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-platfo...@googlegroups.com.
To post to this group, send email to android-...@googlegroups.com.
Visit this group at http://groups.google.com/group/android-platform.
For more options, visit https://groups.google.com/groups/opt_out.



--
Regards
Durgadoss

Dianne Hackborn

unread,
Oct 24, 2013, 8:29:06 PM10/24/13
to android-...@googlegroups.com
You need to set the window type to an appropriate system type.  You can look at the error dialogs in the activity manager for an example.


--
You received this message because you are subscribed to the Google Groups "android-platform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-platfo...@googlegroups.com.
To post to this group, send email to android-...@googlegroups.com.
Visit this group at http://groups.google.com/group/android-platform.
For more options, visit https://groups.google.com/groups/opt_out.



--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

shridutt kothari

unread,
Oct 25, 2013, 2:43:02 AM10/25/13
to android-...@googlegroups.com
Hi Dianne,

Thanks a lot for your kind answer with pointing me in right direction,
After adding following properties to my above code, now i am able to show alert dialog from My SystemService: 
  
 mDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
mDialog.getWindow().getAttributes().privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS; 

:)
Thanks Again,
Shridutt kothari,
Impetus Infotech Pvt Ltd

Darshan Khatri

unread,
Dec 22, 2016, 11:10:37 AM12/22/16
to android-platform
public void showSettingsAlert(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialogBuilder.setMessage("GPS is not enabled. Do you want to go to settings menu?");
alertDialog.setTitle("GPS is settings");

// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});

// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});


// Showing Alert Message
alertDialog.show();
}



i found an error cannot resolve symbol setpositivebutton  
Reply all
Reply to author
Forward
0 new messages