【发布时间】:2013-11-12 11:31:36
【问题描述】:
我想知道在执行我的一些代码之前是否有一种简单的方法可以检查用户是否有可用的互联网连接。
我想知道这一点,因为如果我不启用我的互联网连接然后单击“Setwallpaper”,应用程序会因为“Nullpointerexception”而崩溃,因为没有任何东西可以执行。
在打开下载和设置壁纸的异步任务类之前,我如何检查用户是否启用了互联网连接?
代码:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.SetWallpaper:
//Do something here that checks if the user has connection so if they don't, it won't execute this code here.
new SaveWallpaperAsync(getActivity()).execute(mImageUrl);
break;
case R.id.SaveWallpaper:
//Do something here that checks if the user has connection so if they don't, it won't execute this code here.
new SetWallpaperAsync(getActivity()).execute(mImageUrl);
break;
}
return super.onOptionsItemSelected(item);
}
【问题讨论】:
-
如果你用谷歌搜索,结果太多了......你为什么不呢?
标签: android