下面列出了android.content.Context#getText ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Return a concise, localized title for the given
* {@link ApplicationInfo#category} value, or {@code null} for unknown
* values such as {@link #CATEGORY_UNDEFINED}.
*
* @see #category
*/
public static CharSequence getCategoryTitle(Context context, @Category int category) {
switch (category) {
case ApplicationInfo.CATEGORY_GAME:
return context.getText(com.android.internal.R.string.app_category_game);
case ApplicationInfo.CATEGORY_AUDIO:
return context.getText(com.android.internal.R.string.app_category_audio);
case ApplicationInfo.CATEGORY_VIDEO:
return context.getText(com.android.internal.R.string.app_category_video);
case ApplicationInfo.CATEGORY_IMAGE:
return context.getText(com.android.internal.R.string.app_category_image);
case ApplicationInfo.CATEGORY_SOCIAL:
return context.getText(com.android.internal.R.string.app_category_social);
case ApplicationInfo.CATEGORY_NEWS:
return context.getText(com.android.internal.R.string.app_category_news);
case ApplicationInfo.CATEGORY_MAPS:
return context.getText(com.android.internal.R.string.app_category_maps);
case ApplicationInfo.CATEGORY_PRODUCTIVITY:
return context.getText(com.android.internal.R.string.app_category_productivity);
default:
return null;
}
}
/**
* Return a concise, localized title for the given
* {@link ApplicationInfo#category} value, or {@code null} for unknown
* values such as {@link #CATEGORY_UNDEFINED}.
*
* @see #category
*/
public static CharSequence getCategoryTitle(Context context, @Category int category) {
switch (category) {
case ApplicationInfo.CATEGORY_GAME:
return context.getText(com.android.internal.R.string.app_category_game);
case ApplicationInfo.CATEGORY_AUDIO:
return context.getText(com.android.internal.R.string.app_category_audio);
case ApplicationInfo.CATEGORY_VIDEO:
return context.getText(com.android.internal.R.string.app_category_video);
case ApplicationInfo.CATEGORY_IMAGE:
return context.getText(com.android.internal.R.string.app_category_image);
case ApplicationInfo.CATEGORY_SOCIAL:
return context.getText(com.android.internal.R.string.app_category_social);
case ApplicationInfo.CATEGORY_NEWS:
return context.getText(com.android.internal.R.string.app_category_news);
case ApplicationInfo.CATEGORY_MAPS:
return context.getText(com.android.internal.R.string.app_category_maps);
case ApplicationInfo.CATEGORY_PRODUCTIVITY:
return context.getText(com.android.internal.R.string.app_category_productivity);
default:
return null;
}
}
/**
* Return a concise, localized title for the given
* {@link ApplicationInfo#category} value, or {@code null} for unknown
* values such as {@link #CATEGORY_UNDEFINED}.
*
* @see #category
*/
public static CharSequence getCategoryTitle(Context context, @Category int category) {
switch (category) {
case ApplicationInfo.CATEGORY_GAME:
return context.getText(com.android.internal.R.string.app_category_game);
case ApplicationInfo.CATEGORY_AUDIO:
return context.getText(com.android.internal.R.string.app_category_audio);
case ApplicationInfo.CATEGORY_VIDEO:
return context.getText(com.android.internal.R.string.app_category_video);
case ApplicationInfo.CATEGORY_IMAGE:
return context.getText(com.android.internal.R.string.app_category_image);
case ApplicationInfo.CATEGORY_SOCIAL:
return context.getText(com.android.internal.R.string.app_category_social);
case ApplicationInfo.CATEGORY_NEWS:
return context.getText(com.android.internal.R.string.app_category_news);
case ApplicationInfo.CATEGORY_MAPS:
return context.getText(com.android.internal.R.string.app_category_maps);
case ApplicationInfo.CATEGORY_PRODUCTIVITY:
return context.getText(com.android.internal.R.string.app_category_productivity);
default:
return null;
}
}
private static String getData(Context context, AttributeSet set, int attribute)
throws ReflectiveOperationException {
final TypedArray sa = context.obtainStyledAttributes(set, new int[] {attribute});
try {
final TypedValue tv = sa.peekValue(0);
CharSequence data = null;
if (tv != null && tv.type == TypedValue.TYPE_STRING) {
if (tv.resourceId != 0) {
data = context.getText(tv.resourceId);
} else {
data = tv.string;
}
}
return (data != null) ? data.toString() : null;
} finally {
sa.recycle();
}
}
static void about(Context context) {
final SpannableString s = new SpannableString(
context.getText(R.string.aboutText));
Linkify.addLinks(s, Linkify.ALL);
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle(context.getText(R.string.aboutTitle));
alertDialog.setMessage(s);
alertDialog.setButton(DialogInterface.BUTTON_NEUTRAL,
context.getText(R.string.aboutCloseButton),
new Dialog.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog,
final int which) {
try {
finalize();
} catch (final Throwable e) {
e.printStackTrace();
}
}
});
alertDialog.show();
((TextView) alertDialog.findViewById(android.R.id.message))
.setMovementMethod(mMovementCheck);
}
AsyncProvider(Context context, Listener<ReturnType> listener, boolean showProgressDialog) {
this.message = context.getText(R.string.dialog_progress_loading);
this.listener = listener;
if (showProgressDialog) {
this.progress = new ProgressDialog(context);
} else {
progress = null;
}
}
private void init(Context context, @Nullable AttributeSet attributeSet) {
this.context = context;
setOrientation(LinearLayout.HORIZONTAL);
final TypedArray styleAttributesArray = context.obtainStyledAttributes(attributeSet, R.styleable.BaldSwitch);
size = styleAttributesArray.getDimension(R.styleable.BaldSwitch_size, 0);
yes = styleAttributesArray.getString(R.styleable.BaldSwitch_yes);
if (yes == null)
yes = context.getText(R.string.yes);
no = styleAttributesArray.getString(R.styleable.BaldSwitch_no);
if (no == null)
no = context.getText(R.string.no);
//Dont change, trust me
enabled = (styleAttributesArray.getBoolean(R.styleable.BaldSwitch_enabled, true));
setEnabled(enabled);
//Dont change, trust me
checked = styleAttributesArray.getBoolean(R.styleable.BaldSwitch_checked, false);
styleAttributesArray.recycle();
try {
final TypedValue typedValue = new TypedValue();
Resources.Theme theme = context.getTheme();
theme.resolveAttribute(R.attr.bald_text_on_selected, typedValue, true);
textColorOnSelected = typedValue.data;
theme.resolveAttribute(R.attr.bald_text_on_button, typedValue, true);
textColorOnButton = typedValue.data;
} catch (Exception e) {
Log.e(TAG, e.getMessage());
e.printStackTrace();
textColorOnSelected = Color.BLACK;
textColorOnButton = Color.BLACK;
}
updateView();
}
public static Spanned getCommitMessage(Context context) {
String stringCommit = getCommitHash(context);
if (stringCommit.contains(context.getString(R.string.unavailable))) {
stringCommit = String.format(context.getString(R.string.last_commit), stringCommit);
stringCommit = stringCommit + " " + context.getText(R.string.lastcommit_unavailable);
} else {
stringCommit = String.format(context.getString(R.string.last_commit), stringCommit);
}
return Html.fromHtml(stringCommit);
}
@TargetApi(Build.VERSION_CODES.FROYO)
public WorkTimeTrackerBackupManager(final Context context) {
prefKeyBackupEnabled = context.getText(R.string.keyBackupEnabled) + "";
backupManager = new BackupManager(context);
timestampPrefs = context.getSharedPreferences("timestampPrefs", Context.MODE_PRIVATE);
defaultPrefs = PreferenceManager.getDefaultSharedPreferences(context);
enabled = defaultPrefs.getBoolean(prefKeyBackupEnabled, true);
}
protected CardViewModel(final Context context, final @StringRes int title, final @StringRes int text,
final @StringRes int button_start, final @StringRes int button_end) {
this.title = title == 0 ? null : context.getText(title);
this.text = text == 0 ? null : context.getText(text);
this.button_start = button_start == 0 ? null : context.getText(button_start);
this.button_end = button_end == 0 ? null : context.getText(button_end);
color = ContextCompat.getColor(context, R.color.card_attention);
}
public static Spanned getCommitMessage(Context context) {
String stringCommit = getCommitHash(context);
if (stringCommit.contains(context.getString(R.string.unavailable))) {
stringCommit = String.format(context.getString(R.string.last_commit), stringCommit);
stringCommit = stringCommit + " " + context.getText(R.string.lastcommit_unavailable);
} else {
stringCommit = String.format(context.getString(R.string.last_commit), stringCommit);
}
return Html.fromHtml(stringCommit);
}
private void attachTitle(@NonNull final Content content) {
CharSequence title;
Context context = tvTitle.getContext();
if (content.getTitle() == null) {
title = context.getText(R.string.work_untitled);
} else {
title = content.getTitle();
}
tvTitle.setText(title);
tvTitle.setTextColor(ThemeHelper.getColor(tvTitle.getContext(), R.color.card_title_light));
}
/**
* Resolve the notification string, and return it.
*
* @param context A Context to resolve the string
* @return The notification string.
*/
@NonNull
public CharSequence getNotificationString(@NonNull final Context context) {
@StringRes
final int notificationStringRes;
if (isUnsupported) {
notificationStringRes = R.string.app_version_unsupported;
} else if (lastSupportedDate == null) {
notificationStringRes = R.string.app_version_outdated;
} else {
// Deadline date is available, but won't be displayed for now.
notificationStringRes = R.string.app_version_deprecated;
}
return context.getText(notificationStringRes);
}
private void updateNotifications(VolumeInfo vol, int oldLevel, int newLevel) {
final Context context = getContext();
final UUID uuid = StorageManager.convert(vol.getFsUuid());
if (State.isEntering(State.LEVEL_LOW, oldLevel, newLevel)) {
Intent lowMemIntent = new Intent(StorageManager.ACTION_MANAGE_STORAGE);
lowMemIntent.putExtra(StorageManager.EXTRA_UUID, uuid);
lowMemIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
final CharSequence title = context.getText(
com.android.internal.R.string.low_internal_storage_view_title);
final CharSequence details;
if (StorageManager.UUID_DEFAULT.equals(uuid)) {
details = context.getText(isBootImageOnDisk()
? com.android.internal.R.string.low_internal_storage_view_text
: com.android.internal.R.string.low_internal_storage_view_text_no_boot);
} else {
details = context.getText(
com.android.internal.R.string.low_internal_storage_view_text);
}
PendingIntent intent = PendingIntent.getActivityAsUser(context, 0, lowMemIntent, 0,
null, UserHandle.CURRENT);
Notification notification =
new Notification.Builder(context, SystemNotificationChannels.ALERTS)
.setSmallIcon(com.android.internal.R.drawable.stat_notify_disk_full)
.setTicker(title)
.setColor(context.getColor(
com.android.internal.R.color.system_notification_accent_color))
.setContentTitle(title)
.setContentText(details)
.setContentIntent(intent)
.setStyle(new Notification.BigTextStyle()
.bigText(details))
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setCategory(Notification.CATEGORY_SYSTEM)
.extend(new Notification.TvExtender()
.setChannelId(TV_NOTIFICATION_CHANNEL_ID))
.build();
notification.flags |= Notification.FLAG_NO_CLEAR;
mNotifManager.notifyAsUser(uuid.toString(), SystemMessage.NOTE_LOW_STORAGE,
notification, UserHandle.ALL);
} else if (State.isLeaving(State.LEVEL_LOW, oldLevel, newLevel)) {
mNotifManager.cancelAsUser(uuid.toString(), SystemMessage.NOTE_LOW_STORAGE,
UserHandle.ALL);
}
}
@Override
public void handleMessage(Message msg) {
final Context context = mService.mContext;
final NotificationManager notifManager = context
.getSystemService(NotificationManager.class);
final int max = msg.arg1;
final int index = msg.arg2;
switch (msg.what) {
case MSG_SHOW:
final CharSequence title = context
.getText(R.string.android_upgrading_notification_title);
final Intent intent = new Intent();
intent.setClassName("com.android.settings",
"com.android.settings.HelpTrampoline");
intent.putExtra(Intent.EXTRA_TEXT, "help_url_upgrading");
final PendingIntent contentIntent;
if (context.getPackageManager().resolveActivity(intent, 0) != null) {
contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
} else {
contentIntent = null;
}
final Notification notif =
new Notification.Builder(mService.mContext,
SystemNotificationChannels.UPDATES)
.setSmallIcon(R.drawable.stat_sys_adb)
.setWhen(0)
.setOngoing(true)
.setTicker(title)
.setColor(context.getColor(
com.android.internal.R.color.system_notification_accent_color))
.setContentTitle(title)
.setContentIntent(contentIntent)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setProgress(max, index, false)
.build();
notifManager.notifyAsUser(TAG, SystemMessage.NOTE_SYSTEM_UPGRADING, notif,
UserHandle.of(mUserId));
break;
case MSG_HIDE:
notifManager.cancelAsUser(TAG, SystemMessage.NOTE_SYSTEM_UPGRADING,
UserHandle.of(mUserId));
break;
}
}
private static void updateWidget(Context context, AppWidgetManager manager, int appWidgetId, boolean serviceRunning) {
RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_recording);
// change the subtext depending on whether the service is running or not
CharSequence subtext = context.getText(
serviceRunning ? R.string.widget_recording_in_progress : R.string.widget_start_recording);
updateViews.setTextViewText(R.id.widget_subtext, subtext);
// if service not running, don't show the "recording" icon
updateViews.setViewVisibility(R.id.record_badge_image_view, serviceRunning ? View.VISIBLE : View.INVISIBLE);
PendingIntent pendingIntent = getPendingIntent(context, appWidgetId);
updateViews.setOnClickPendingIntent(R.id.clickable_linear_layout, pendingIntent);
manager.updateAppWidget(appWidgetId, updateViews);
}
AlertParams(@NonNull Context context, @NonNull Bundle bundle) {
mTheme = bundle.getInt(ARG_THEME);
mTitleText = bundle.getCharSequence(ARG_TITLE_TEXT);
if (mTitleText == null) {
int titleResId = bundle.getInt(ARG_TITLE_TEXT_ID, 0);
if (titleResId != 0) {
mTitleText = context.getText(titleResId);
}
}
mMessageText = bundle.getCharSequence(ARG_MESSAGE_TEXT);
if (mMessageText == null) {
int messageResId = bundle.getInt(ARG_MESSAGE_TEXT_ID, 0);
if (messageResId != 0) {
mMessageText = context.getText(messageResId);
}
}
mMessageTextAlignment = bundle.getInt(ARG_MESSAGE_TEXT_ALIGNMENT, View.TEXT_ALIGNMENT_TEXT_START);
mPositiveText = bundle.getCharSequence(ARG_POSITIVE_TEXT);
if (mPositiveText == null) {
int positiveResId = bundle.getInt(ARG_POSITIVE_TEXT_ID, 0);
if (positiveResId != 0) {
mPositiveText = context.getText(positiveResId);
}
}
mNegativeText = bundle.getCharSequence(ARG_NEGATIVE_TEXT);
if (mNegativeText == null) {
int negativeResId = bundle.getInt(ARG_NEGATIVE_TEXT_ID, 0);
if (negativeResId != 0) {
mNegativeText = context.getText(negativeResId);
}
}
mViewLayoutResId = bundle.getInt(ARG_VIEW_LAYOUT_ID, 0);
int viewWidth = bundle.getInt(ARG_VIEW_LAYOUT_WIDTH, 0);
int viewHeight = bundle.getInt(ARG_VIEW_LAYOUT_HEIGHT, 0);
if (viewWidth != 0 && viewHeight != 0) {
mViewParams = new FrameLayout.LayoutParams(viewWidth, viewHeight);
int gravity = bundle.getInt(ARG_VIEW_LAYOUT_GRAVITY, 0);
if (gravity != 0) {
mViewParams.gravity = gravity;
}
}
}
private static void updateWidget(Context context, AppWidgetManager manager, int appWidgetId, boolean serviceRunning) {
RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_recording);
// change the subtext depending on whether the service is running or not
CharSequence subtext = context.getText(
serviceRunning ? R.string.widget_recording_in_progress : R.string.widget_start_recording);
updateViews.setTextViewText(R.id.widget_subtext, subtext);
// if service not running, don't show the "recording" icon
updateViews.setViewVisibility(R.id.record_badge_image_view, serviceRunning ? View.VISIBLE : View.INVISIBLE);
PendingIntent pendingIntent = getPendingIntent(context, appWidgetId);
updateViews.setOnClickPendingIntent(R.id.clickable_linear_layout, pendingIntent);
manager.updateAppWidget(appWidgetId, updateViews);
}
private static void updateWidget(Context context, AppWidgetManager manager, int appWidgetId, boolean serviceRunning) {
RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_recording);
// change the subtext depending on whether the service is running or not
CharSequence subtext = context.getText(
serviceRunning ? R.string.widget_recording_in_progress : R.string.widget_start_recording);
updateViews.setTextViewText(R.id.widget_subtext, subtext);
// if service not running, don't show the "recording" icon
updateViews.setViewVisibility(R.id.record_badge_image_view, serviceRunning ? View.VISIBLE : View.INVISIBLE);
PendingIntent pendingIntent = getPendingIntent(context, appWidgetId);
updateViews.setOnClickPendingIntent(R.id.clickable_linear_layout, pendingIntent);
manager.updateAppWidget(appWidgetId, updateViews);
}
/**
* Sets the error message, which should be shown, if the validation fails.
*
* @param context
* The context, which should be used to retrieve the error message, as an instance of
* the class {@link Context}. The context may not be null
* @param resourceId
* The resource ID of the string resource, which contains the error message, which
* should be set, as an {@link Integer} value. The resource ID must correspond to a
* valid string resource
*/
public final void setErrorMessage(@NonNull final Context context,
@StringRes final int resourceId) {
Condition.INSTANCE.ensureNotNull(context, "The context may not be null");
this.errorMessage = context.getText(resourceId);
}