简体   繁体   中英

Run code when internet / WiFi is connected

I have been having a big issue all day. I have an app and it runs right when the device is booted up, the only issue is that the app requires internet connection to start and the wifi is not connected before the app is booted up. So I am using a Combo of BroadcastReceiver and WifiManager to determine if there is a WiFi connect. My only question, if it passes the test of having a connection, then I want to run the onCreate in MainActivity.java .

How would I do that? I am very new to Java and very confused by this whole thing, but kinda catching on quickly.

I have this code in my MainActivity.java :

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

        setContentView(R.layout.activity_main);

        WebView myWebView = (WebView) findViewById(R.id.webview);
        myWebView.setWebViewClient(new WebViewClient());
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.loadUrl("http://www.example.com");

    }

And here is the code inside SMSReceiver.Java:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;

public class SMSReceiver extends BroadcastReceiver
{


    @Override
    public void onReceive(Context context, Intent intent) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        wifiManager.setWifiEnabled(true);
        if(wifiManager.isWifiEnabled()){
            //Call onCreate in MainActivity.java
        }
    }

}

And here is the manifest code:

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SMSReceiver"
            android:label="@string/title_activity_smsreceiver" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

What Am I doing wrong?

UPDATE

I Change SMSReceiver to a receiver and not an activity like so:

<receiver android:name=".SMSReceiver ">
            <intent-filter>
                <action android:name="android.intent.action.YOUR_FILTER_OR_SOME_STANDARD" />
            </intent-filter>
        </receiver>

Now what do I do?

Yeah i agree with @alpinescrambler about your manifest declaration should be like this and you want to observe connectivity change

<receiver android:name=".SMSReceiver" >
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
        </receiver>

You should also add this permissions in the manifest

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

If you want to observe all network changes check this out

    public class SMSReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, SMSReceiver.class.getSimpleName() +"\naction: " + intent.getAction());
        if(isOnline(context) == true) {
            //do your staff
            context.startActivity(new Intent(context, MainActivity.class));
        }
    }

    boolean isOnline(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            return true;
        }
        return false;
    }

}

Your manifest declaration for the receiver is wrong? It should be something like

<receiver android:name=".SMSReceiver ">
        <intent-filter>
                <action android:name="android.intent.action.YOUR_FILTER_OR_SOME_STANDARD" />
        </intent-filter>
</receiver>

also, to start your MainActivity, something like:

    Intent i = new Intent(context, MainActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(i); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM