我的清单里有一个接收器。
<receiver
android:name="com.deviceinventory.StartAppAtBootReceiver"
android:enabled="true"
android:exported="false"
android:label="StartMyServiceAtBootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
我的com.deviceinventory.StartAppAtBootReceiver onReceive()是
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent startUpIntent = new Intent(context, StartUpActivity.class);
startUpIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startUpIntent);
}
由于StartUpActivity使用互联网,所以我想在互联网连接可用后启动此活动。
目前它在互联网连接建立之前的某个时间开始。
我不知道如何修改manifest和BroadCastReceiver
答案 0 :(得分:1)
为此,您需要再添加一个Receiver来检查Internet连接是启用还是禁用,启用后,您可以启动要运行的bootReceiver或Any Activity。
public class NetworkInfoReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
try {
/***
* Here we need to check its running perfectly means this receiver
* call when net is off/on. You should have to check
* */
final ConnectivityManager conMngr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
// Check if we are connected to an active data network.
final NetworkInfo activeNetwork = conMngr.getActiveNetworkInfo();
final boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
if (isConnected) {
/**
* Start service/receiver/activity here.
* */
} else {
/**
* Stop service/receiver/activity here.
* */
}
} catch (final Exception e) {
}
}
}
然后将此接收器添加到AndroidManifest.xml文件中。
<receiver
android:name=".NetworkInfoReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
答案 1 :(得分:0)
您可以尝试编辑意图过滤器。现在,当设备完全启动时,你的收音机会响应。改变它以在连接性改变时响应它......
<receiver android:name=".NetworkChangeReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
希望这段代码可以帮到你。
答案 2 :(得分:0)
@Hemant你需要检查onReceive是否满足两个条件才能启动服务。
public void onReceive(Context context, Intent intent) {
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if(intent.equals(Intent.ACTION_BOOT_COMPLETE) && wifiManager.isWifiEnabled()){
//start your service
}
}