android.content.Context类的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(11.9k)|赞(0)|评价(0)|浏览(341)

本文整理了Java中android.content.Context类的一些代码示例,展示了Context类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Context类的具体详情如下:
包路径:android.content.Context
类名称:Context

Context介绍

[英]http://developer.android.com/reference/android/content/Context.html
[中]http://developer.android.com/reference/android/content/Context.html

代码示例

代码示例来源:origin: nickbutcher/plaid

public static void addToPocket(Context context,
                String url,
                String tweetStatusId) {
  Intent intent = new Intent(Intent.ACTION_SEND);
  intent.setPackage(PACKAGE);
  intent.setType(MIME_TYPE);
  intent.putExtra(Intent.EXTRA_TEXT, url);
  if (tweetStatusId != null && tweetStatusId.length() > 0) {
    intent.putExtra(EXTRA_TWEET_STATUS_ID, tweetStatusId);
  }
  intent.putExtra(EXTRA_SOURCE_PACKAGE, context.getPackageName());
  context.startActivity(intent);
}

代码示例来源:origin: JakeWharton/butterknife

private static String getResourceEntryName(View view, @IdRes int id) {
 if (view.isInEditMode()) {
  return "<unavailable while editing>";
 }
 return view.getContext().getResources().getResourceEntryName(id);
}

代码示例来源:origin: stackoverflow.com

AlertDialog.Builder builder;
AlertDialog alertDialog;

Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater)
    mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,
    (ViewGroup) findViewById(R.id.layout_root));

TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);

builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();

代码示例来源:origin: bumptech/glide

@Nullable
private static PackageInfo getPackageInfo(@NonNull Context context) {
 try {
  return context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
 } catch (PackageManager.NameNotFoundException e) {
  Log.e(TAG, "Cannot resolve info for" + context.getPackageName(), e);
  return null;
 }
}

代码示例来源:origin: stackoverflow.com

public void startNewActivity(Context context, String packageName) {
  Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
  if (intent == null) {
    // Bring user to the market or let them choose an app?
    intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse("market://details?id=" + packageName));
  }
  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  context.startActivity(intent);
}

代码示例来源:origin: square/leakcanary

private void requestWritePermissionNotification() {
 if (permissionNotificationDisplayed) {
  return;
 }
 permissionNotificationDisplayed = true;
 PendingIntent pendingIntent = RequestStoragePermissionActivity.createPendingIntent(context);
 String contentTitle = context.getString(R.string.leak_canary_permission_notification_title);
 CharSequence packageName = context.getPackageName();
 String contentText =
   context.getString(R.string.leak_canary_permission_notification_text, packageName);
 showNotification(context, contentTitle, contentText, pendingIntent, 0xDEAFBEEF);
}

代码示例来源:origin: sunfusheng/StickyHeaderListView

@Override
public View getView(int position, View convertView, ViewGroup parent) {
  final ViewHolder holder;
  if (convertView == null) {
    convertView = mInflater.inflate(R.layout.item_filter_one, null);
    holder = new ViewHolder(convertView);
    convertView.setTag(holder);
  } else {
    holder = (ViewHolder) convertView.getTag();
  }
  FilterEntity entity = getItem(position);
  holder.tvTitle.setText(entity.getKey());
  if (entity.isSelected()) {
    holder.tvTitle.setTextColor(mContext.getResources().getColor(R.color.colorPrimary));
  } else {
    holder.tvTitle.setTextColor(mContext.getResources().getColor(R.color.font_black_3));
  }
  return convertView;
}

代码示例来源:origin: GrenderG/Toasty

boolean withIcon, boolean shouldTint) {
final Toast currentToast = Toast.makeText(context, "", duration);
final View toastLayout = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
    .inflate(R.layout.toast_layout, null);
final ImageView toastIcon = toastLayout.findViewById(R.id.toast_icon);
final TextView toastTextView = toastLayout.findViewById(R.id.toast_text);
Drawable drawableFrame;
  ToastyUtils.setBackground(toastIcon, tintIcon ? ToastyUtils.tintIcon(icon, textColor) : icon);
} else {
  toastIcon.setVisibility(View.GONE);
toastTextView.setText(message);
toastTextView.setTextColor(textColor);
toastTextView.setTypeface(currentTypeface);
toastTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);

