网络连接不能在片段中工作
在问这个问题之前,我曾在Google
&上搜索。但绝望。网络连接不能在片段中工作
- 问题是连接吐司显示当互联网缺席。
这是我的代码,我需要你的敏锐建议。
protected boolean isInternetOn() {
ConnectivityManager connec = (ConnectivityManager)getActivity().getSystemService(getActivity().CONNECTIVITY_SERVICE);
//check for network connections
if(connec.getNetworkInfo(0).getState()== android.net.NetworkInfo.State.CONNECTED||
connec.getNetworkInfo(0).getState()== android.net.NetworkInfo.State.CONNECTING||
connec.getNetworkInfo(1).getState()== android.net.NetworkInfo.State.CONNECTING||
connec.getNetworkInfo(1).getState()== android.net.NetworkInfo.State.CONNECTED){
Toast.makeText(getActivity(), "Connected", Toast.LENGTH_LONG).show();
return true;
}else if (connec.getNetworkInfo(0).getState()== android.net.NetworkInfo.State.DISCONNECTED||
connec.getNetworkInfo(1).getState()== android.net.NetworkInfo.State.DISCONNECTED){
Toast.makeText(getActivity(), "Not Connected", Toast.LENGTH_LONG).show();
return false;
}
return false;
}
现在把这块isInternetOn
方法在我abstract
类扩展Fragment
这里是结构
public abstract class HomeTabFragment extends Fragment
//
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
if (RootView == null)
{
isInternetOn();
}
}
Here return view//
我试过,
ConnectivityManager connec = (ConnectivityManager)getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
是否有任何人符合这个问题?任何帮助将不胜感激。
试试这个:
ConnectivityManager connec = (ConnectivityManager)getActivity().getSystemService(getActivity().CONNECTIVITY_SERVICE);
变化
ConnectivityManager connec = (ConnectivityManager)getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
#Quang Doan先生。感谢您的关注。我已经尝试过。但不工作。此代码在'Activity'部分工作。但在这里错误返回 –
我跟着http://*.com/questions/16481334/check-网络连接片段 –
尝试这种方式。可帮助您
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) getActivity().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;
}
和片段类
if (RootView!= null)
{
isConnectingToInternet();
}
还没有工作。非常奇怪的情况 –
更新你的logcat –
两次打印'返回true;' –
You can write one class for check internet connection:
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class ConnectionDetector {
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;
}
}
And After that you can check connection in your fragment by using below code:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
cd = new ConnectionDetector(getActivity());
isInternetPresent = cd.isConnectingToInternet();
if (isInternetPresent) {
// Internet Connection is Present
Toast.makeText(getActivity(), "Connected", Toast.LENGTH_LONG).show();
} else {
// Internet connection is not present
Toast.makeText(getActivity(), "Not Connected!", Toast.LENGTH_LONG).show();
}
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank, container, false);
}
#vKuldeep Kulkarni:我测试过了。 –
你检查在其他actvities这个代码? –
@beardedbeast它在活动部分工作正常。但抽象类有问题。 –