Last active
May 23, 2016 10:00
-
-
Save lonelylonelygod/15dc6c8a822880df6929da392fb66a1b to your computer and use it in GitHub Desktop.
Get device hardware information at runtime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.content.pm.PackageManager; | |
import android.net.ConnectivityManager; | |
import android.net.NetworkInfo; | |
import android.os.Build; | |
import android.telephony.TelephonyManager; | |
import android.util.DisplayMetrics; | |
import android.view.WindowManager; | |
public class DeviceHelper { | |
public static int getNetwork(Context context) { | |
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); | |
NetworkInfo info = cm.getActiveNetworkInfo(); | |
if (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI) { | |
return 1; | |
} | |
TelephonyManager mTelephonyManager = (TelephonyManager) | |
context.getSystemService(Context.TELEPHONY_SERVICE); | |
int networkType = mTelephonyManager.getNetworkType(); | |
switch (networkType) { | |
case TelephonyManager.NETWORK_TYPE_GPRS: | |
case TelephonyManager.NETWORK_TYPE_EDGE: | |
case TelephonyManager.NETWORK_TYPE_CDMA: | |
case TelephonyManager.NETWORK_TYPE_1xRTT: | |
case TelephonyManager.NETWORK_TYPE_IDEN: | |
return 2; | |
case TelephonyManager.NETWORK_TYPE_UMTS: | |
case TelephonyManager.NETWORK_TYPE_EVDO_0: | |
case TelephonyManager.NETWORK_TYPE_EVDO_A: | |
case TelephonyManager.NETWORK_TYPE_HSDPA: | |
case TelephonyManager.NETWORK_TYPE_HSUPA: | |
case TelephonyManager.NETWORK_TYPE_HSPA: | |
case TelephonyManager.NETWORK_TYPE_EVDO_B: | |
case TelephonyManager.NETWORK_TYPE_EHRPD: | |
case TelephonyManager.NETWORK_TYPE_HSPAP: | |
return 3; | |
case TelephonyManager.NETWORK_TYPE_LTE: | |
return 4; | |
default: | |
return 6; | |
} | |
} | |
public static String getDisplayResolution(Context context) { | |
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); | |
DisplayMetrics displaymetrics = new DisplayMetrics(); | |
wm.getDefaultDisplay().getMetrics(displaymetrics); | |
int height = displaymetrics.heightPixels; | |
int width = displaymetrics.widthPixels; | |
return height + "×" + width; | |
} | |
public static String getBrand() { | |
return Build.MANUFACTURER + " " + Build.BRAND + " " + Build.MODEL; | |
} | |
public static String getIMEI(Context context) { | |
TelephonyManager telephonyManager = (TelephonyManager) | |
context.getSystemService(Context.TELEPHONY_SERVICE); | |
return telephonyManager.getDeviceId(); | |
} | |
/** | |
* Check if the CPU architecture is X64 | |
* @return | |
*/ | |
public static boolean isX64() { | |
String arch = System.getProperty("os.arch").toLowerCase(); | |
return arch.contains("64"); | |
} | |
/** | |
* Check the application is installed or not | |
* @param context | |
* @param packageName the application package name | |
* @return | |
*/ | |
public static boolean isPackageInstalled(Context context, String packageName) { | |
PackageManager pm = context.getPackageManager(); | |
try { | |
pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES); | |
return true; | |
} catch (PackageManager.NameNotFoundException e) { | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment