com.blankj.utilcode.util.Utils Java Examples
The following examples show how to use
com.blankj.utilcode.util.Utils.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: App.java From V2EX with GNU General Public License v3.0 | 6 votes |
@Override public void onCreate() { super.onCreate(); app = this; mSecretConfig = new SecretImpl(); mSecretConfig.init(this); Config.init(this); setTheme(getResources().getIdentifier(Config.getConfig(ConfigRefEnum.CONFIG_THEME), "style", getPackageName())); Logger.addLogAdapter(new AndroidLogAdapter()); RetrofitManager.init(this); Utils.init(this); registerActivityLifecycleCallbacks(this); setFontScaleAndUiScale(); }
Example #2
Source File: DangerousUtils.java From AndroidUtilCode with Apache License 2.0 | 6 votes |
/** * Enable or disable mobile data. * <p>Must hold {@code android:sharedUserId="android.uid.system"}, * {@code <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />}</p> * * @param enabled True to enabled, false otherwise. * @return {@code true}: success<br>{@code false}: fail */ @RequiresPermission(MODIFY_PHONE_STATE) public static boolean setMobileDataEnabled(final boolean enabled) { try { TelephonyManager tm = (TelephonyManager) Utils.getApp().getSystemService(Context.TELEPHONY_SERVICE); if (tm == null) return false; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { tm.setDataEnabled(enabled); return true; } Method setDataEnabledMethod = tm.getClass().getDeclaredMethod("setDataEnabled", boolean.class); setDataEnabledMethod.invoke(tm, enabled); return true; } catch (Exception e) { Log.e("NetworkUtils", "setMobileDataEnabled: ", e); } return false; }
Example #3
Source File: OnlineMusicActivity.java From YCAudioPlayer with Apache License 2.0 | 6 votes |
/** * 播放音乐 * @param onlineMusic onlineMusic */ private void playMusic(OnlineMusicList.SongListBean onlineMusic) { new AbsPlayOnlineMusic(this, onlineMusic) { @Override public void onPrepare() { } @Override public void onExecuteSuccess(AudioBean music) { getPlayService().play(music); showPlayingFragment(); ToastUtils.showRoundRectToast("正在播放" + music.getTitle()); } @Override public void onExecuteFail(Exception e) { ToastUtils.showRoundRectToast(Utils.getApp().getResources().getString(R.string.unable_to_play)); } }.execute(); }
Example #4
Source File: LockModificationActivity.java From SuperNote with GNU General Public License v3.0 | 6 votes |
private boolean firstDrawPass(List<Integer> passPositions) { if (passPositions.size() < 4) { mTvTip.setText("至少连接四个点,请重试"); return false; } else { mLastPassword = getStringForList(passPositions); mTvTip.setText("已记录图案"); android.os.Handler handler = new android.os.Handler(); handler.postDelayed(new Runnable() { @Override public void run() { mLickView.resetPoints(); mDrawTimes++; mTvTip.setText("再次绘制图案进行确认"); mLlRoot.setVisibility(View.VISIBLE); mBtnOk.setClickable(false); mBtnOk.setTextColor(Utils.getContext().getResources().getColor(R.color.colorBlackAlpha26)); } }, 1 * 1000); return true; } }
Example #5
Source File: EditNotePresenter.java From SuperNote with GNU General Public License v3.0 | 6 votes |
@Override public void clickNoteEditText(MyEditText editText) { // 获取光标位置 int selectionAfter = editText.getSelectionStart(); Logger.d("光标位置:" + selectionAfter); for (int i = 0; i < editText.mImageList.size(); i++) { ImageEntity imageEntity = editText.mImageList.get(i); if (selectionAfter >= imageEntity.getStart() && selectionAfter <= imageEntity.getEnd()) { // 光标位置在照片的位置内 Logger.d("起点:" + imageEntity.getStart() + " 终点:" + imageEntity.getEnd()); // 隐藏键盘 InputMethodManager imm = (InputMethodManager) Utils.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); // 光标移到图片末尾的换行符后面 editText.setSelection(imageEntity.getEnd() + 1); mSelectedImageEntity = imageEntity; toImageInfoActivity(); break; } } }
Example #6
Source File: LocationUtils.java From AndroidUtilCode with Apache License 2.0 | 6 votes |
/** * 注册 * <p>使用完记得调用{@link #unregister()}</p> * <p>需添加权限 {@code <uses-permission android:name="android.permission.INTERNET" />}</p> * <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />}</p> * <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />}</p> * <p>如果{@code minDistance}为0,则通过{@code minTime}来定时更新;</p> * <p>{@code minDistance}不为0,则以{@code minDistance}为准;</p> * <p>两者都为0,则随时刷新。</p> * * @param minTime 位置信息更新周期(单位:毫秒) * @param minDistance 位置变化最小距离:当位置距离变化超过此值时,将更新位置信息(单位:米) * @param listener 位置刷新的回调接口 * @return {@code true}: 初始化成功<br>{@code false}: 初始化失败 */ @RequiresPermission(ACCESS_FINE_LOCATION) public static boolean register(long minTime, long minDistance, OnLocationChangeListener listener) { if (listener == null) return false; mLocationManager = (LocationManager) Utils.getApp().getSystemService(Context.LOCATION_SERVICE); if (!mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) && !mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { Log.d("LocationUtils", "无法定位,请打开定位服务"); return false; } mListener = listener; String provider = mLocationManager.getBestProvider(getCriteria(), true); Location location = mLocationManager.getLastKnownLocation(provider); if (location != null) listener.getLastKnownLocation(location); if (myLocationListener == null) myLocationListener = new MyLocationListener(); mLocationManager.requestLocationUpdates(provider, minTime, minDistance, myLocationListener); return true; }
Example #7
Source File: Configurator.java From HHComicViewer with Apache License 2.0 | 6 votes |
public final void configure() { //初始化OkHttpClient if (HH_CONFIGS.get(ConfigKeys.OKHTTP_CLIENT) == null) { //添加interceptor for (Interceptor interceptor : INTERCEPTORS) { BUILDER.addInterceptor(interceptor); } HH_CONFIGS.put(ConfigKeys.OKHTTP_CLIENT, BUILDER.build()); } //初始化Utils库 Utils.init((Application) HHEngine.getApplicationContext()); //初始化icon库 initIcons(); //初始化logger库 Logger.addLogAdapter(new AndroidLogAdapter()); HH_CONFIGS.put(ConfigKeys.CONFIG_READY, true); }
Example #8
Source File: RvNoteListAdapter.java From SuperNote with GNU General Public License v3.0 | 6 votes |
/** * 设置网格布局 * * @describe */ private void setGridLayout(BaseViewHolder helper, Note item) { helper.addOnClickListener(R.id.cv_note_list_grid); helper.addOnLongClickListener(R.id.cv_note_list_grid); helper.setVisible(R.id.ll_note_list_linear, false); helper.setVisible(R.id.cv_note_list_grid, true); TextView tvContent=helper.getView(R.id.tv_note_list_grid_content); if(isPrivacyAndRecycle(item)) helper.setText(R.id.tv_note_list_grid_content,Utils.getContext().getResources().getString(R.string.note_privacy_and_recycle)); else parseTextContent(tvContent,item.getNoteContent()); // 设置便签的时间显示 setNoteTime(helper, item.getModifiedTime()); // 设置多选按钮 setCheckBox(helper); }
Example #9
Source File: RvNoteListAdapter.java From SuperNote with GNU General Public License v3.0 | 6 votes |
/** * 设置线性布局 * * @describe */ private void setLinearLayout(BaseViewHolder helper, Note item) { helper.addOnClickListener(R.id.ll_note_list_line); helper.addOnLongClickListener(R.id.ll_note_list_line); // 显示竖排布局,隐藏网格布局 helper.setVisible(R.id.cv_note_list_grid, false); helper.setVisible(R.id.ll_note_list_linear, true); TextView tvContent=helper.getView(R.id.tv_note_list_linear_content); if(isPrivacyAndRecycle(item)) helper.setText(R.id.tv_note_list_linear_content,Utils.getContext().getResources().getString(R.string.note_privacy_and_recycle)); else parseTextContent(tvContent,item.getNoteContent()); // 设置便签的时间显示 setNoteTime(helper, item.getModifiedTime()); // 设置便签的分组显示 setLinearLayoutGroup(helper, item.getCreatedTime()); // 设置多选按钮 setCheckBox(helper); }
Example #10
Source File: BatteryUtils.java From AndroidUtilCode with Apache License 2.0 | 6 votes |
void registerListener(final OnBatteryStatusChangedListener listener) { if (listener == null) return; ThreadUtils.runOnUiThread(new Runnable() { @SuppressLint("MissingPermission") @Override public void run() { int preSize = mListeners.size(); mListeners.add(listener); if (preSize == 0 && mListeners.size() == 1) { IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED); Utils.getApp().registerReceiver(BatteryChangedReceiver.getInstance(), intentFilter); } } }); }
Example #11
Source File: App.java From AndroidSamples with Apache License 2.0 | 5 votes |
@Override public void onCreate() { instance = this; super.onCreate(); Utils.init(this); initLog(); }
Example #12
Source File: ClipboardUtils.java From AndroidUtilCode with Apache License 2.0 | 5 votes |
/** * 获取剪贴板的文本 * * @return 剪贴板的文本 */ public static CharSequence getText() { ClipboardManager cm = (ClipboardManager) Utils.getApp().getSystemService(Context.CLIPBOARD_SERVICE); ClipData clip = cm.getPrimaryClip(); if (clip != null && clip.getItemCount() > 0) { return clip.getItemAt(0).coerceToText(Utils.getApp()); } return null; }
Example #13
Source File: DangerousUtils.java From AndroidUtilCode with Apache License 2.0 | 5 votes |
/** * Reboot the device. * <p>Requires root permission * or hold {@code android:sharedUserId="android.uid.system"} in manifest.</p> * * @return {@code true}: success<br>{@code false}: fail */ public static boolean reboot() { try { ShellUtils.CommandResult result = ShellUtils.execCmd("reboot", true); if (result.result == 0) return true; Intent intent = new Intent(Intent.ACTION_REBOOT); intent.putExtra("nowait", 1); intent.putExtra("interval", 1); intent.putExtra("window", 0); Utils.getApp().sendBroadcast(intent); return true; } catch (Exception e) { return false; } }
Example #14
Source File: DangerousUtils.java From AndroidUtilCode with Apache License 2.0 | 5 votes |
/** * Shutdown the device * <p>Requires root permission * or hold {@code android:sharedUserId="android.uid.system"}, * {@code <uses-permission android:name="android.permission.SHUTDOWN" />} * in manifest.</p> * * @return {@code true}: success<br>{@code false}: fail */ public static boolean shutdown() { try { ShellUtils.CommandResult result = ShellUtils.execCmd("reboot -p", true); if (result.result == 0) return true; Utils.getApp().startActivity(IntentUtils.getShutdownIntent()); return true; } catch (Exception e) { return false; } }
Example #15
Source File: LocationUtils.java From AndroidUtilCode with Apache License 2.0 | 5 votes |
/** * 根据经纬度获取地理位置 * * @param latitude 纬度 * @param longitude 经度 * @return {@link Address} */ public static Address getAddress(double latitude, double longitude) { Geocoder geocoder = new Geocoder(Utils.getApp(), Locale.getDefault()); try { List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1); if (addresses.size() > 0) return addresses.get(0); } catch (IOException e) { e.printStackTrace(); } return null; }
Example #16
Source File: BatteryUtils.java From AndroidUtilCode with Apache License 2.0 | 5 votes |
void unregisterListener(final OnBatteryStatusChangedListener listener) { if (listener == null) return; ThreadUtils.runOnUiThread(new Runnable() { @Override public void run() { int preSize = mListeners.size(); mListeners.remove(listener); if (preSize == 1 && mListeners.size() == 0) { Utils.getApp().unregisterReceiver(BatteryChangedReceiver.getInstance()); } } }); }
Example #17
Source File: AppStoreUtils.java From AndroidUtilCode with Apache License 2.0 | 5 votes |
private static boolean isAppSystem(final String packageName) { if (TextUtils.isEmpty(packageName)) return false; try { PackageManager pm = Utils.getApp().getPackageManager(); ApplicationInfo ai = pm.getApplicationInfo(packageName, 0); return ai != null && (ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return false; } }
Example #18
Source File: CountryUtils.java From AndroidUtilCode with Apache License 2.0 | 5 votes |
/** * Return the country by sim card. * * @return the country */ public static String getCountryBySim() { TelephonyManager manager = (TelephonyManager) Utils.getApp().getSystemService(Context.TELEPHONY_SERVICE); if (manager != null) { return manager.getSimCountryIso().toUpperCase(); } return ""; }
Example #19
Source File: App.java From EasyPopup with Apache License 2.0 | 5 votes |
@Override public void onCreate() { super.onCreate(); // init it in the function of onCreate in ur Application Utils.init(this); }
Example #20
Source File: MyUtil.java From ImageLoader with Apache License 2.0 | 5 votes |
/** * 复制文本到剪贴板 * * @param text 文本 */ public static void copyText(final CharSequence text) { //部分机型出现SecurityException //http://10.0.20.7/zentao/bug-view-22357.html try { ClipboardManager clipboard = (ClipboardManager) Utils.getApp().getSystemService(Context.CLIPBOARD_SERVICE); clipboard.setPrimaryClip(ClipData.newPlainText("text", text)); } catch (Exception e) { e.printStackTrace(); } }
Example #21
Source File: DangerousUtils.java From AndroidUtilCode with Apache License 2.0 | 5 votes |
/** * Send sms silently. * <p>Must hold {@code <uses-permission android:name="android.permission.SEND_SMS" />}</p> * * @param phoneNumber The phone number. * @param content The content. */ @RequiresPermission(SEND_SMS) public static void sendSmsSilent(final String phoneNumber, final String content) { if (TextUtils.isEmpty(content)) return; PendingIntent sentIntent = PendingIntent.getBroadcast(Utils.getApp(), 0, new Intent("send"), 0); SmsManager smsManager = SmsManager.getDefault(); if (content.length() >= 70) { List<String> ms = smsManager.divideMessage(content); for (String str : ms) { smsManager.sendTextMessage(phoneNumber, null, str, sentIntent, null); } } else { smsManager.sendTextMessage(phoneNumber, null, content, sentIntent, null); } }
Example #22
Source File: CommonItemSwitch.java From AndroidUtilCode with Apache License 2.0 | 5 votes |
public CommonItemSwitch(@NonNull CharSequence title, @NonNull Utils.Supplier<Boolean> getStateSupplier, @NonNull Utils.Consumer<Boolean> setStateConsumer) { super(R.layout.common_item_title_switch); mTitle = title; mGetStateSupplier = getStateSupplier; mSetStateConsumer = setStateConsumer; mState = getStateSupplier.get(); mContent = String.valueOf(mState); }
Example #23
Source File: ClipboardUtils.java From AndroidUtilCode with Apache License 2.0 | 5 votes |
/** * 获取剪贴板的意图 * * @return 剪贴板的意图 */ public static Intent getIntent() { ClipboardManager cm = (ClipboardManager) Utils.getApp().getSystemService(Context.CLIPBOARD_SERVICE); ClipData clip = cm.getPrimaryClip(); if (clip != null && clip.getItemCount() > 0) { return clip.getItemAt(0).getIntent(); } return null; }
Example #24
Source File: AppStoreUtils.java From AndroidUtilCode with Apache License 2.0 | 5 votes |
private static boolean go2NormalAppStore(String packageName) { Intent intent = getNormalAppStoreIntent(); if (intent == null) return false; intent.setData(Uri.parse("market://details?id=" + packageName)); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Utils.getApp().startActivity(intent); return true; }
Example #25
Source File: MvpModel.java From AndroidUtilCode with Apache License 2.0 | 5 votes |
@Override public void requestUpdateMsg(final Utils.Consumer<String> consumer) { ThreadUtils.executeByCached(addAutoDestroyTask(new ThreadUtils.SimpleTask<String>() { @Override public String doInBackground() throws Throwable { Thread.sleep(2000); return "msg: " + index++; } @Override public void onSuccess(String result) { consumer.accept(result); } })); }
Example #26
Source File: MvpPresenter.java From AndroidUtilCode with Apache License 2.0 | 5 votes |
@Override public void updateMsg() { getView().setLoadingVisible(true); getModel(MvpModel.class).requestUpdateMsg(new Utils.Consumer<String>() { @Override public void accept(String s) { getView().showMsg(s); getView().setLoadingVisible(false); } }); }
Example #27
Source File: EditNotePresenter.java From SuperNote with GNU General Public License v3.0 | 5 votes |
@Override public Bitmap getNoteShareBitmap(View view) { Bitmap bitmap = ImageUtils.view2Bitmap(view); int x = bitmap.getWidth() - SizeUtils.sp2px(72); int y = bitmap.getHeight() - SizeUtils.sp2px(16); int textWaterMarkColor = Utils.getContext().getResources().getColor(R.color.colorBlackAlpha54); bitmap = ImageUtils.addTextWatermark(bitmap, EditNoteConstans.watermarkText, 24, textWaterMarkColor, x, y); return bitmap; }
Example #28
Source File: LockModificationActivity.java From SuperNote with GNU General Public License v3.0 | 5 votes |
private boolean secondDrawPass(List<Integer> passPositions) { if (mLastPassword.equals(getStringForList(passPositions))) { mTvTip.setText("请确认您的密码图案"); mBtnOk.setClickable(true); mBtnOk.setTextColor(Utils.getContext().getResources().getColor(R.color.colorBlackAlpha87)); mLickView.setClickable(false); mPassword = MD5Util.getMd5Value(mLastPassword); return true; } else { mTvTip.setText("两次密码不一样,请重试"); mBtnOk.setClickable(false); return false; } }
Example #29
Source File: BaseApplication.java From Android-IM with Apache License 2.0 | 5 votes |
@Override public void onCreate() { super.onCreate(); baseApplication = this; MultiDex.install(this); sharedPrefHelper = SharedPrefHelper.getInstance(); sharedPrefHelper.setRoaming(true); //开启极光调试 JPushInterface.setDebugMode(true); mContext = BaseApplication.this; //实例化极光推送 JPushInterface.init(mContext); //实例化极光IM,并自动同步聊天记录 JMessageClient.init(getApplicationContext(), true); JMessageClient.setDebugMode(true); //初始化极光sms // SMSSDK.getInstance().initSdk(mContext); //初始化数据库 setupDatabase(); //通知管理,通知栏开启,其他关闭 JMessageClient.setNotificationFlag(FLAG_NOTIFY_SILENCE); //初始化utils Utils.init(this); //推送状态 initJPush2(); //初始化统计 JAnalyticsInterface.init(mContext); JAnalyticsInterface.setDebugMode(true); }
Example #30
Source File: CoverLoader.java From YCAudioPlayer with Apache License 2.0 | 5 votes |
/** * 获取默认的bitmap视图 * @param type 类型 * @return bitmap对象 */ private Bitmap getDefaultCover(Type type) { switch (type) { case BLUR: return BitmapFactory.decodeResource(Utils.getApp().getResources(), R.drawable.default_cover); case ROUND: Bitmap bitmap = BitmapFactory.decodeResource(Utils.getApp().getResources(), R.drawable.default_cover); bitmap = ImageUtils.resizeImage(bitmap, ScreenUtils.getScreenWidth() / 2, ScreenUtils.getScreenWidth() / 2); return bitmap; default: return BitmapFactory.decodeResource(Utils.getApp().getResources(), R.drawable.default_cover); } }