我整天都有一个大问题。我有一个应用程序,它在设备启动时运行,唯一的问题是应用程序需要启动互联网连接,并且在启动应用程序之前未连接wifi。所以我使用BroadcastReceiver和WifiManager的Combo来确定是否有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>
我做错了什么?
更新
我将SMSReceiver更改为接收者,而不是像这样的活动:
<receiver android:name=".SMSReceiver ">
<intent-filter>
<action android:name="android.intent.action.YOUR_FILTER_OR_SOME_STANDARD" />
</intent-filter>
</receiver>
现在该怎么办?
答案 0 :(得分:2)
是的,我同意@alpinescrambler关于你的清单声明应该是这样的,你想观察连接性变化
<receiver android:name=".SMSReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
您还应该在清单中添加此权限
<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;
}
}
答案 1 :(得分: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);