如何检测网络,并打开无线设置 [英] how to detect internet and open wireless settings
本文介绍了如何检测网络,并打开无线设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的应用程序应该如何工作 http://postimg.org/image/57byitwpz/
这里是错误的即时得到 http://postimg.org/image/ynyqerln9/
请DONOT给任何东西在我的code加纠正,而不是我的code。
香港专业教育学院误写成的.xml代替的.class第一形象,请忽略
这里是我的code ** ** SplashScreen.java
公共类闪屏延伸活动{
如果(!NetworkCheckClass.haveNetworkConnection(SplashActivity.this)){
Toast.makeText(SplashScreen.this,没有互联网连接!,Toast.LENGTH_LONG).show(); 意向意图=新意图(Settings.ACTION_WIRELESS_SETTINGS);
startActivity(意向);
}
其他{
//你的code如果连接可用
//这是启动画面的我的code
私有静态诠释SPLASH_TIME_OUT = 3000; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_splash); 新的处理程序()。postDelayed(新的Runnable(){ / *
*显示启动画面用计时器。这将是有益的,当你
*希望出现在您的应用程序标识/公司
* / @覆盖
公共无效的run(){
//一旦定时结束该方法将被执行
//开始您的应用程序主要活动
意图I =新意图(SplashScreen.this,MainActivity.class);
startActivity(ⅰ); //关闭此活动
完();
}
},SPLASH_TIME_OUT);
}}
解决方案
试试这个,它可以帮助你。
进口android.app.Activity;
进口android.app.AlertDialog;
进口android.app.AlertDialog.Builder;
进口android.content.Context;
进口android.content.DialogInterface;
进口android.content.DialogInterface.OnClickListener;
进口android.content.Intent;
进口android.net.ConnectivityManager;
进口android.os.Bundle;
进口android.provider.Settings;
进口android.util.Log;
进口android.widget.Toast;公共类飞溅延伸活动{
静态ConnectivityManager厘米;
AlertDialog dailog;
AlertDialog.Builder构建;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
厘米=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); //检查
// 互联网
打造=新生成器(Splash.this); //连接
的setContentView(R.layout.activity_splash);
如果(cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)//如果连接
//有屏幕变为
//下一个画面
//别的节目
// 信息
.isConnectedOrConnecting()
|| cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
.isConnectedOrConnecting()){
Log.e(CM价值
+ cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
.isConnectedOrConnecting());
Toast.makeText(Splash.this,互联网是积极的,2000年).show();
线程MyThread的=新主题(){
公共无效的run(){
尝试{ 睡眠(5000); }赶上(例外五){
} {最后
意向意图=新意图(Splash.this,
yournextactivity.class);
startActivity(意向);
完();
}
}
};
mythread.start();
}其他{ build.setMessage(此应用程序需要互联网connection.Would你连接到互联网?);
build.setPositiveButton(是,新OnClickListener(){ @覆盖
公共无效的onClick(DialogInterface对话,诠释它){
// TODO自动生成方法存根 startActivity(新意图(Settings.ACTION_WIFI_SETTINGS)); }
});
build.setNegativeButton(否,新OnClickListener(){ @覆盖
公共无效的onClick(DialogInterface对话,诠释它){
// TODO自动生成方法存根
build.setMessage(是否确定要退出吗?);
build.setPositiveButton(是,新OnClickListener(){ @覆盖
公共无效的onClick(DialogInterface对话,诠释它){
// TODO自动生成方法存根
完();
}
}); }
});
dailog = build.create();
dailog.show(); }}
}
here is how my application should work http://postimg.org/image/57byitwpz/ and here is the errors im getting http://postimg.org/image/ynyqerln9/ please donot give anything to add in my code instead correct my code. ive mistakenly written .xml instead of .class in the first image , please ignore that
here is my code
**SplashScreen.java**
public class SplashScreen extends Activity {
if (!NetworkCheckClass.haveNetworkConnection(SplashActivity.this)) {
Toast.makeText(SplashScreen.this, "No internet connection!",Toast.LENGTH_LONG).show();
Intent intent=new Intent(Settings.ACTION_WIRELESS_SETTINGS);
startActivity(intent);
}
else {
// your code if connection is available
// this is my code of splash screen
private static int SPLASH_TIME_OUT = 3000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
@Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
}
解决方案
try this it may help you
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;
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,
yournextactivity.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));
}
});
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();
}
}
}
这篇关于如何检测网络,并打开无线设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文