android小片段

一,Toast:


显示在屏幕中间

Toast localToast = Toast.makeText(getApplicationContext(),
						"当前网络不可用", Toast.LENGTH_SHORT);
				localToast.setGravity(0, 0, 0);
				localToast.show();

自定义toast的view

public static void showTimePop(Context paramContext) {
		View localView = ((LayoutInflater) paramContext
				.getSystemService("layout_inflater")).inflate(R.layout.poptime,
				null, false);
		TextView tv = (TextView) localView.findViewById(R.id.textView_time);
		tv.setTextColor(android.graphics.Color.RED);
		tv.setText("当前网络不可用");
		Toast localToast = new Toast(paramContext.getApplicationContext());
		localToast.setGravity(0, 0, 0);
		localToast.setDuration(1);
		localToast.setView(localView);
		localToast.show();
	}

二,java时间


获取当前时,分

public static String getCurrentTime() {
		Calendar localCalendar = Calendar.getInstance();
		int i = localCalendar.get(11);
		int j = localCalendar.get(12);
		return "当前时间:" + i + "时" + j + "分";
	}

获取今天日期和明天日期

	// 获取今天和明天的日期
	// /MM月dd日HH时mm分ss秒 yyyy-MM.dd
	private String getTodayDate() {
		SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日");
		Date curDate = new Date(System.currentTimeMillis());
		return formatter.format(curDate);
	}

	private String getTomorrowDate() {
		GregorianCalendar gc = new GregorianCalendar();
		gc.add(GregorianCalendar.DATE, 1);
		SimpleDateFormat ymdf = new SimpleDateFormat("MM月dd日");
		return ymdf.format(new Date(gc.getTimeInMillis()));
	}

获取今天星期几

public String getWeekDayString() {
		String weekString = "";
		final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五",
				"星期六" };
		Calendar calendar = Calendar.getInstance();
		Date date = new Date();
		calendar.setTime(date);
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
		weekString = dayNames[dayOfWeek - 1];
		return weekString;
	}

三,SD卡和文件相关的操作

SD卡是否准备就绪

public boolean isCanUseSdCard() {
		try {
			return Environment.getExternalStorageState().equals(
					Environment.MEDIA_MOUNTED);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}


在SD卡中创建文件夹

public String createSDCardDir(String addPath) {
		String path = null;
		try {
			if (isCanUseSdCard()) {
				path = Environment.getExternalStorageDirectory().getPath()
						+ addPath;
				File f = new File(path);
				File file = f.getParentFile();
				file.mkdirs();
				f.createNewFile();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return path;
	}

删除某文件夹下的文件

public static boolean deletefile(String delpath)
			throws FileNotFoundException, IOException {
		try {
			File file = new File(delpath);
			if (!file.isDirectory()) {
				file.delete();
			} else if (file.isDirectory()) {
				String[] filelist = file.list();
				for (int i = 0; i < filelist.length; i++) {
					File delfile = new File(delpath + "\\" + filelist[i]);
					if (!delfile.isDirectory()) {
						delfile.delete();
					} else if (delfile.isDirectory()) {
						deletefile(delpath + "\\" + filelist[i]);
					}
				}
				file.delete();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return true;
	}

递归查找某文件夹下的文件

public static boolean readfile(String filepath)
			throws FileNotFoundException, IOException {
		List<String> list = new ArrayList<String>();
		try {
			File file = new File(filepath);
			if (file.isDirectory()) {
				String[] filelist = file.list();
				for (int i = 0; i < filelist.length; i++) {
					File readfile = new File(filepath + "\\" + filelist[i]);
					if (!readfile.isDirectory()) {
						list.add(readfile.getPath()
								+ readfile.getAbsolutePath()
								+ readfile.getName());
					} else if (readfile.isDirectory()) {
						readfile(filepath + "\\" + filelist[i]);
					}
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return true;
	}



三,网络相关

检测手机网络环境

public boolean CheckNetworkState() {
		ConnectivityManager manager = (ConnectivityManager) act
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		State mobile = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
				.getState();
		State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
				.getState();
		if (mobile == State.CONNECTED || mobile == State.CONNECTING) {
			return true;
		} else if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
			return true;
		}
		return false;
	}

无网络时提示用户,是否去设置网络

public void showTips() {
		AlertDialog.Builder builder = new AlertDialog.Builder(act);
		builder.setIcon(android.R.drawable.ic_dialog_alert);
		builder.setTitle("无网络");
		builder.setMessage("是否现在去设置网络连接?");
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				act
						.startActivity(new Intent(
								Settings.ACTION_WIRELESS_SETTINGS));
			}
		});
		builder.setNegativeButton("退出", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
				System.exit(0);
			}
		});
		builder.create();
		builder.show();
	}


最小化应用

public void showMiniDialog(String tit, String content, String posi,
			String nega) {
		new AlertDialog.Builder(act).setTitle(tit).setMessage(content)
				.setPositiveButton(posi, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						Intent intent = new Intent(Intent.ACTION_MAIN);
						intent.addCategory(Intent.CATEGORY_HOME);
						act.startActivity(intent);
						act.finish();
					}
				}).setNegativeButton(nega,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface arg0, int arg1) {
								act.finish();
								System.exit(0);
							}
						}).show();
	}

联网加载进度框

public void showProgressDialog(String title, String content) {
		mpDialog = new ProgressDialog(act);
		mpDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		mpDialog.setTitle(title);
		mpDialog.setIcon(R.drawable.ic_launcher);
		mpDialog.setMessage(content);
		mpDialog.setIndeterminate(false);// 设置进度条是否为不明确
		mpDialog.setCancelable(true);// 设置进度条是否可以按退回键取消
		mpDialog.show();
	}

	public void cancleProgressDialog() {
		if (mpDialog.isShowing()) {
			mpDialog.cancel();
		}
	}


四,Activity相关设置

这个没什么好说的。去app的name,系统的标题栏,设置屏幕方向

public void avoidAppTitle() {
		act.requestWindowFeature(Window.FEATURE_NO_TITLE);
	}

	public void avoidSysTitle() {
		act.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
	}

	public void setScreen() {
		act.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

	}

五,取手机硬件和SIM参数

有SIM卡,并且可用

private boolean checkSIMStatu() {
		TelephonyManager mTelephonyManager = (TelephonyManager) act
				.getSystemService(Service.TELEPHONY_SERVICE);
		if (mTelephonyManager.getSimState() != TelephonyManager.SIM_STATE_READY) {
			return false;
		} else {
			ConnectivityManager cManager = (ConnectivityManager) act
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo info = cManager.getActiveNetworkInfo();
			if (info != null && info.isAvailable()) {
				return true;
			} else {
				return false;
			}
		}
	}



你可能感兴趣的:(android,Date,String,calendar,File,dialog)