工具类笔记
正则ip地址:
Pattern IP_ADDRESS = Pattern.compile(
"((25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])\\.(25[0-5]|2[0-4]"
+ "[0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]"
+ "[0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}"
+ "|[1-9][0-9]|[0-9]))");
String GOOD_IRI_CHAR = "a-zA-Z0-9\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF";
WEB_URL = Pattern.compile(
"((?:(http|https|Http|Https):\\/\\/(?:(?:[a-zA-Z0-9\\$\\-\\_\\.\\+\\!\\*\\'\\(\\)"
+ "\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,64}(?:\\:(?:[a-zA-Z0-9\\$\\-\\_"
+ "\\.\\+\\!\\*\\'\\(\\)\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,25})?\\@)?)?"
+ "(?:" + DOMAIN_NAME + ")"
+ "(?:\\:\\d{1,5})?)" // plus option port number
+ "(\\/(?:(?:[" + GOOD_IRI_CHAR + "\\;\\/\\?\\:\\@\\&\\=\\#\\~" // plus option query params
+ "\\-\\.\\+\\!\\*\\'\\(\\)\\,\\_])|(?:\\%[a-fA-F0-9]{2}))*)?"
+ "(?:\\b|$)");
//判断是否安装了google地图
public static boolean isGoogleMapsInstalled(final Context mContext) {
try {
mContext.getPackageManager().getApplicationInfo("com.google.android.apps.maps", 0);
return true;
} catch (PackageManager.NameNotFoundException e) {
if (mContext == null) {
return false;
}
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setMessage("Install Google Maps?");
builder.setCancelable(true);
builder.setPositiveButton(LocaleController.getString("OK", R.string.OK), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.google.android.apps.maps"));
mContext.startActivityForResult(intent, 500);
} catch (Exception e) {
}
}
});
builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
mContext.showDialog(builder.create());
return false;
}
}
//显示键盘
public static void showKeyboard(View view) {
if (view == null) {
return;
}
try {
InputMethodManager inputManager = (InputMethodManager)view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
} catch (Exception e) {
FileLog.e("tmessages", e);
}
}
/**
* 隐藏键盘
* @param view
*/
public static void hideKeyboard(View view) {
if (view == null) {
return;
}
try {
InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (!imm.isActive()) {
return;
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
} catch (Exception e) {
FileLog.e("tmessages", e);
}
}
/**
* 获取文件
* @return
*/
public static File getCacheDir() {
String state = null;
try {
state = Environment.getExternalStorageState();
} catch (Exception e) {
FileLog.e("tmessages", e);
}
if (state == null || state.startsWith(Environment.MEDIA_MOUNTED)) {
try {
File file = ApplicationLoader.applicationContext.getExternalCacheDir();
if (file != null) {
return file;
}
} catch (Exception e) {
FileLog.e("tmessages", e);
}
}
try {
File file = ApplicationLoader.applicationContext.getCacheDir();
if (file != null) {
return file;
}
} catch (Exception e) {
FileLog.e("tmessages", e);
}
return new File("");
}
/**
* 向上取整
* @param value
* @return
*/
public static int dp(float value) {
if (value == 0) {
return 0;
}
/*进一法*/
return (int) Math.ceil(density * value);
}
/**
*
显示光标
* @param editText
*/
public static void clearCursorDrawable(EditText editText) {
if (editText == null) {
return;
}
try {
Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
mCursorDrawableRes.setAccessible(true);
mCursorDrawableRes.setInt(editText, 0);
} catch (Exception e) {
FileLog.e("tmessages", e);
}
}
- /**
- * 添加桌面快捷方式
- */
- private void addShortCurt2DeskTop() {
- Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
- //快捷方式的名称
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "TonyAutoShortCut");
- shortcut.putExtra("duplicate", false); //不允许重复创建
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(LancherDemoActivity.this, R.drawable.beauty));
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(LancherDemoActivity.this,LancherDemoActivity.class).setAction(<span style="color:#FF0000;">"com.android.action.test"</span>));
- //发送广播
- sendBroadcast(shortcut);
- }
- });
- /**
- * 删除桌面快捷方式
- */
- button2.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- deleteShortCurt2DeskTop();
- }
- private void deleteShortCurt2DeskTop() {
- Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
- //快捷方式的名称
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "TonyAutoShortCut");
- shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(LancherDemoActivity.this,LancherDemoActivity.class).setAction(<span style="color:#FF0000;">"com.android.action.test"</span>));
- sendBroadcast(shortcut);
- }
- });
- }
- }
Build.VERSION.SDK_INT---sdk版本

浙公网安备 33010602011771号