gpt4 book ai didi

android - 启用 Wi-Fi 后,它应该会打开一个新的 Activity

转载 作者:行者123 更新时间:2023-11-30 01:53:52 25 4
gpt4 key购买 nike

我现在正在使用这段代码来检查 Internet 连接是打开还是关闭。如果关闭,则会显示无线设置页面。所以,我想要的是,在我启用 wi-fi 连接后,它应该打开 SplashScreen 2 Activity 。这个怎么做?正如您将在下面看到的,没有请求打开新 Activity 的 Intent/操作。

public class Splash extends Activity {
static ConnectivityManager cm;
AlertDialog dailog;
AlertDialog.Builder build;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);// checking
// internet
build = new Builder(Splash.this); // connectivity
setContentView(R.layout.activity_splash);
if (cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)// if connection is
// there screen goes
// to next screen
// else shows
// message
.isConnectedOrConnecting()
|| cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
.isConnectedOrConnecting()) {
Log.e("cm value",
""
+ cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
.isConnectedOrConnecting());
Toast.makeText(Splash.this, "Internet is active", 2000).show();
Thread mythread = new Thread() {
public void run() {
try {

sleep(5000);

} catch (Exception e) {
} finally {
Intent intent = new Intent(Splash.this,
SplashScreen2.class);
startActivity(intent);
finish();
}
}
};
mythread.start();
} else {

build.setMessage("This application requires Internet connection.Would you connect to internet ?");
build.setPositiveButton("Yes", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub

startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));

`Here the problem. There is no action after enable Wifi Connection. It should open SplashScreen2 activity`

}
});
build.setNegativeButton("No", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
build.setMessage("Are sure you want to exit?");
build.setPositiveButton("Yes", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
}
});

}
});
dailog = build.create();
dailog.show();

}

最佳答案

您可以简单地将整个代码放在 onResume() 而不是 onCreate() 方法中,如下所示,

public class Splash extends Activity {
static ConnectivityManager cm;
AlertDialog dailog;
AlertDialog.Builder build;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
}
@Override
public void onResume() {
super.onResume();
cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);// checking
// internet
build = new Builder(Splash.this); // connectivity
if (cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)// if connection is
// there screen goes
// to next screen
// else shows
// message
.isConnectedOrConnecting()
|| cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
.isConnectedOrConnecting()) {
Log.e("cm value",
""
+ cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
.isConnectedOrConnecting());
Toast.makeText(Splash.this, "Internet is active", 2000).show();
Thread mythread = new Thread() {
public void run() {
try {

sleep(5000);

} catch (Exception e) {
} finally {
Intent intent = new Intent(Splash.this,
SplashScreen2.class);
startActivity(intent);
finish();
}
}
};
mythread.start();
} else {

build.setMessage("This application requires Internet connection.Would you connect to internet ?");
build.setPositiveButton("Yes", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub

startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));

`Here the problem. There is no action after enable Wifi Connection. It should open SplashScreen2 activity`

}
});
build.setNegativeButton("No", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
build.setMessage("Are sure you want to exit?");
build.setPositiveButton("Yes", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
}
});

}
});
dailog = build.create();
dailog.show();

}
}

关于android - 启用 Wi-Fi 后,它应该会打开一个新的 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32573483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com