Close application from broadcast receiver

I am new to android programming. I've tried registering the broadcast receiver in action, but my receiver doesn't work when the apps are onPause. So I found that I needed to register my receiver in the manifest.

My goal is to close my application for some time after the user disconnects Wi-Fi.

This is my code, but it doesn't work.

public class ReceiverWifi extends BroadcastReceiver {

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

        Handler handler = new Handler();
        Runnable runnable = new Runnable() {

            @Override
            public void run() {
                MainActivity m = new MainActivity();
                m.finish();

            }
        };

        if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {

            int newWifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
                    WifiManager.WIFI_STATE_UNKNOWN);

            switch (newWifiState) {

            case WifiManager.WIFI_STATE_DISABLED:

                Toast.makeText(context, "Wi-fi Disconnected ",
                        Toast.LENGTH_SHORT).show();

                handler.postDelayed(runnable, 15 * 1000);
                break;

            }
        }

    }
}

      

my manifest:

<receiver android:name="com.example.wifimonitor.ReceiverWifi" >
    <intent-filter>
        <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
    </intent-filter>
</receiver>

      

How to achieve your goal?

+3


source to share


3 answers


Is your broadcast receiver in primary activity class? If so, you can save the global context of your application and close it later in the broadcast receiver.

If it's on a service or vendor, just send it an instance of your app when you start / register it. Then you call finish () from that instance.

EDIT:

Try the following:

In your main activity, create this public method:



   public static void closeActivity(){
        finish();
   }

      

Then, in your broadcast receiver, call this method like this:

   MainActivity.closeActivity();

      

Hope it helps;)

0


source


Use the following code to send a new intent to MainActivity from BroadcastReceiver:

Intent i = new Intent(context, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags (Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.putExtra("close_activity",true);
context.startActivity(i);

      

in MainActivity use OnNewIntent like below:



@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if(intent.getBooleanExtra("close_activity",false)){
        this.finish();

    }
}

      

FYI, I haven't tried the above code, but similar code works for me.

+2


source


MainActivity m = new MainActivity();
m.finish();

      

When you say you're done, it won't clear the stack because you didn't first load the activity onto the stack. MainActivity is not an activity here, it is just a class object in your code

You have a broadcast receiver in mainactivity when onreceive exits. don't instantiate with a new one.

0


source







All Articles