代码示例来源:origin: TeamNewPipe/NewPipe

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
             Bundle savedInstanceState) {
  View rootView = inflater.inflate(R.layout.fragment_about, container, false);
  Context context = this.getContext();
  TextView version = rootView.findViewById(R.id.app_version);
  version.setText(BuildConfig.VERSION_NAME);
  View githubLink = rootView.findViewById(R.id.github_link);
  githubLink.setOnClickListener(nv -> openWebsite(context.getString(R.string.github_url), context));
  View donationLink = rootView.findViewById(R.id.donation_link);
  donationLink.setOnClickListener(v -> openWebsite(context.getString(R.string.donation_url), context));
  View websiteLink = rootView.findViewById(R.id.website_link);
  websiteLink.setOnClickListener(nv -> openWebsite(context.getString(R.string.website_url), context));
  View privacyPolicyLink = rootView.findViewById(R.id.privacy_policy_link);
  privacyPolicyLink.setOnClickListener(v -> openWebsite(context.getString(R.string.privacy_policy_url), context));
  return rootView;
}

代码示例来源:origin: ogaclejapan/SmartTabLayout

@Override
public View createTabView(ViewGroup container, int position, PagerAdapter adapter) {
 LayoutInflater inflater = LayoutInflater.from(container.getContext());
 Resources res = container.getContext().getResources();
 View tab = inflater.inflate(R.layout.custom_tab_icon_and_notification_mark, container, false);
 View mark = tab.findViewById(R.id.custom_tab_notification_mark);
 mark.setVisibility(View.GONE);
 ImageView icon = (ImageView) tab.findViewById(R.id.custom_tab_icon);
 switch (position) {
  case 0:
   icon.setImageDrawable(res.getDrawable(R.drawable.ic_home_white_24dp));
   break;
  case 1:
   icon.setImageDrawable(res.getDrawable(R.drawable.ic_search_white_24dp));
   break;
  case 2:
   icon.setImageDrawable(res.getDrawable(R.drawable.ic_person_white_24dp));
   break;
  default:
   throw new IllegalStateException("Invalid position: " + position);
 }
 return tab;
}

代码示例来源:origin: square/leakcanary

