我的应用程序主要是基于互联网的,当设备没有互联网时,它会因为尝试下载文件但没有成功而崩溃。我在键入此内容时也刚刚意识到,如果它需要的数据脱机,它也会崩溃。我曾尝试阅读文档并尝试将其添加到我的应用程序中,但似乎我需要一些与我一直在阅读的内容不同的东西。
- 最好在活动中添加“无互联网连接”对话框,而不是通过祝酒词。
- 向可能在 toast 中的片段添加“数据不可用”对话框
我使用我的 MainActivity 来加载片段,所以我怀疑我可以在其中放入某种代码来显示片段使用区域的对话框。无论哪种方式,我都会添加我的 MainActivity 和我的互联网加载片段。
MainActivity.class:
public class MainActivity extends SherlockFragmentActivity {
private ViewPager mViewPager;
private TabSwipe mTabSwipe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new FirstLaunchEULA(this).show();
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.pager);
mViewPager.setOffscreenPageLimit(2);
setContentView(mViewPager);
final ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mTabSwipe = new TabSwipe(this, mViewPager);
mTabSwipe.addTab(bar.newTab().setText("Games"), A_Internet_Service.class, null);
mTabSwipe.addTab(bar.newTab().setText("Movies"), Test_Fragment.class, null);
mTabSwipe.addTab(bar.newTab().setText("Tech"), Purchase_Fragment.class, null);
}
}
A_Internet_Service.class:
public class A_Fragment_Service extends Fragment {
// All static variables
static final String URL = "this.is.a.url";
// XML node keys
static final String KEY_DATA = "data"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_DATE1 = "date1";
static final String KEY_DATE2 = "date2";
static final String KEY_DATE3 = "date3";
static final String KEY_DATE2VIS = "date2vis";
static final String KEY_DATE3VIS = "date3vis";
static final String KEY_PLATFORMS = "platforms";
static final String KEY_THUMB_URL = "thumb_url";
ArrayList<HashMap<String, String>> songsList;
View view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new TheTask().execute();
/*
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
}); */
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
view = inflater.inflate(R.layout.main, container, false);
//list = (ListView) view.findViewById(R.id.data_list);
return view;
}
@Override
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
class TheTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
songsList = new ArrayList<HashMap<String, String>>();
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_DATA);
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_DATE1, parser.getValue(e, KEY_DATE1));
map.put(KEY_DATE2, parser.getValue(e, KEY_DATE2));
map.put(KEY_DATE3, parser.getValue(e, KEY_DATE3));
map.put(KEY_DATE2VIS, parser.getValue(e, KEY_DATE2VIS));
map.put(KEY_DATE3VIS, parser.getValue(e, KEY_DATE3VIS));
map.put(KEY_PLATFORMS, parser.getValue(e, KEY_PLATFORMS));
map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
// adding HashList to ArrayList
songsList.add(map);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Getting adapter by passing xml data ArrayList
LazyAdapter adapter = new LazyAdapter(getActivity(), dataList);
view = getView();
ListView mlist = (ListView) view.findViewById(R.id.data_list);
mlist.setAdapter(adapter);
super.onPostExecute(result);
}
}
}
编辑片段部分:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
view = inflater.inflate(R.layout.main, container, false);
return view;
if(!isConnected(getActivity())) buildDialog(getActivity()).show();
else {
new TheTask().execute();
}
}
public boolean isConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netinfo = cm.getActiveNetworkInfo();
if (netinfo != null && netinfo.isConnectedOrConnecting()) {
android.net.NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if((mobile != null && mobile.isConnectedOrConnecting()) || (wifi != null && wifi.isConnectedOrConnecting())) return true;
else return false;
} else
return false;
}
public AlertDialog.Builder buildDialog(Context c) {
AlertDialog.Builder builder = new AlertDialog.Builder(c);
builder.setTitle("No Internet connection.");
builder.setMessage("You have no internet connection");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
return builder;
}
//All the other code