5

I'm new in android programming. I've tried registering broadcast receiver in activity, but my receiver not working when apps onPause. So i found that i need to registering my receiver in manifest.

My objective is to close my application for some time after user turn off Wifi.

This is my code but its not working.

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 do i achieve my objective?

vintry
  • 301
  • 3
  • 9

3 Answers3

3

Use code below to send 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 as 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 I have similar code working.

Manavendher
  • 385
  • 4
  • 5
-1
MainActivity m = new MainActivity();
m.finish();

When you say finish, it wont clear from stack because you didn't loaded the activity into stack first. The MainActivity is not activity here, it is just a class object in your code

Have broadcast receiver in mainactivity, when onreceive finish the activity. don't create instance with new.

Maveňツ
  • 1
  • 12
  • 50
  • 89
Harsha Vardhan
  • 3,324
  • 2
  • 17
  • 22
-1

Your broadcast receiver is in the main activity class? If so you can save a global context of your app and close it later in the broadcast receiver.

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

EDIT:

Try this:

In your main activity create this public method:

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

Then in your broadcast receiver call this method like:

   MainActivity.closeActivity();

Hope it helps ;)

kodartcha
  • 1,063
  • 12
  • 23
  • i have my ReceiverWifi as a new class, how do i send an instance to it? – vintry Oct 01 '14 at 06:38
  • 1
    i got this error : Cannot make a static reference to the non-static method finish() from the type Activity – vintry Oct 01 '14 at 09:43
  • 2
    Change `finish()` for `MainActivity.finish()`... or create a `private static Context context;` in your `onCreat` you do `context = this;` and then in the `closeActivity()` method you do `context.finish();` – kodartcha Oct 01 '14 at 09:47
  • 1
    @margabro it's not working for me.. :( giving the same error.i tried your solution 'in comments' ,then it says cannot resolve method `finish(); ` If I define finish it gives can't refer non-static from static,if i declare finish as static it gives static method finish cannot override instance method.. :( :( – Prabs Aug 07 '15 at 14:05
  • It is obviously not working, finish is a non-static method as it performs an action over an instace, if there is no instace you cant call the finish(). Anyone has a better solution? – Tiago_nes Oct 23 '17 at 16:15