我是基于java的Android应用程序的新手。 如果互联网连接可用,我想开始新的活动(另一个屏幕)。
我在app发明者中尝试过。
就像点击按钮一样,它通过网址https://www.google.com检查互联网连接,如果重新启动状态代码200导航到下一个屏幕,则将吐司消息称为“启用互联网连接”。 我怎么能在Android应用程序中做到这一点。
(编辑问题)
如何在此内插入连接检查
public class mainmenu extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from activity_main.xml
setContentView(R.layout.main_menu);
// Locate the button in activity_main.xml
Button indiabt = (Button)findViewById(R.id.indiab);
// Capture button clicks
indiabt.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent = new Intent(mainmenu.this,
exampage.class);
startActivity(myIntent);
}
});
谢谢大家的帮助。我得到了代码。 期待更多帮助。我需要的代码如下。
public class mainmenu extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from activity_main.xml
setContentView(R.layout.main_menu);
Button indiabt = (Button)findViewById(R.id.indiab);
indiabt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = cm
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo datac = cm
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if ((wifi != null & datac != null)
&& (wifi.isConnected() | datac.isConnected())) {
setContentView(R.layout.question);
}else{
//no connection
Toast toast = Toast.makeText(mainmenu.this, "No Internet Connection", Toast.LENGTH_LONG);
toast.show();
}
}
});
}}
答案 0 :(得分:2)
使用此功能执行任务。
Button indiabt = (Button)findViewById(R.id.indiab);
indiabt .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = cm
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo datac = cm
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if ((wifi != null & datac != null)
&& (wifi.isConnected() | datac.isConnected())) {
setContentView(R.layout.activity_main2);
}else{
//no connection
Toast toast = Toast.makeText(MainActivity.this, "No Internet Connection", Toast.LENGTH_LONG);
toast.show();
}
}
});
答案 1 :(得分:0)
在按钮内尝试:
if(isNetworkAvailable()){
startActivity(context, yourActivity.class);
}
检查互联网连接是否可用的方法:
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
您还需要在清单中添加一个permision
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
答案 2 :(得分:0)
如果你真的想检查互联网访问,而不仅仅是连接状态, 使用这篇文章的答案:
并将处理程序更改为:
Handler h = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what != 1) { // code if not connected
Toast.makeText(getActivity(), "Enable internet connection", Toast.LENGTH_SHORT).show();
} else { // code if connected
Intent intent = new Intent (this, ActivityUWantToStart.class);
startActivity(intent);
}
}
};
希望这能解决您的问题
答案 3 :(得分:0)
检查this question的答案。通过Alexandre Jasmin提供的功能,您可以检查您是否有互联网连接。
然后你需要做的就是检查这个函数的结果,并在if子句中执行你的操作。
rlutil::setColor(rlutil::GREY)
答案 4 :(得分:0)
首先创建一个'ConnectionDetector'类并将以下代码放入其中:
private Context _context;
public ConnectionDetector(Context context){
this._context = context;
}
/**
* Checking for all possible internet providers
* **/
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
然后在您的活动中通过添加以下内容检查按钮单击时的网络连接:
cd = new ConnectionDetector(getApplicationContext());
if (!cd.isConnectingToInternet()) {
AlertDialog alertDialog=new AlertDialog.Builder(LoginActivity.this, R.style.cust_dialog).create();
alertDialog.setTitle("Alert!");
alertDialog.setMessage("Internet Connection Error, Please connect to working Internet connection");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which)
{
Toast.makeText(A.this, "You clicked on OK", Toast.LENGTH_SHORT).show();
}
});
// Showing Alert Message
alertDialog.show();
}
if(cd.isConnectingToInternet()) {
Intent i=new Intent(A.this, B.class);
startActivity(i);
}
还要将以下权限添加到“AndroidManifest.xml”文件中:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />