我有一个连接管理器检查器,我想在很多类中使用,但我不知道如何在另一个类中调用boolean的值。
public class Online {
public static boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni != null && ni.isConnected())
return true;
else
return false;
}
}
第二节课如下:
public class SbyCD extends ListActivity {
ProgressDialog pDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(R.string.tit_cs);
setContentView(R.layout.listview_f_all);
new LoadAll().execute();
}
public void onlineee() {
...
}
public class LoadAll extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
...
}
protected String doInBackground(String... args) {
if (Online.isOnline()) {
...
}
protected void onPostExecute(final String file_url) {
pDialog.dismiss();
if (!Online.isOnline()) {
onlineee();
}
...
}
}
}
我想从第一个得到第二个布尔值(true或false)。
答案 0 :(得分:1)
为什么不用整数来试试呢?应该这样工作:
public class Online {
public static int yon;
public void isOnline() {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni != null && ni.isConnected()){
yon = 1;
}else{
yon = 0;
}
}
答案 1 :(得分:0)
您正在寻找的是帮助方法。
public static boolean isOnline(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
return (ni != null && ni.isConnected())
}
您可以在NameOfClass.isOnline(context)
答案 2 :(得分:0)
我猜你可能希望在网络连接结束时通知Activity。
在这种情况下,您可以创建AsyncTask Singleton以定期检查连接。
你应该定义一个接口说
public interface OnConnectionOutListener{
void onOut();
}
让你的活动实现它
在AsyncTask Singleton中,添加OnConnectionOutListener类型的属性
在您的活动中的onCreate上,获取AsyncTask Singleton并将自己设置为侦听器(Observer模式)
然后,如果在任务中检测到连接输出,请回调listener.onOut()
答案 3 :(得分:0)
将您的方法设为静态
public static boolean isOnline(Context cm) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni != null && ni.isConnected())
return true;
else
return false;
}
只要您想知道您的连接状态,请编写此逻辑。
if(NameOfYourClass.isOnline(NameOfYourClass.this)){
//here do what do you want to do
}
答案 4 :(得分:0)
这样做:
声明监听器接口,例如:
public interface OnConnectionOutListener {
/** notifies Activity when network connectivity is out. */
void onOut();
}
在 SkyCD课程:
中实施public class SbyCD extends ListActivity implements OnConnectionOutListener {
...
//our listener implementation after we detect that connection is out
@Override
public void onOut() {
// ups... we are not connected do some stuff then
}
...
}
然后在 AsyncTask类:
如下:
class LoadAll extends AsyncTask<String, String, String> {
//listener
private OnConnectionOutListener connectionOut;
//context declaration
private Activity _context
//constructor
public LoadAll(Activity _context) {
this._context = _context;
this.connectionOut = (OnConnectionOutListener) _context;
}
//override method on Task pre execution
@Override
protected void onPreExecute() {
super.onPreExecute();
if (!isOnline()) {
connectionOut.onOut();
}
}
//implementation of connection checking
//where **_context** is context passed to your non Activity class LoadAll
public boolean isOnline() {
ConnectivityManager connectivityManager =
(ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
return true;
} else return false;
}
将您的任务作为 SkyCD类传递上下文
中的线程解雇new Thread(new Runnable() {
public void run() {
.... = new LoadAll(this).execute(args);
}
}).start();