Java tutorial
/* Copyright 2015, 2016 Jenly Yu <a href="mailto:jenly1314@gmail.com">Jenly</a> Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package com.king.base.util; import android.app.Activity; import android.app.Instrumentation; import android.content.Context; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; import android.os.Build; import android.provider.MediaStore; import android.provider.Settings; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v4.app.Fragment; import android.support.v4.content.ContextCompat; import android.support.v4.content.FileProvider; import android.telephony.SmsManager; import android.text.TextUtils; import android.view.KeyEvent; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import java.io.File; import java.util.List; /** * @author Jenly <a href="mailto:jenly1314@gmail.com">Jenly</a> */ public class SystemUtils { private SystemUtils() { throw new AssertionError(); } /** * * @param runnable */ public static void asyncThread(Runnable runnable) { new Thread(runnable).start(); } public static boolean isNetWorkActive(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); return activeNetInfo != null && activeNetInfo.isConnected(); } /** * ?? * @param keyCode */ public static void sendKeyCode(final int keyCode) { asyncThread(new Runnable() { @Override public void run() { try { Instrumentation inst = new Instrumentation(); inst.sendKeyDownUpSync(keyCode); } catch (Exception e) { LogUtils.e("Exception when sendPointerSync", e); } } }); } /** * * * @param et */ public static void deleteClick(EditText et) { final KeyEvent keyEventDown = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL); et.onKeyDown(KeyEvent.KEYCODE_DEL, keyEventDown); } /** * ?? * * @param context * @param phoneNumber */ public static void call(Context context, String phoneNumber) { Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(String.format("tel:%s", phoneNumber))); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } /** * ?? * * @param context * @param phoneNumber */ public static void sendSMS(Context context, String phoneNumber) { Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(String.format("smsto:%s", phoneNumber))); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } /** * ? * * @param phoneNumber * @param msg * */ public static void sendSMS(String phoneNumber, String msg) { SmsManager sm = SmsManager.getDefault(); List<String> msgs = sm.divideMessage(msg); for (String text : msgs) { sm.sendTextMessage(phoneNumber, null, text, null, null); } } /** * ? * * @param activity * @param requestCode */ public static void imageCapture(Activity activity, int requestCode) { imageCapture(activity, null, requestCode); } /** * ? * * @param activity * @param path * @param requestCode */ public static void imageCapture(Activity activity, String path, int requestCode) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (!TextUtils.isEmpty(path)) { Uri uri = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { uri = FileProvider.getUriForFile(activity, activity.getPackageName() + ".fileProvider", new File(path)); } else { uri = Uri.fromFile(new File(path)); } intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); } activity.startActivityForResult(intent, requestCode); } /** * ? * * @param fragment * @param path * @param requestCode */ public static void imageCapture(Fragment fragment, String path, int requestCode) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (!TextUtils.isEmpty(path)) { Uri uri = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { uri = FileProvider.getUriForFile(fragment.getContext(), fragment.getContext().getPackageName() + ".fileProvider", new File(path)); } else { uri = Uri.fromFile(new File(path)); } intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); } fragment.startActivityForResult(intent, requestCode); } /** * ?? * * @param context * @return */ public static PackageInfo getPackageInfo(Context context) { PackageInfo packageInfo = null; try { packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0); } catch (PackageManager.NameNotFoundException e) { LogUtils.e(e); } catch (Exception e) { LogUtils.e(e); } return packageInfo; } /** * ???? * * @param context * @return */ public static String getVersionName(Context context) { PackageInfo packageInfo = getPackageInfo(context); return packageInfo != null ? packageInfo.versionName : null; } /** * ??? * * @param context * @return */ public static int getVersionCode(Context context) { PackageInfo packageInfo = getPackageInfo(context); return packageInfo != null ? packageInfo.versionCode : 0; } /** * app? * @param context */ public static void startAppDetailSetings(Context context) { Uri uri = Uri.fromParts("package", context.getPackageName(), null); Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, uri); context.startActivity(intent); } /** * apk * @param context * @param path */ public static void installApk(Context context, String path) { installApk(context, new File(path)); } /** * apk * @param context * @param file */ public static void installApk(Context context, File file) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri uriData = null; String type = "application/vnd.android.package-archive"; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); uriData = FileProvider.getUriForFile(context, context.getPackageName() + ".fileProvider", file); } else { uriData = Uri.fromFile(file); } intent.setDataAndType(uriData, type); context.startActivity(intent); } /** * ?apk * @param context * @param packageName */ public static void uninstallApk(Context context, String packageName) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri uriData = Uri.parse("package:" + packageName); intent.setData(uriData); context.startActivity(intent); } /** * ? * @param context */ public static void uninstallApk(Context context) { uninstallApk(context, context.getPackageName()); } /** * ?? * @param context * @param permission * @return */ public static boolean checkSelfPermission(Context context, @NonNull String permission) { return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED; } /** * ?? * @param activity * @param permission * @param requestCode */ public static void requestPermission(Activity activity, @NonNull String permission, int requestCode) { ActivityCompat.requestPermissions(activity, new String[] { permission }, requestCode); } /** * ? * @param activity * @param permission */ public static void shouldShowRequestPermissionRationale(Activity activity, @NonNull String permission) { ActivityCompat.shouldShowRequestPermissionRationale(activity, permission); } /** * ?? * * @param context * @param v */ public static void hideInputMethod(Context context, EditText v) { InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(v.getWindowToken(), 0); } /** * * * @param context * @param v */ public static void showInputMethod(Context context, EditText v) { v.requestFocus(); InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT); } }