Android Utililty Methods Context Get

List of utility methods to do Context Get

Description

The list of methods to do Context Get are organized into topic(s).

Method

StringgetBatteryUsageInfo(Context context)
Gets the battery info of device, and then writes it to a file.
Intent batteryIntent = context.getApplicationContext()
        .registerReceiver(null,
                new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int rawlevel = batteryIntent.getIntExtra("level", -1);
double scale = batteryIntent.getIntExtra("scale", -1);
return String.format("Device has %s of %s battery left", rawlevel,
        scale);
booleangetBoolean(Context context, String key, boolean defValue)
get Boolean
return getDefaultSharedPreferences(context).getBoolean(key,
        defValue);
StringgetBroadcastAddress(Context context)
get Broadcast Address
WifiManager wifi = (WifiManager) context
        .getSystemService(Context.WIFI_SERVICE);
DhcpInfo dhcp = wifi.getDhcpInfo();
int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
byte[] quads = new byte[4];
for (int k = 0; k < 4; k++)
    quads[k] = (byte) (broadcast >> (k * 8));
try {
...
StringgetCarrier(Context context)
get Carrier
TelephonyManager manager = (TelephonyManager) context
        .getSystemService(Context.TELEPHONY_SERVICE);
return manager.getNetworkOperatorName();
String[]getCertificateFingerprint(Context ctx, String packageName)
Get current certificate fingerprint
try {
    if (ctx == null || ctx.getPackageManager() == null)
        return null;
    PackageInfo info = ctx.getPackageManager().getPackageInfo(
            packageName, PackageManager.GET_SIGNATURES);
    assert info.signatures != null;
    String[] result = new String[info.signatures.length];
    int i = 0;
...
StringgetCertificateFromSDCard(Context context)
get Certificate From SD Card
serverCertificate = "";
String baseDir = Environment.getExternalStorageDirectory()
        .getAbsolutePath();
String certificateName = "localhost.crt";
try {
    InputStream in = new BufferedInputStream(new FileInputStream(
            baseDir + File.separator + certificateName));
    serverCertificate = inputStreamToString(in);
...
StringgetChannelNo(Context context)
get Channel No
String appKey = "";
try {
    ApplicationInfo ai = context.getPackageManager()
            .getApplicationInfo(context.getPackageName(),
                    PackageManager.GET_META_DATA);
    if (null != ai && null != ai.metaData) {
        appKey = ai.metaData.getString("channel");
} catch (Exception e) {
    e.printStackTrace();
return appKey;
intgetConnectedType(Context context)
get Connected Type
if (context != null) {
    ConnectivityManager mConnectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mNetworkInfo = mConnectivityManager
            .getActiveNetworkInfo();
    if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
        return mNetworkInfo.getType();
return -1;
intgetConnectedType(Context context)
get Connected Type
ConnectivityManager cManager = (ConnectivityManager) context
        .getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo nInfo = cManager.getActiveNetworkInfo();
if (nInfo != null && nInfo.isAvailable()) {
    int type = nInfo.getType();
    int subType = nInfo.getSubtype();
    switch (type) {
    case ConnectivityManager.TYPE_MOBILE:
...
StringgetContactDisplayNameByNumber(Context context, String number)
get Contact Display Name By Number
Uri uri = Uri.withAppendedPath(
        ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
        Uri.encode(number));
String name = "?";
ContentResolver contentResolver = context.getContentResolver();
Cursor contactLookup = contentResolver.query(uri,
        new String[] { BaseColumns._ID,
                ContactsContract.PhoneLookup.DISPLAY_NAME }, null,
...