关键字:网络状态,系统设置,系统时间,时间格式化
首先是判断系统的网络状态
/** * 对网络连接状态进行判断 * @return true, 可用; false, 不可用 */ private boolean isOpenNetwork() { ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); if (connManager.getActiveNetworkInfo() != null) { return connManager.getActiveNetworkInfo().isAvailable(); } return false; }
PS:你肯定要加入网络访问的权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
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 DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { 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..."); e.printStackTrace(); } } }) .setNegativeButton(resources.getString(R.string.no), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog,int which) { dialog.cancel(); finish(); } }).show(); }这里再引申一下,通过android.provider.Settings可以获取很多我们需要的和系统设置相关的信息,比如系统时间是24还是12制
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"); }
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM"); //参数为你想要显示的时间格式 String date=sdf.format(new java.util.Date());
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 t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料。 t.setToNow(); // 取得系统时间。 int year = t.year; int month = t.month; int date = t.monthDay; int hour = t.hour; // 0-23 int minute = t.minute; int second = t.second;