Java 如何重新启动或恢复活动
如果连接被禁用,我想在更改wifi设置菜单时重新启动活动。 这是我的代码(onCreate方法): 这是显示对话框的代码:Java 如何重新启动或恢复活动,java,android,Java,Android,如果连接被禁用,我想在更改wifi设置菜单时重新启动活动。 这是我的代码(onCreate方法): 这是显示对话框的代码: @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the Builder class for convenient dialog construction // 1. Instantiate an AlertDialog.Builder with it
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
// 1. Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// 2. Chain together various setter methods to set the dialog characteristics
builder.setTitle("Connection Error !")
.setMessage("Please check your Internet Connection for start the application.")
.setPositiveButton("Back to Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent=new Intent();
intent.setComponent(new ComponentName("com.android.settings", "com.android.settings.WirelessSettings"));
startActivity(intent);
}
});
// Create the AlertDialog object and return it
return builder.create();
}
当用户将wifi网络更改为“开”时,我如何重新启动或恢复应用程序以检查网络连接
感谢使用onRestart方法
protected void onRestart() {
super.onRestart();
}
我认为,如果你需要重新启动活动,那么你的设计就有问题了
startActivity(getIntent());
finish();
这将从“活动”中调用recreate()来完成此操作
public void recreate ()
使用新实例重新创建此活动。这导致的流程与由于配置更改而创建活动时的流程基本相同——当前实例将通过其生命周期进入onDestroy(),然后再创建一个新实例。尝试将第一块代码放入onResume()方法中 当从WirelessSettings返回到activity时,调用onResume方法并为status var分配一个新值
Try this one.
@Override
protected void onResume() {
super.onResume();
notify("onResume");
}
or
@Override
protected void onStart() {
super.onStart(); // Always call the superclass method first
// The activity is either being restarted or started for the first time
// so this is where we should make sure that GPS is enabled
LocationManager locationManager =
(LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!gpsEnabled) {
// Create a dialog here that requests the user to enable GPS, and use an intent
// with the android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS action
// to take the user to the Settings screen to enable GPS when they click "OK"
}
}
@Override
protected void onRestart() {
super.onRestart(); // Always call the superclass method first
// Activity being restarted from stopped state
}
请访问此链接。
onCreateDialog方法位于我在onCreate方法中调用的另一个类中。。如果连接被禁用,该对话框将向用户发送wifi设置并打开wifi连接。当用户返回到应用程序时,我如何重新检查连接?您可以在将用户发送到网络设置时发出一个标志,并在OnResume()中检查连接,只要该标志位于我正在使用布尔值解析的数据库上。如果用户转到设置页面,则为TRUE;如果按下取消按钮,则为FALSE。
Try this one.
@Override
protected void onResume() {
super.onResume();
notify("onResume");
}
or
@Override
protected void onStart() {
super.onStart(); // Always call the superclass method first
// The activity is either being restarted or started for the first time
// so this is where we should make sure that GPS is enabled
LocationManager locationManager =
(LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!gpsEnabled) {
// Create a dialog here that requests the user to enable GPS, and use an intent
// with the android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS action
// to take the user to the Settings screen to enable GPS when they click "OK"
}
}
@Override
protected void onRestart() {
super.onRestart(); // Always call the superclass method first
// Activity being restarted from stopped state
}