POPUP on Boot Completed
Trying to create a popup after every boot is completed to get some confirmation. Able to get the BOOT_COMPLETED Message but while opening Alertdialog its crashing, Below are the code lines i am using. Please suggest
public class BootReceiver extends BroadcastReceiver {
private static final String TAG = "BootReceiver";
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
Log.d(TAG, "BroadcastReceiver:" + action);
if (action.equals(Intent.ACTION_BOOT_COMPLETED))
{
Log.d(TAG, "ACTION_BOOT_COMPLETETED Receved" );
CharSequence text = "BOOT COMPLETED!";
int duration = Toast.LENGTH_SHORT;
Toast.makeText(context,text,Toast.LENGTH_SHORT).show();
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setMessage("Message");
alertDialog.setTitle("CONSENT");
alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
alertDialog.setCancelable(false);
alertDialog.setPositiveButton("AGREE", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.d(TAG, " AGREE CLICKED" );
} });
开发者_高级运维 alertDialog.setNegativeButton("DISAGREE", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.d(TAG, " DISAGREE CLICKED" );
} });
alertDialog.show();
}
}
}
Back Trace :
I/ActivityManager( 109): Start proc com.android.mms for broadcast com.android.m
ms/.transaction.MmsSystemEventReceiver: pid=311 uid=10015 gids={3003, 1015}
E/AndroidRuntime( 246): FATAL EXCEPTION: main
E/AndroidRuntime( 246): java.lang.RuntimeException: Unable to start receiver co
m.android.settings.BootReceiver: android.view.WindowManager$BadTokenExceptio
n: Unable to add window -- token null is not for an application
E/AndroidRuntime( 246): at android.app.ActivityThread.handleReceiver(Act
ivityThread.java:1862)
E/AndroidRuntime( 246): at android.app.ActivityThread.access$2400(Activi
tyThread.java:124)
E/AndroidRuntime( 246): at android.app.ActivityThread$H.handleMessage(Ac
tivityThread.java:1035)
E/AndroidRuntime( 246): at android.os.Handler.dispatchMessage(Handler.ja
va:99)
E/AndroidRuntime( 246): at android.os.Looper.loop(Looper.java:126)
E/AndroidRuntime( 246): at android.app.ActivityThread.main(ActivityThrea
d.java:3897)
E/AndroidRuntime( 246): at java.lang.reflect.Method.invokeNative(Native
Method)
E/AndroidRuntime( 246): at java.lang.reflect.Method.invoke(Method.java:4
91)
E/AndroidRuntime( 246): at com.android.internal.os.ZygoteInit$MethodAndA
rgsCaller.run(ZygoteInit.java:841)
E/AndroidRuntime( 246): at com.android.internal.os.ZygoteInit.main(Zygot
eInit.java:599)
E/AndroidRuntime( 246): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 246): Caused by: android.view.WindowManager$BadTokenException
: Unable to add window -- token null is not for an application
E/AndroidRuntime( 246): at android.view.ViewRoot.setView(ViewRoot.java:4
34)
E/AndroidRuntime( 246): at android.view.WindowManagerImpl.addView(Window
ManagerImpl.java:181)
E/AndroidRuntime( 246): at android.view.WindowManagerImpl.addView(Window
ManagerImpl.java:95)
E/AndroidRuntime( 246): at android.app.Dialog.show(Dialog.java:269)
E/AndroidRuntime( 246): at android.app.AlertDialog$Builder.show(AlertDia
log.java:853)
E/AndroidRuntime( 246): at com.android.settings.BootReceiver.onRecei
ve(XtraBootReceiver.java:53)
E/AndroidRuntime( 246): at android.app.ActivityThread.handleReceiver(Act
ivityThread.java:1855)
E/AndroidRuntime( 246): ... 10 more
A dialog needs an activity context. You have a service. Your options are:
- Use a toast.
- Create an activity and give it a Dialog theme.
- There may be a third option to create or obtain a suitable context, but I'm not aware of one.
精彩评论