关键字:网络状态,系统设置,系统时间,时间格式化
首先是判断系统的网络状态
/**
* 对网络连接状态进行判断
* @return true, 可用; false, 不可用
*/
private boolean isOpenNetwork() {
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (connManager.getActiveNetworkInfo() != null) {
return connManager.getActiveNetworkInfo().isAvailable();
}
return false;
}
PS:你肯定要加入网络访问的权限
[html]
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
如果没有网络则进入设置界面
[java]
if (isOpenNetwork() == true) {
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(
SomeActivity.this);
builder.setTitle("title")
.setMessage("message");
builder.setPositiveButton(resources.getString(R.string.ok),
new DialogInte易做图ce.OnClickListener() {
@Override
public void onClick(DialogInte易做图ce dialog, int which) {
<span style="white-space:pre"> </span>Intent intent = null;
try {
String sdkVersion = android.os.Build.VERSION.SDK;
if (Integer.valueOf(sdkVersion) > 10) {
intent = new Intent(
android.provider.Settings.ACTION_SETTINGS);
} else {
intent = new Intent();
ComponentName comp = new ComponentName(
"com.android.settings",
"com.android.settings.Settings");
intent.setComponent(comp);
intent.setAction("android.intent.action.VIEW");
}
SomeActivity.this.startActivity(intent);
} catch (Exception e) {
Log.v("LOG","open network settings failed, please check...");
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>}
}
})
.setNegativeButton(resources.getString(R.string.no),
new DialogInte易做图ce.OnClickListener() {
@Override
public void onClick(DialogInte易做图ce dialog,int which) {
dialog.cancel();
finish();
}
}).show();
}
这里再引申一下,通过android.provider.Settings可以获取很多我们需要的和系统设置相关的信息,比如系统时间是24还是12制
[java]
ContentResolver cv = this.getContentResolver();
String strTimeFormat = android.provider.Settings.System.getString(cv,
android.provider.Settings.System.TIME_12_24);
if(strTimeFormat.equals("24"))
{
Log.i("activity","24");
}
额。。这篇博客写得比较杂,那就把时间格式化,获取系统时间也补上吧
[java]
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM"); //参数为你想要显示的时间格式
String date=sdf.format(new java.util.Date());
利用Calendar获取
[java]
Calendar c = Calendar.getInstance();
取得系统日期:year = c.get(Calendar.YEAR)
month = c.grt(Calendar.MONTH)
day = c.get(Calendar.DAY_OF_MONTH)
取得系统时间:hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE)
Calendar c = Calendar.getInstance();
取得系统日期:year = c.get(Calendar.YEAR)
month = c.grt(Calendar.MONTH)
day = c.get(Calendar.DAY_OF_MONTH)
取得系统时间:hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE)
利用Time获取
[java]
Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料。
t.setToNow(); // 取得系统时间。
int year = t.year;
int month = t.month;