Android 启用Wi-Fi后,应打开一个新活动
我现在使用这个代码来检查Internet连接是否打开。如果关闭,将显示无线设置页面。所以,我想要的是,在我启用wi-fi连接后,它应该打开SplashScreen 2活动。如何做到这一点?正如您将在下面看到的,没有意图/行动要求打开新活动Android 启用Wi-Fi后,应打开一个新活动,android,android-internet,Android,Android Internet,我现在使用这个代码来检查Internet连接是否打开。如果关闭,将显示无线设置页面。所以,我想要的是,在我启用wi-fi连接后,它应该打开SplashScreen 2活动。如何做到这一点?正如您将在下面看到的,没有意图/行动要求打开新活动 public class Splash extends Activity { static ConnectivityManager cm; AlertDialog dailog; AlertDialog.Builder build; @Override pr
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();
}
}
看看这里——所以,在构建、cm、对话框界面等上方出现了多个红色标记。。。这行有多个标记-生成器无法解析为一种类型。不要删除全局变量,我正在更新我的答案。它成功了。但是,当我将此启动设置为我的Android清单上的第二个启动,并将主活动设置为对此的第一个启动调用时,这些活动会相互争斗,然后应用程序就会停止。你能发布你的堆栈跟踪吗?我解决了我的问题。我在我的主要活动中做了一些更改-我删除了“public void run”,这是启动新活动的一些代码,所以我不知道为什么,但它导致了崩溃。好的,谢谢你的帮助,我把你的回答标为可以接受。