if (convertView == null) {
  convertView =
    LayoutInflater.from(context).inflate(R.layout.leak_canary_ref_top_row, parent, false);
 textView.setText(context.getPackageName());
} else {
 if (convertView == null) {
  convertView =
    LayoutInflater.from(context).inflate(R.layout.leak_canary_ref_row, parent, false);
  detailView.setVisibility(View.VISIBLE);
 } else {
  detailView.setVisibility(View.GONE);
 Resources resources = convertView.getResources();
 if (position == 1) {
  titleView.setText(Html.fromHtml("<font color='"

代码示例来源:origin: CarGuo/GSYVideoPlayer

public void initList(List<SwitchVideoModel> data, OnListItemClickListener onItemClickListener) {
  this.onItemClickListener = onItemClickListener;
  this.data = data;
  LayoutInflater inflater = LayoutInflater.from(mContext);
  View view = inflater.inflate(R.layout.switch_video_dialog, null);
  listView = (ListView) view.findViewById(R.id.switch_dialog_list);
  setContentView(view);
  adapter = new ArrayAdapter<>(mContext, R.layout.switch_video_dialog_item, data);
  listView.setAdapter(adapter);
  listView.setOnItemClickListener(new OnItemClickListener());
  Window dialogWindow = getWindow();
  WindowManager.LayoutParams lp = dialogWindow.getAttributes();
  DisplayMetrics d = mContext.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
  lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6
  dialogWindow.setAttributes(lp);
}

代码示例来源:origin: commonsguy/cw-omnibus

RowController(View row) {
 super(row);
 title=row.findViewById(android.R.id.text1);
 thumbnail=row.findViewById(R.id.thumbnail);
 row.setOnClickListener(view -> {
  Intent i=new Intent(Intent.ACTION_VIEW);
  i.setDataAndType(videoUri, videoMimeType);
  title.getContext().startActivity(i);
 });
}

代码示例来源:origin: ArthurHub/Android-Image-Cropper

Intent intent = context instanceof Activity ? ((Activity) context).getIntent() : null;
if (intent != null) {
 Bundle bundle = intent.getBundleExtra(CropImage.CROP_IMAGE_EXTRA_BUNDLE);
 if (bundle != null) {
  options = bundle.getParcelable(CropImage.CROP_IMAGE_EXTRA_OPTIONS);
  TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CropImageView, 0, 0);
  try {
   options.fixAspectRatio =
mFlipVertically = options.flipVertically;
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.crop_image_view, this, true);
mImageView = v.findViewById(R.id.ImageView_image);
mImageView.setScaleType(ImageView.ScaleType.MATRIX);
mCropOverlayView = v.findViewById(R.id.CropOverlayView);
mCropOverlayView.setCropWindowChangeListener(
  new CropOverlayView.CropWindowChangeListener() {
mCropOverlayView.setInitialAttributeValues(options);
mProgressBar = v.findViewById(R.id.CropProgressBar);
setProgressBarVisibility();

代码示例来源:origin: binIoter/GuideView

@Override public View getView(LayoutInflater inflater) {
 LinearLayout ll = new LinearLayout(inflater.getContext());
 LinearLayout.LayoutParams param =
   new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
     LinearLayout.LayoutParams.WRAP_CONTENT);
 ll.setOrientation(LinearLayout.VERTICAL);
 ll.setLayoutParams(param);
 TextView textView = new TextView(inflater.getContext());
 textView.setText(R.string.nearby);
 textView.setTextColor(inflater.getContext().getResources().getColor(R.color.color_white));
 textView.setTextSize(20);
 ImageView imageView = new ImageView(inflater.getContext());
 imageView.setImageResource(R.mipmap.arrow);
 ll.removeAllViews();
 ll.addView(textView);
 ll.addView(imageView);
 ll.setOnClickListener(new View.OnClickListener() {
  @Override public void onClick(View view) {
   Toast.makeText(view.getContext(), "引导层被点击了", Toast.LENGTH_SHORT).show();
  }
 });
 return ll;
}

代码示例来源:origin: commonsguy/cw-omnibus

@Override
public RemoteViews getViewAt(int position) {
 RemoteViews row=
   new RemoteViews(ctxt.getPackageName(), R.layout.row);
 row.setTextViewText(android.R.id.text1, items[position]);
 Intent i=new Intent();
 Bundle extras=new Bundle();
 extras.putString(WidgetProvider.EXTRA_WORD, items[position]);
 extras.putInt(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
 i.putExtras(extras);
 row.setOnClickFillInIntent(android.R.id.text1, i);
 return(row);
}

代码示例来源:origin: google/ExoPlayer

private static PendingIntent createBroadcastIntent(
  String action, Context context, int instanceId) {
 Intent intent = new Intent(action).setPackage(context.getPackageName());
 intent.putExtra(EXTRA_INSTANCE_ID, instanceId);
 return PendingIntent.getBroadcast(
   context, instanceId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
}

代码示例来源:origin: nickbutcher/plaid

public static void addKeepAliveExtra(Context context, Intent intent) {
  Intent keepAliveIntent = new Intent().setClassName(
      context.getPackageName(), KeepAliveService.class.getCanonicalName());
  intent.putExtra(EXTRA_CUSTOM_TABS_KEEP_ALIVE, keepAliveIntent);
}

代码示例来源:origin: seven332/EhViewer

@SuppressWarnings("deprecation")
public void init(Context context) {
  setOrientation(VERTICAL);
  setDividerDrawable(context.getResources().getDrawable(R.drawable.spacer_keyline));
  setShowDividers(SHOW_DIVIDER_MIDDLE);
  LayoutInflater.from(context).inflate(R.layout.widget_image_search, this);
  mPreview = (ImageView) ViewUtils.$$(this, R.id.preview);
  mSelectImage = ViewUtils.$$(this, R.id.select_image);
  mSearchUSS = (CheckBox) ViewUtils.$$(this, R.id.search_uss);
  mSearchOSC = (CheckBox) ViewUtils.$$(this, R.id.search_osc);
  mSearchSE = (CheckBox) ViewUtils.$$(this, R.id.search_se);
  mSelectImage.setOnClickListener(this);
}

代码示例来源:origin: novoda/android-demos

public View getView(int position, View convertView, ViewGroup parent) {
    View view;
    if (convertView == null) {
      final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
          Context.LAYOUT_INFLATER_SERVICE);
      view = inflater.inflate(R.layout.carousel_gallery_li, null);
    } else {
      view = convertView;
    }

    final ImageView imageView = (ImageView) view.findViewById(R.id.image);

    Bitmap image = null;
    try {
      InputStream bitmap = mContext.getAssets().open(PLACEHOLDER_FILE);
      image = BitmapFactory.decodeStream(bitmap);
    } catch (IOException exception) {
      Log.e(TAG, "An error occurred when you have tried to open the file: "+ PLACEHOLDER_FILE, exception);
    }

    imageView.setImageBitmap(image);
    return view;
  }
}

相关文章

微信公众号

最新文章

更多

Context类方法