Example usage for android.content Context getString

List of usage examples for android.content Context getString

Introduction

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

Prototype

@NonNull
public final String getString(@StringRes int resId) 

Source Link

Document

Returns a localized string from the application's package's default string table.

Usage

From source file:com.bw.hawksword.wiktionary.SimpleWikiHelper.java

/**
 * Prepare the internal User-Agent string for use. This requires a
 * {@link Context} to pull the package name and version number for this
 * application.//ww  w .ja  v a2 s. com
 */
public static void prepareUserAgent(Context context) {
    try {
        // Read package name and version number from manifest
        PackageManager manager = context.getPackageManager();
        PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
        sUserAgent = String.format(context.getString(R.string.template_user_agent), info.packageName,
                info.versionName);

    } catch (NameNotFoundException e) {
        Log.e(TAG, "Couldn't find package information in PackageManager", e);
    }
}

From source file:com.bt.download.android.gui.util.UIUtils.java

public static void showLongMessage(Context context, int resId) {
    showLongMessage(context, context.getString(resId));
}

From source file:Main.java

/**
 * @param context//  w  w w.j ava 2  s  .c  o m
 *            if null, use the default format (Mozilla/5.0 (Linux; U;
 *            Android %s) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0
 *            %sSafari/534.30).
 * @return
 */

public static String getUserAgent(Context context) {
    String webUserAgent = null;
    if (context != null) {
        try {
            Class<?> sysResCls = Class.forName("com.android.internal.R$string");
            Field webUserAgentField = sysResCls.getDeclaredField("web_user_agent");
            Integer resId = (Integer) webUserAgentField.get(null);
            webUserAgent = context.getString(resId);
        } catch (Throwable ignored) {
        }
    }
    if (TextUtils.isEmpty(webUserAgent)) {
        webUserAgent = "Mozilla/5.0 (Linux; U; Android %s) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 %sSafari/533.1";
    }

    Locale locale = Locale.getDefault();
    StringBuffer buffer = new StringBuffer();
    // Add version
    final String version = Build.VERSION.RELEASE;
    if (version.length() > 0) {
        buffer.append(version);
    } else {
        // default to "1.0"
        buffer.append("1.0");
    }
    buffer.append("; ");
    final String language = locale.getLanguage();
    if (language != null) {
        buffer.append(language.toLowerCase());
        final String country = locale.getCountry();
        if (country != null) {
            buffer.append("-");
            buffer.append(country.toLowerCase());
        }
    } else {
        // default to "en"
        buffer.append("en");
    }
    // add the model for the release build
    if ("REL".equals(Build.VERSION.CODENAME)) {
        final String model = Build.MODEL;
        if (model.length() > 0) {
            buffer.append("; ");
            buffer.append(model);
        }
    }
    final String id = Build.ID;
    if (id.length() > 0) {
        buffer.append(" Build/");
        buffer.append(id);
    }
    return String.format(webUserAgent, buffer, "Mobile ");
}

From source file:com.bt.download.android.gui.util.UIUtils.java

public static void showShortMessage(Context context, int resId) {
    showShortMessage(context, context.getString(resId));
}

From source file:net.frygo.findmybuddy.GCMIntentService.java

private static void generateNotification(Context context, String message) {

    Random rand = new Random();
    int x = rand.nextInt();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    String title = context.getString(R.string.app_name);
    Intent notificationIntent = new Intent(context, customlistview.class);
    notificationIntent.putExtra("alert", message);
    message = message + " would like to add you as friend";
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_CANCEL_CURRENT);

    Notification notification = new Notification(R.drawable.logo, message, System.currentTimeMillis());
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(x, notification);

    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    final PowerManager.WakeLock mWakelock = pm
            .newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, title);
    mWakelock.acquire();//from w  w w .  j  a  va2 s .c o  m

    // Timer before putting Android Device to sleep mode.
    Timer timer = new Timer();
    TimerTask task = new TimerTask() {
        public void run() {
            mWakelock.release();
        }
    };
    timer.schedule(task, 5000);

}

From source file:com.example.igorklimov.popularmoviesdemo.helpers.Utility.java

public static void setSortByPreference(Context c, int value) {
    PreferenceManager.getDefaultSharedPreferences(c).edit().putInt(c.getString(R.string.key_sort_types), value)
            .apply();//w ww .  ja  v  a  2 s  .co m
}

From source file:com.balk.rsswidget.GoogleReaderHelper.java

/**
 * Prepare the internal User-Agent string for use. This requires a
 * {@link Context} to pull the package name and version number for this
 * application./*from ww  w  .j av  a2 s.  c o m*/
 */
public static void prepareUserAgent(Context context) {
    try {
        // Read package name and version number from manifest
        PackageManager manager = context.getPackageManager();
        PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
        sUserAgent = String.format(context.getString(R.string.template_wotd_title), info.packageName,
                info.versionName);

    } catch (NameNotFoundException e) {
        Log.e(TAG, "Couldn't find package information in PackageManager", e);
    }
}

From source file:com.example.igorklimov.popularmoviesdemo.helpers.Utility.java

public static void setIsTabletPreference(Context c, boolean b) {
    PreferenceManager.getDefaultSharedPreferences(c).edit().putBoolean(c.getString(R.string.is_two_pane), b)
            .apply();//w w w . ja va 2s  .  c om
}

From source file:Main.java

/**
 * @param context if null, use the default format
 *                (Mozilla/5.0 (Linux; U; Android %s) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 %sSafari/534.30).
 * @return//from   w  ww . java2 s. c  om
 */
public static String getUserAgent(Context context) {
    String webUserAgent = null;
    if (context != null) {
        try {
            Class sysResCls = Class.forName("com.android.internal.R$string");
            Field webUserAgentField = sysResCls.getDeclaredField("web_user_agent");
            Integer resId = (Integer) webUserAgentField.get(null);
            webUserAgent = context.getString(resId);
        } catch (Throwable ignored) {
        }
    }
    if (TextUtils.isEmpty(webUserAgent)) {
        webUserAgent = "Mozilla/5.0 (Linux; U; Android %s) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 %sSafari/533.1";
    }

    Locale locale = Locale.getDefault();
    StringBuffer buffer = new StringBuffer();
    // Add version
    final String version = Build.VERSION.RELEASE;
    if (version.length() > 0) {
        buffer.append(version);
    } else {
        // default to "1.0"
        buffer.append("1.0");
    }
    buffer.append("; ");
    final String language = locale.getLanguage();
    if (language != null) {
        buffer.append(language.toLowerCase());
        final String country = locale.getCountry();
        if (country != null) {
            buffer.append("-");
            buffer.append(country.toLowerCase());
        }
    } else {
        // default to "en"
        buffer.append("en");
    }
    // add the model for the release build
    if ("REL".equals(Build.VERSION.CODENAME)) {
        final String model = Build.MODEL;
        if (model.length() > 0) {
            buffer.append("; ");
            buffer.append(model);
        }
    }
    final String id = Build.ID;
    if (id.length() > 0) {
        buffer.append(" Build/");
        buffer.append(id);
    }
    return String.format(webUserAgent, buffer, "Mobile ");
}

From source file:net.giovannicapuano.visualnovel.Memory.java

public static boolean putLastScript(Context context, String script) {
    try {//from  w w  w  .  j av a2  s  .c o m
        FileOutputStream fos = context.openFileOutput(SCRIPT, Context.MODE_PRIVATE);
        fos.write(script.getBytes());
        fos.flush();
        fos.close();

        return true;
    } catch (IOException e) {
        Utils.error(e);
        Utils.error(context, context.getString(R.string.error_saving_game));
        return false;
    }
}