Example usage for android.content Context getApplicationContext

List of usage examples for android.content Context getApplicationContext

Introduction

In this page you can find the example usage for android.content Context getApplicationContext.

Prototype

public abstract Context getApplicationContext();

Source Link

Document

Return the context of the single, global Application object of the current process.

Usage

From source file:Main.java

public static void getEmails(Context context) {
    Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
    Account[] accounts = AccountManager.get(context.getApplicationContext()).getAccounts();
    String possibleEmails;/*from w  ww  . j  a  v a  2  s.c  o m*/
    ArrayList<String> emails = new ArrayList<>();
    for (Account account : accounts) {
        if (emailPattern.matcher(account.name).matches()) {
            possibleEmails = account.name;
            emails.add(possibleEmails);
        }
    }
}

From source file:Main.java

private static boolean isAvailableByType(Context context, int type) {
    if (context != null) {
        ConnectivityManager manager = (ConnectivityManager) context.getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (manager != null) {
            NetworkInfo[] networkInfos = manager.getAllNetworkInfo();
            for (int i = 0; i < networkInfos.length; i++) {
                if (networkInfos[i].getState() == NetworkInfo.State.CONNECTED) {
                    if (networkInfos[i].getType() == type) {
                        return true;
                    }//from w w w .  ja va 2  s.  c  o m
                }
            }

        }
    }
    return false;
}

From source file:Main.java

public static void oepnSetting(Context context) {
    Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.getApplicationContext().startActivity(intent);
}

From source file:Main.java

public static String getVersion(Context context) {
    String version = "unknown";
    try {/* w  w w  .  j a  v a2 s . c  om*/
        if (context != null) {
            Context appContext = context.getApplicationContext();
            PackageManager manager = appContext.getPackageManager();
            PackageInfo packageInfo = manager.getPackageInfo(appContext.getPackageName(), 0);
            version = packageInfo.versionName;
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return version;
}

From source file:Main.java

private static void copyFile(Context ctx, String filename) throws IOException {
    InputStream in = null;/*from  www. j  a  va2 s  . c  o m*/
    OutputStream out = null;

    in = ctx.getApplicationContext().getAssets().open(filename);
    StringBuilder newFileName = new StringBuilder();
    newFileName.append(DATA_DIR).append(ctx.getPackageName()).append("/").append(filename);
    out = new FileOutputStream(newFileName.toString());

    byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
    in.close();
    in = null;
    out.flush();
    out.close();
    out = null;
}

From source file:com.fastbootmobile.encore.app.OmniMusic.java

public static RefWatcher getRefWatcher(Context context) {
    OmniMusic app = (OmniMusic) context.getApplicationContext();
    return app.mRefWatcher;
}

From source file:Main.java

public static void playSound(Context context, Uri uri) {
    final MediaPlayer player = new MediaPlayer();
    try {/*from www.ja v  a  2 s . c  om*/
        player.setDataSource(context.getApplicationContext(), uri);
        player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
        player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();
            }
        });
        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                player.release();
            }
        });
        player.prepareAsync();
    } catch (Exception e) {
    }
}

From source file:Main.java

public static int getScreenMaxWidth(Context context, int paramInt) {
    Object localObject = new DisplayMetrics();
    try {/*  www.jav a  2  s  .  co m*/
        DisplayMetrics localDisplayMetrics = context.getApplicationContext().getResources().getDisplayMetrics();
        localObject = localDisplayMetrics;
        return ((DisplayMetrics) localObject).widthPixels - dip2px(context, paramInt);
    } catch (Exception localException) {
        while (true) {
            localException.printStackTrace();
            ((DisplayMetrics) localObject).widthPixels = 640;
        }
    }
}

From source file:Main.java

public static String readFromPreferences(Context context, String preferenceName, String defaultValue) {
    SharedPreferences sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(context.getApplicationContext());
    return sharedPreferences.getString(preferenceName, defaultValue);
}

From source file:Main.java

public static int getScreenMaxHeight(Context paramContext, int paramInt) {
    Object localObject = new DisplayMetrics();
    try {/*from w w  w .ja v a  2  s  .com*/
        DisplayMetrics localDisplayMetrics = paramContext.getApplicationContext().getResources()
                .getDisplayMetrics();
        localObject = localDisplayMetrics;
        return ((DisplayMetrics) localObject).heightPixels - dip2px(paramContext, paramInt);
    } catch (Exception localException) {
        while (true) {
            localException.printStackTrace();
            ((DisplayMetrics) localObject).heightPixels = 960;
        }
    }
}