Java 如何在android中显示未连接的internet
我需要帮助。我想干杯,以显示我的应用程序中的互联网状态。它适用于wifi和移动数据包(移动互联网)。但当互联网完全断开时,它什么也看不见。我不知道怎么做。这是我的密码Java 如何在android中显示未连接的internet,java,android,android-intent,android-toast,Java,Android,Android Intent,Android Toast,我需要帮助。我想干杯,以显示我的应用程序中的互联网状态。它适用于wifi和移动数据包(移动互联网)。但当互联网完全断开时,它什么也看不见。我不知道怎么做。这是我的密码 public class ConnectivityReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.d("myapp", "action: " + inten
public class ConnectivityReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("myapp", "action: " + intent.getAction());
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
if(isConnected)
{
if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
{
Toast toast = Toast.makeText(context, "Connected to wifi", Toast.LENGTH_LONG);
toast.show();
}
else if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
{
Toast toast = Toast.makeText(context, "Connected to Mobile Data Pakage", Toast.LENGTH_LONG);
toast.show();
}
else
{
Toast toast = Toast.makeText(context, "Not Connected", Toast.LENGTH_LONG);
toast.show();
}
也可以尝试此操作,但编译器显示错误。请帮帮我。提前谢谢
else if(activeNetwork.getType() == ConnectivityManager.EXTRA_NO_CONNECTIVITY)
{
Toast toast = Toast.makeText(context, "Not Connected", Toast.LENGTH_LONG);
toast.show();
宣言
<receiver android:name="com.networkstatechange.ConnectivityReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
如果缩进正确,您会看到您的吐司“未连接”仅在您输入块
if(isConnected)
时才会显示。这样重写:
if(isConnected)
{
if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
{
Toast toast = Toast.makeText(context, "Connected to wifi", Toast.LENGTH_LONG);
toast.show();
}
else if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
{
Toast toast = Toast.makeText(context, "Connected to Mobile Data Pakage", Toast.LENGTH_LONG);
toast.show();
}
}
else
{
Toast toast = Toast.makeText(context, "Not Connected", Toast.LENGTH_LONG);
toast.show();
}
}
如果缩进正确,您会看到只有在输入块
if(isConnected)
时才会显示吐司“notconnected”。这样重写:
if(isConnected)
{
if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
{
Toast toast = Toast.makeText(context, "Connected to wifi", Toast.LENGTH_LONG);
toast.show();
}
else if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
{
Toast toast = Toast.makeText(context, "Connected to Mobile Data Pakage", Toast.LENGTH_LONG);
toast.show();
}
}
else
{
Toast toast = Toast.makeText(context, "Not Connected", Toast.LENGTH_LONG);
toast.show();
}
}
试试这个
public static boolean checkConn(Context ctx) {
ConnectivityManager conMgr = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo i = conMgr.getActiveNetworkInfo();
if (i == null)
return false;
if (!i.isConnected())
return false;
if (!i.isAvailable())
return false;
return true;
}
也使用
试试这个
public static boolean checkConn(Context ctx) {
ConnectivityManager conMgr = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo i = conMgr.getActiveNetworkInfo();
if (i == null)
return false;
if (!i.isConnected())
return false;
if (!i.isAvailable())
return false;
return true;
}
也使用
公共类连接检测器{
private Context _context;
public ConnectionDetector(Context context){
this._context = context;
}
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;
}
private Context\u Context;
公共连接检测器(上下文){
这._context=context;
}
公共布尔值未连接到Internet(){
ConnectivityManager connectivity=(ConnectivityManager)_context.getSystemService(context.connectivity_SERVICE);
if(连接性!=null)
{
NetworkInfo[]info=connectivity.getAllNetworkInfo();
如果(信息!=null)
对于(int i=0;i
}
创建这个类
并在接收端使用“连接到internet”方法
它应该很好用
haply编码:)公共类连接检测器{
private Context _context;
public ConnectionDetector(Context context){
this._context = context;
}
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;
}
private Context\u Context;
公共连接检测器(上下文){
这._context=context;
}
公共布尔值未连接到Internet(){
ConnectivityManager connectivity=(ConnectivityManager)_context.getSystemService(context.connectivity_SERVICE);
if(连接性!=null)
{
NetworkInfo[]info=connectivity.getAllNetworkInfo();
如果(信息!=null)
对于(int i=0;i
}
创建这个类
并在接收端使用“连接到internet”方法
它应该很好用
可能编码:)使用
ACCESS\u NETWORK\u STATE
permission..检查我的答案..使用ACCESS\u NETWORK\u STATE
permission..检查我的答案。。