I want to show an alert dialog when booting of device is completed. There are no problems with the broadcast receiver, it works fine. But when boot is completed, there is no open activity, hence I get NullPointerException
here. How can I show a dialog box in this situation? This is the code I use to show the dialog:
public class RestartReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
AlertDialog alertHelp;
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
TextView m_timetext = new TextView(context );
m_timetext.setText("hello");
// m_timetext.setTextColor(getResources().getColor(R.color.dark_green));
LinearLayout linearLayout = new LinearLayout(context );
linearLayout.setLayoutParams( new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));
linearLayout.setOrientation(1);
linearLayout.addView(m_timetext);
dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}});
dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}});
alertHelp = dialog.create();
alertHelp.setView(linearLayout);
alertHelp.show();
Log.d("In","Switched On");
}
}
Please help me. Thanks in advance.
Here is a post on how to do it. You can get the source code from here.
Your code doesn't work because you can't show dialog directly from your broadcast receiver. You have to use an Activity
. Also, in order to receive ACTION_BOOT_COMPLETED
your activity must be first explicitly started by user or by another application (google application stopped state for more information).
Basically, to achieve the required functionality, you need to do:
BroadcastReceiver
that receives ACTION_BOOT_COMPLETED
and starts your activity.Also, this question provides more information on how to create a transparent activity.