-3

I am trying to check internet connectivity from broadcast receiver and if there is not internet want to show an alert dialog but here I receives an error ablove mentioned due to AlertDialog.Builder(context) . But if I change it to AlertDialog.Builder(this) or AlertDialog.Builder(NetworkChangeReceiver.this), I get compile error. The constructor AlertDialog.Builder(NetworkChangeReceiver) is undefined

NetworkChangeReceiver.java

package com.example;


public class NetworkChangeReceiver extends BroadcastReceiver {

@SuppressWarnings("deprecation")
@Override
public void onReceive(final Context context, final Intent intent) {
    Log.d("Inside Network change receiver", Background.isActivityVisible()+"------------------------");
    if(Background.isActivityVisible()==true){
        Boolean status = NetworkUtil.getConnectivityStatusString(context);
        Log.d("NetworkChangeReceiver", status+"");
        if(status){
            Log.d("NetworkChangeReceiver", "User Comes online");
        }
        else{
            Intent i = new Intent(context, Offline.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
            try{
            final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

            // set title
            alertDialogBuilder.setTitle("Lost Internet Connectivity");

            // set dialog message
            alertDialogBuilder
            .setMessage("Do you want to retry!")
            .setCancelable(false)
            .setPositiveButton("Yes",new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, close
                    // current activity
                    Boolean status = NetworkUtil.getConnectivityStatusString(context);  
                    if(status){
                        Intent i = new Intent(context, MainActivity.class);
                        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(i);
                    }

                    else{
                        // create alert dialog
                        AlertDialog alertDialog = alertDialogBuilder.create();

                        // show it
                        alertDialog.show();
                    }

                }
            })
            .setNegativeButton("No",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    Intent intent = new Intent(Intent.ACTION_MAIN);
                    intent.addCategory(Intent.CATEGORY_HOME);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                  context.  startActivity(intent);
                }
            });

            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();
        }   catch(Exception e){
            e.printStackTrace();
        }     
        }
    }
}}
Parsania Hardik
  • 4,593
  • 1
  • 33
  • 33
SwagDevelopers
  • 109
  • 2
  • 12

3 Answers3

2

It is not possible to show alert dialog in broadcast receiver. Please check this and this.

Community
  • 1
  • 1
SripadRaj
  • 1,687
  • 2
  • 22
  • 33
0

Try this,

YourAlertDialog dialog = new YourAlertDialog(mContext);
dialog.getWindow()
    .setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.show();

And Add system alert permission in your mainfest.xml:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />

this may helps you.

Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
0

Unable to show alert dialog-android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an application

Due to :

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

line. because AlertDialog.Builder require Context of Visible application component like Activity instead of Service, BroadcastReceiver,etc.

Currently passing context to AlertDialog.Builder which is first parameter of onReceive method, is not valid Context to show UI elements.

To show Alert from BroadcastReceiver:

1. start an Activity with Theme.Translucent.NoTitleBar theme and
2. show AlertDialog in onCreate of Activity

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213