Java tutorial
/* * Copyright (C) 2018 AlexMofer * * 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 am.project.x.utils; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.DhcpInfo; import android.net.Uri; import android.net.wifi.WifiManager; import android.os.Build; import android.support.annotation.Nullable; import android.support.v4.app.ActivityCompat; /** * Context */ public class ContextUtils { private ContextUtils() { //no instance } /** * ?? * * @param context Context * @param url ? */ public static void openBrowser(Context context, String url) { final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); if (intent.resolveActivity(context.getPackageManager()) != null) { context.startActivity(intent); } } /** * ??? * * @param context Context * @param subject * @param attachment * @param addresses ? */ public static void sendEmail(Context context, @Nullable String subject, @Nullable Uri attachment, String... addresses) { Intent intent = new Intent(Intent.ACTION_SENDTO); intent.setData(Uri.parse("mailto:")); intent.putExtra(Intent.EXTRA_EMAIL, addresses); if (subject != null) intent.putExtra(Intent.EXTRA_SUBJECT, subject); if (attachment != null) intent.putExtra(Intent.EXTRA_STREAM, attachment); if (intent.resolveActivity(context.getPackageManager()) != null) { context.startActivity(intent); } } /** * ??? * * @param context Context * @return ??? */ @SuppressWarnings("BooleanMethodIsAlwaysInverted") public static boolean hasWriteExternalStoragePermission(Context context) { return Build.VERSION.SDK_INT < 23 || ActivityCompat.checkSelfPermission(context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED; } /** * WIFI? * * @param context Context * @return true: false: */ @SuppressWarnings("BooleanMethodIsAlwaysInverted") public static boolean isWifiConnected(Context context) { final WifiManager manager = (WifiManager) context.getApplicationContext() .getSystemService(Context.WIFI_SERVICE); if (manager == null) return false; final DhcpInfo info = manager.getDhcpInfo(); return info != null && info.ipAddress != 0; } /** * ?WIFI IP? * * @param context Context * @return IP? */ public static String getWifiIp(Context context) { final WifiManager manager = (WifiManager) context.getApplicationContext() .getSystemService(Context.WIFI_SERVICE); if (manager == null) return "0.0.0.0"; final DhcpInfo info = manager.getDhcpInfo(); if (info == null) return "0.0.0.0"; final int ip = info.ipAddress; return (0xFF & ip) + "." + (0xFF & ip >> 8) + "." + (0xFF & ip >> 16) + "." + (0xFF & ip >> 24); } /** * ??Intent * * @param context Context * @return ?Intent */ @SuppressWarnings("unused") public static Intent getLaunchIntent(Context context) { //noinspection ConstantConditions return context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); } }