2014-09-19 108 views
1

我整天都在遇到一个大问题。我有一个应用程序,它在设备启动时运行正常,唯一的问题是该应用程序需要启动互联网连接,并且在应用程序启动之前wifi没有连接。所以我使用BroadcastReceiver和WifiManager的组合来确定是否有WiFi连接。我唯一的问题,如果它通过了有连接的测试,那么我想在MainActivity.java中运行onCreate。连接互联网/无线网络时运行代码

我该怎么做?我对Java非常陌生,并且对整个事情感到困惑,但很快就有所收获。

我有这样的代码在我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"); 

    } 

这里是内部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 
     } 
    } 

} 

这里是清单代码:

<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> 

我究竟做错了什么?

UPDATE

我更改SMSReceiver到接收器,而不是像这样的活动:

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

现在我该怎么办?

+0

你AndroidManifest应该声明SMSReceiver作为接收器,而不是一个活动。然后添加意图操作和权限以监听BOOT_COMPLETED意图。 – 2014-09-19 19:58:40

回答

2

是啊,我跟你的舱单申报@alpinescrambler同意应该是这样的,你要观察的连接变化

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

你也应该在清单

添加此权限0
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 

如果你想观察所有的网络变化检查了这一点

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; 
    } 

} 
+0

这完美的作品,我仍然首先看到404错误,但是当它连接wifi时,404错误消失,想要添加一个加载屏幕。 – 2014-09-19 20:51:13

0

您的接收器清单声明是错误的?它应该像

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

也,开始您的MainActivity,是这样的:

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

我改变了一个接收器的活动,我把我的更新的问题....我怎么做下一步从SMSReciver.java调用MainActivity中的onCreate – 2014-09-19 20:05:56

+0

我更新了我的答案 – alpinescrambler 2014-09-19 20:13:57