-3
package dgameman1.com.emojiupdaterroot;

import android.app.AlertDialog;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;

import java.io.IOException;
public class DownloadBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
        AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
        alertDialog.setTitle("Restart?");
        alertDialog.setMessage("Android needs to restart to update Emojis.");
        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "I don't have a choice",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        try {
                            Runtime.getRuntime().exec("su -c reboot");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
        alertDialog.show();



    }
}
}

So for some reason, this line

AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();

has a red squiggly line under MainActivity.this I'm not sure what to change it to, because it works when It's inside my MainActivity.Java just not when I put it inside the BroadCastReceiver.java Class

  • 2
    It's because `DownloadBroadcastReceiver` isn't a nested class inside `MainActivity`. – Andy Turner Feb 09 '16 at 09:55
  • @AndyTurner Ah, so what should I change it to then? I tried changing it to `DownloadBroadcastReceiver.this` and that didn't work – IdkHowToCodeAtAll Feb 09 '16 at 09:56
  • 1
    You already have `context` in `onReceive(Context context, Intent intent)` – M D Feb 09 '16 at 09:57
  • @AndyTurner I keep getting a crash now though. java.lang.RuntimeException: Unable to start receiver dgameman1.com.emojiupdaterroot.DownloadBroadcastReceiver: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application – IdkHowToCodeAtAll Feb 09 '16 at 10:21

1 Answers1

0

The problem is you are trying to show an AlertDialog from a BroadcastReceiver, which isn't allowed. You can't show an AlertDialog from a BroadcastReceiver. you can creat an activity for your dialogBox and start your Activity in the BroadcastReceiver. or Creat your Broadcast class in the Activity then call a method of your Activity. See this example show an alert dialog in broadcast receiver after a system reboot

example:

A broadCastReceiver for Network change state that start an activity with a dialogBox:

public class NetworkStateReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {


        Log.d("app", "Network connectivity change");
        if (intent.getExtras() != null) {
            NetworkInfo ni = (NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
            if (ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
                Log.i("app", "Network " + ni.getTypeName() + " connected");
                Toast.makeText(context, context.getResources().getString(R.string.network_on), Toast.LENGTH_LONG).show();
                Intent intent1 = new Intent(context , Main2Activity.class);
                intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent1);

            } else if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) {
                Log.d("app", "There's no network connectivity");
                Intent intent1 = new Intent(context , Main2Activity.class);
                intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent1);
                Toast.makeText(context, context.getResources().getString(R.string.network_off), Toast.LENGTH_LONG).show();
            }
        }


    }
}

the actvity's code :

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        dialogbox();
    }


    public void dialogbox(){

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
        // // set the message to display
        alertbox.setTitle("title");
        //alertbox.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        // alertbox.setIcon(R.drawable.ic_launcher);

        LayoutInflater factory = LayoutInflater.from(this);
        //View layout = factory.inflate(R.layout.dialog_box, null);

        //alertbox.setView(layout);
        alertbox.setNeutralButton("cancel",
                new DialogInterface.OnClickListener() {

                    // click listener on the alert box
                    public void onClick(DialogInterface arg0, int arg1) {
                        // the button was clicked
                        Main2Activity.this.finish();
                    }
                });
        // show it
        alertbox.show();


    }

}

Don't forget to add the flag new Task in the Broadcast Receiver and to register your broadcast Receiver by adding this to the manifest:

<receiver android:name=".NetworkStateReceiver">
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
                <action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
            </intent-filter>
        </receiver>
Community
  • 1
  • 1
zied
  • 201
  • 3
  • 7
  • 17