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:ru.gkpromtech.exhibition.db.DbHelper.java

public static DbHelper getInstance(Context context) {
    if (mInstance == null)
        mInstance = new DbHelper(context.getApplicationContext());
    return mInstance;
}

From source file:com.example.mediastock.util.Utilities.java

/**
 * Method to delete all the files within the directory typeDir, where type can be music, image or video
 *
 * @param type    the directory type: music, video or image
 * @param context the context//  w  w  w.j av  a 2  s .co m
 */
public static void deleteAllMediaFromInternalStorage(String type, Context context) {
    ContextWrapper cw = new ContextWrapper(context.getApplicationContext());
    File dir = cw.getDir(type + "Dir", Context.MODE_PRIVATE);

    final File[] fileNames = dir.listFiles();

    if (fileNames.length > 0) {
        for (File file : fileNames)
            file.delete();
    }
}

From source file:Main.java

public static void ShowNetworkSettings(final Context ctx) {
    new AlertDialog.Builder(ctx).setTitle("Nessuna connessione ad internet")
            .setMessage("Vuoi aprire le impostazioni di rete?")
            .setPositiveButton("SI", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // continue
                    ctx.getApplicationContext()
                            .startActivity(new Intent(android.provider.Settings.ACTION_DATA_ROAMING_SETTINGS));
                }/*from   w  w w  .  j a  v  a2  s  .  c om*/
            }).setNegativeButton("NO", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // do nothing
                }
            }).show();
}

From source file:com.example.mediastock.util.Utilities.java

/**
 * Checks if the device is connected to the Internet
 *
 * @return true if connected, false otherwise
 *///  w w w. j av  a 2s.  c  om
public static boolean deviceOnline(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnectedOrConnecting();
}

From source file:ir.keloud.android.lib.common.accounts.AccountUtils.java

public static void saveClient(KeloudClient client, Account savedAccount, Context context) {

    // Account Manager
    AccountManager ac = AccountManager.get(context.getApplicationContext());

    if (client != null) {
        String cookiesString = client.getCookiesString();
        if (cookiesString != "") {
            ac.setUserData(savedAccount, Constants.KEY_COOKIES, cookiesString);
            // Log_OC.d(TAG, "Saving Cookies: "+ cookiesString );
        }//from   ww  w  .j a va2s.c o m
    }

}

From source file:com.owncloud.android.lib.common.accounts.AccountUtils.java

public static void saveClient(OwnCloudClient client, Account savedAccount, Context context) {

    // Account Manager
    AccountManager ac = AccountManager.get(context.getApplicationContext());

    if (client != null) {
        String cookiesString = client.getCookiesString();
        if (cookiesString != "") {
            ac.setUserData(savedAccount, Constants.KEY_COOKIES, cookiesString);
            // Log_OC.d(TAG, "Saving Cookies: "+ cookiesString );
        }/*from  w  ww.jav  a 2  s .c om*/
    }

}

From source file:com.example.mediastock.util.Utilities.java

/**
 * Method to delete a media file within the typeDir directory. The directory is stored into the internal storage.
 *
 * @param type    the directory type: music, video or image
 * @param context the context/*from  w  w  w.ja  va  2 s .  co m*/
 * @param path    the path of the media file
 * @return true, if the file was deleted, false otherwise
 */
public static boolean deleteSpecificMediaFromInternalStorage(String type, Context context, String path) {
    ContextWrapper cw = new ContextWrapper(context.getApplicationContext());
    File dir = cw.getDir(type + "Dir", Context.MODE_PRIVATE);

    final File[] fileNames = dir.listFiles();

    boolean result = false;
    for (File file : fileNames) {
        if (file.getName().equals(path))
            result = file.delete();
    }

    return result;
}

From source file:com.example.mediastock.util.Utilities.java

/**
 * It loads an image from the storage//from w w  w  . j av a 2  s . c o  m
 * @param context the context
 * @param path    the path of the image
 * @return the file containing the blob image
 */
public static File loadImageFromInternalStorage(Context context, String path) {
    ContextWrapper cw = new ContextWrapper(context.getApplicationContext());
    File imageDir = cw.getDir("imageDir", Context.MODE_PRIVATE);

    final File[] fileNames = imageDir.listFiles();

    File target = null;
    for (File file : fileNames) {
        if (file.getName().equals(path))
            target = file;
    }

    return target;
}

From source file:Main.java

/**
 * Create a backup of the database./*ww w . j a v a 2  s . c  o  m*/
 *
 * @param context who calls the function.
 * @param dbName  Name of the database to backup. (If empty, radiomap.db is used).
 */
public static void exportDb(Context context, String dbName) {
    try {
        if (dbName.equals(""))
            dbName = "radiomap.db";

        File sd = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "//data//" + context.getApplicationContext().getPackageName()
                    + "//databases//" + dbName;
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sd, dbName);

            FileChannel src = new FileInputStream(currentDB).getChannel();
            FileChannel dst = new FileOutputStream(backupDB).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();

            Toast.makeText(context, "Datenbank nach Downloads exportiert!", Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Log.e("Utils", e.getMessage());
        Toast.makeText(context, "Exportieren fehlgeschlagen!", Toast.LENGTH_LONG).show();
    }
}

From source file:Main.java

/**
 * Create a backup of the database./*from  ww  w  .  j a  v a2 s.c om*/
 *
 * @param context who calls the function.
 * @param dbName  Name of the database to backup. (If empty, radiomap.db is used).
 */
public static void importDb(Context context, String dbName) {
    try {
        if (dbName.equals(""))
            dbName = "radiomap.db";

        File sd = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "//data//" + context.getApplicationContext().getPackageName()
                    + "//databases//" + dbName;
            File backupDB = new File(data, currentDBPath);
            File currentDB = new File(sd, dbName);

            FileChannel src = new FileInputStream(currentDB).getChannel();
            FileChannel dst = new FileOutputStream(backupDB).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();

            Toast.makeText(context, "Datenbank von Downloads importiert!", Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Log.e("Utils", e.getMessage());
        Toast.makeText(context, "Import fehlgeschlagen!", Toast.LENGTH_LONG).show();
    }
}