Bhavesh Jethani
Bhavesh Jethani

Reputation: 3875

open app when connect with wifi

How can I open my application when an user enters a zone that has wi-fi? Is this possible? Suppose my application is onPause() state (means My Device's homescreen). now when device connected with wifi. it will automatically open my application.

Upvotes: 5

Views: 6380

Answers (3)

marekb
marekb

Reputation: 873

Maybe doable with the Tasker app from Play Store (not free though). Or you can create a Service (http://developer.android.com/guide/components/services.html) that will have code outlined in the other answers and then launch your app (Activity) when wifi is available.

Upvotes: 0

g00dy
g00dy

Reputation: 6778

What I can imagine is an overwriting of the onPause() Method like this:

@Override
public void onPause() {
    String conn_context = Context.WIFI_SERVICE;
    final WifiManager wifi = (WifiManager) getSystemService(conn_context);
    if (wifi.isWifiEnabled())
    {
      super.onResume();
    }
    else
    {
      super.onPause(); 
    }
}

But you must also figure a way to handle the real onPause event.

Upvotes: 0

prozhyga
prozhyga

Reputation: 439

Try add broadcast receiver and listen network changes, when wi-fi connected start your activity. Something like this solution

public class ConnectivityReceiver extends BroadcastReceiver {

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

                ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                if (((null != wifi)&&(wifi.isAvailable())) || ((null != mobile)&&(mobile.isAvailable()))){
                    Intent uplIntent = new Intent(context, YourActivity.class);
                    uplIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(uplIntent);
                }

    }
}

And add to manifest

    <receiver android:name=".receiver.ConnectivityReceiver">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Upvotes: 5

Related Questions