连接网络时如何检测网络变化并将activity从A更改为B
How to detect Network change and change activity from A to B when Network is connected
我有 SplashActivity、Activity A 和 Activity B。
当 Internet 不可用时,Splash activity 重定向到 Activity A,当 Internet 可用且已连接时,SplashActivity 重定向到 Activity B。
我想在用户自己连接时立即关闭 Activity A,并在用户仍在应用程序中并打开他的 wifi 或移动数据时打开 Activity B。
这是我在 SplahsActivity 中使用的代码,根据网络状态重定向到 Activity A 和 Activity B
public static boolean isNetworkStatusAvialable(Context context) {
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
}
我用
检查
if (isNetworkStatusAvialable(getApplicationContext())) {
// Load Activity B
} else {
Load Activity A and Toast Message, " No Internet"
}
提前致谢。
在这种情况下,您应该使用 BroadcastReceiver
检查网络状态,因为您想要立即关闭当前 activity 并移动到其他 activity。所以下面是它的完整代码:
ConnectivityStatusReceiver.java
public class ConnectivityStatusReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connMgr.getActiveNetworkInfo();
if (activeNetworkInfo != null) {
Toast.makeText(context, activeNetworkInfo.getTypeName() + " connected", Toast.LENGTH_SHORT).show();
// Your code to start Activity B
Activity activity = (Activity) context;
intent = new Intent(activity, ActivityB.class);
activity.startActivity(intent);
} else {
Toast.makeText(context, "No Internet or Network connection available", Toast.LENGTH_LONG).show();
}
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
ConnectivityStatusReceiver connectivityStatusReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connectivityStatusReceiver = new ConnectivityStatusReceiver();
}
@Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(connectivityStatusReceiver, intentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (connectivityStatusReceiver != null) {
// unregister receiver
unregisterReceiver(connectivityStatusReceiver);
}
}
}
要关闭 Activity A,您只需在清单中添加 android:noHistory="true"
,如下所示:
<activity android:label="@string/app_name" android:name="ActivityA"/>
希望对您有所帮助。
我有 SplashActivity、Activity A 和 Activity B。
当 Internet 不可用时,Splash activity 重定向到 Activity A,当 Internet 可用且已连接时,SplashActivity 重定向到 Activity B。
我想在用户自己连接时立即关闭 Activity A,并在用户仍在应用程序中并打开他的 wifi 或移动数据时打开 Activity B。
这是我在 SplahsActivity 中使用的代码,根据网络状态重定向到 Activity A 和 Activity B
public static boolean isNetworkStatusAvialable(Context context) {
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
}
我用
检查 if (isNetworkStatusAvialable(getApplicationContext())) {
// Load Activity B
} else {
Load Activity A and Toast Message, " No Internet"
}
提前致谢。
在这种情况下,您应该使用 BroadcastReceiver
检查网络状态,因为您想要立即关闭当前 activity 并移动到其他 activity。所以下面是它的完整代码:
ConnectivityStatusReceiver.java
public class ConnectivityStatusReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connMgr.getActiveNetworkInfo();
if (activeNetworkInfo != null) {
Toast.makeText(context, activeNetworkInfo.getTypeName() + " connected", Toast.LENGTH_SHORT).show();
// Your code to start Activity B
Activity activity = (Activity) context;
intent = new Intent(activity, ActivityB.class);
activity.startActivity(intent);
} else {
Toast.makeText(context, "No Internet or Network connection available", Toast.LENGTH_LONG).show();
}
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
ConnectivityStatusReceiver connectivityStatusReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connectivityStatusReceiver = new ConnectivityStatusReceiver();
}
@Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(connectivityStatusReceiver, intentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (connectivityStatusReceiver != null) {
// unregister receiver
unregisterReceiver(connectivityStatusReceiver);
}
}
}
要关闭 Activity A,您只需在清单中添加 android:noHistory="true"
,如下所示:
<activity android:label="@string/app_name" android:name="ActivityA"/>
希望对您有所帮助。