我在 MainClass 中有两个看起来像这样的方法......
public boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
public void setUI() {
final AlertDialog.Builder builder;
builder = new AlertDialog.Builder(MainActivity.this);
builder.setCancelable(false);
builder.setTitle("No internet connection");
builder.setMessage("message ")
.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if connection is back, dialod dissapears, else pops out again
if (!isNetworkAvailable()) {
builder.show();
} else {
}
}
});
// .setNegativeButton("Close", new DialogInterface.OnClickListener() {
// public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog --> soft-close the app?
// finish();
//} });
if (!isNetworkAvailable()) {
builder.show();
} is this possible to call this 2 methods in a fragment?Thank you in advance.
答案 0 :(得分:0)
我猜你的mainClass扩展了FragmentActiviy
,你有这两种方法
在片段中创建MainClass实例,如
mainclass=(MainClass)getActivity();
并调用类似
的方法mainclass.isNetworkAvailable();
mainclss.setUI();