0

In my android app, I want to use broadcast receiver to check network state. SO till now I have created a broadcast receiver using,

    public class NetworkChangeListener extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        if(intent.getExtras() != null)
        {
            final ConnectivityManager connectivityManager=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
            final NetworkInfo ni=connectivityManager.getActiveNetworkInfo();

            if(ni!=null && ni.isConnectedOrConnecting())
            {
                //here i want to close the activity which is open from the else block
            }
            else
            {
                Intent mIntent = new Intent(context,NetworkDialog.class);
                mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(mIntent);

            }
        }
    }
}

as you can see in above code, I am calling the Networkdialog activity in else block. I would like to mention that, the NetworkDialog.class is a activity, but I have set its style as Dialog, using below code in manifest file

<activity
            android:name=".NetworkDialog"
            android:excludeFromRecents="true"
            android:label="You are Offline"
            android:screenOrientation="portrait"
            android:theme="@style/Theme.AppCompat.Light.Dialog.Alert"
            android:windowSoftInputMode="stateHidden" />

now, the problem is, this activity is calling successfully when network is not available, but when the user connects to internet, I am unable to finish this activity.

(Note- As we all know, the broadcast receiver works globally, means it will work even if your app is not active, so to prevent this, I am not registering the receiver in manifest file, I am registering and unregistering it in the activity where I want to use that receiver)

SO I just want to ask is, how can I finish that dialog activity in first if block, which is called from intent in else block

Here is my updated code

NetworkDialog.java

    public class NetworkDialog extends AppCompatActivity {
    public IntentFilter filter;

    BroadcastReceiver receiver=new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

                Toast.makeText(context,"Finish Activity here",Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.network_dialog);
        setFinishOnTouchOutside(false);

        filter=new IntentFilter();
        filter.addAction("com.hello.action");
        registerReceiver(receiver,filter);

    }
}

Broadcast receiver class

    public class NetworkChangeListener extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        if(intent.getExtras() != null)
        {
            final ConnectivityManager connectivityManager=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
            final NetworkInfo ni=connectivityManager.getActiveNetworkInfo();

            if(ni!=null && ni.isConnectedOrConnecting())
            {
                Intent local=new Intent();
                local.setAction("com.hello.action");
                context.sendBroadcast(local);
            }
            else
            {
                Intent mIntent = new Intent(context,NetworkDialog.class);
                mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(mIntent);

            }
        }
    }
}

I havent changed any other file

av development
  • 53
  • 1
  • 10

1 Answers1

0
   public class NetworkChangeListener extends BroadcastReceiver
 {
    @Override
   public void onReceive(Context context, Intent intent)
 {
    if(intent.getExtras() != null)
    {
        final ConnectivityManager connectivityManager=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo ni=connectivityManager.getActiveNetworkInfo();

        if(ni!=null && ni.isConnectedOrConnecting())
        {
            //here You need to call local broadcast receiver
            // Like this 
               Intent local = new Intent();
               local.setAction("com.hello.action");
               sendBroadcast(local);


        }
        else
        {
            Intent mIntent = new Intent(context,NetworkDialog.class);
            mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(mIntent);

        }
      }
   }
}

Code For dialog activity

   public class YourDialogActivity extends Activity {
     ArrayAdapter<String> adapter;

     @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

      IntentFilter filter = new IntentFilter();

      filter.addAction("com.hello.action");
      registerReceiver(receiver, filter);

    }
        BroadcastReceiver receiver = new BroadcastReceiver() {

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

      }
    };

    public void finish() {
        super.finish();
        };
   }
ankur
  • 51
  • 6