下面列出了android.content.Context#getClass ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void clickHelper(Context context,EditText mEdit, View v) {
src = mEdit.getText().toString();
while (src.length() != 30) {
src += " ";
}
mEdit.setText(src);
System.out.println(context.getClass());
if(context.getClass()==android.app.Application.class)
{generate(context);
fab_trans.show();}
else
return;
}
public void write(String JSONcontent,Message message,Context context,File myInternalFile){
if(isFilePresent(context, "storage.json")) {
try {
FileOutputStream fOut = new FileOutputStream(myInternalFile);
fOut.write(JSONcontent.getBytes());
fOut.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
//proceed with storing the first todo or show ui
} else {
create(context, "storage.json", "{}");
if(context.getClass()==android.app.Application.class)
printmessage(message);
}
}
private void fixBaseContextImplOpsPackage(Context context) throws IllegalAccessException {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1 && context != null && !TextUtils.equals(context.getPackageName(), mHostContext.getPackageName())) {
Context baseContext = context;
Class clazz = baseContext.getClass();
Field mOpPackageName = FieldUtils.getDeclaredField(clazz, "mOpPackageName", true);
if (mOpPackageName != null) {
Object valueObj = mOpPackageName.get(baseContext);
if (valueObj instanceof String) {
String opPackageName = ((String) valueObj);
if (!TextUtils.equals(opPackageName, mHostContext.getPackageName())) {
mOpPackageName.set(baseContext, mHostContext.getPackageName());
Log.i(TAG, "fixBaseContextImplOpsPackage OK!Context=%s,", baseContext);
}
}
}
}
}
private void fixBaseContextImplContentResolverOpsPackage(Context context) throws IllegalAccessException {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1 && context != null && !TextUtils.equals(context.getPackageName(), mHostContext.getPackageName())) {
Context baseContext = context;
Class clazz = baseContext.getClass();
Field mContentResolver = FieldUtils.getDeclaredField(clazz, "mContentResolver", true);
if (mContentResolver != null) {
Object valueObj = mContentResolver.get(baseContext);
if (valueObj instanceof ContentResolver) {
ContentResolver contentResolver = ((ContentResolver) valueObj);
Field mPackageName = FieldUtils.getDeclaredField(ContentResolver.class, "mPackageName", true);
Object mPackageNameValueObj = mPackageName.get(contentResolver);
if (mPackageNameValueObj != null && mPackageNameValueObj instanceof String) {
String packageName = ((String) mPackageNameValueObj);
if (!TextUtils.equals(packageName, mHostContext.getPackageName())) {
mPackageName.set(contentResolver, mHostContext.getPackageName());
Log.i(TAG, "fixBaseContextImplContentResolverOpsPackage OK!Context=%s,contentResolver=%s", baseContext, contentResolver);
}
}
}
}
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mListener = (Listener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.getClass() + " must implement Listener");
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mListener = (Listener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.getClass() + " must implement Listener");
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mListener = (Listener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.getClass() + " must implement Listener");
}
}
/**
* 下载APK
*
* @param context
* @param apkUrl
*/
public static void downLoadApk(@NonNull Context context,
@IdRes int appIcon,
@NonNull String appName,
@NonNull String channelId,
@NonNull String apkUrl) {
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,channelId);
Intent intent = new Intent(context, context.getClass());
intent.putExtra("isRetry", false);
//未完成
}
public static Class getCompatClass(Context context) {
if (context instanceof StatusService || context instanceof StatusServiceCompat)
return context.getClass(); //prevents issues disabling services during compatibility switch
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 && !((Boolean) PreferenceData.STATUS_NOTIFICATIONS_COMPAT.getValue(context))
? StatusService.class : StatusServiceCompat.class;
}
public static void printClickInfo(View view) {
Context context = view.getContext();
Class contextClass = context.getClass();
String humanId = context.getResources().getResourceName(view.getId());
String packageName = context.getResources().getResourcePackageName(view.getId());
Log.e(0 + TRACK, TOP_BORDER);
Log.e(1 + TRACK, String.format(CLASS_FORMAT, HORIZONTAL_LINE, contextClass.getCanonicalName()));
Log.e(2 + TRACK, String.format(VIEW_NAME_FORMAT, HORIZONTAL_LINE, humanId));
Log.e(3 + TRACK, String.format(PACK_NAME_FORMAT, HORIZONTAL_LINE, packageName));
Log.e(4 + TRACK, BOTTOM_BORDER);
}
/**
* Get the activity from the context of the view
*
* @param view View from which the activity will be inferred
* @return Activity that contains the given view
*/
private static Activity getActivity(final View view) {
Context context = view.getContext();
while (!(context instanceof Activity)) {
if (context instanceof ContextWrapper) {
context = ((ContextWrapper) context).getBaseContext();
} else {
throw new IllegalStateException("Got a context of class " + context.getClass() + " and I don't know "
+ "how to get the Activity from it");
}
}
return (Activity) context;
}
private static Notification.Builder notificationInit(Context context) {
Intent intent = new Intent(context, context.getClass());
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
Notification.Builder builder = new Notification.Builder(context);
builder.setSmallIcon(android.R.drawable.stat_sys_download)
.setTicker("开始下载")
.setContentTitle(GetAppInfo.getAppName(context))
.setContentText("正在更新")
.setContentIntent(pIntent)
.setWhen(System.currentTimeMillis());
return builder;
}
/**
* Get the activity from the context of the view
* @param view View from which the activity will be inferred
* @return Activity that contains the given view
*/
private static Activity getActivity(final View view) {
Context context = view.getContext();
while (!(context instanceof Activity)) {
if (context instanceof ContextWrapper) {
context = ((ContextWrapper) context).getBaseContext();
} else {
throw new IllegalStateException("Got a context of class " + context.getClass() + " and I don't know how to get the Activity from it");
}
}
return (Activity) context;
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
try {
listener = (NoteFragmentListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.getClass() + " must implement " + NoteFragmentListener.class);
}
db = NotesDatabase.getInstance(context);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof ScreenTransferController) {
mScreenTransferController = (ScreenTransferController) context;
} else {
throw new IllegalStateException(
context.getClass() + "Activity must implement ScreenTransferController");
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof ScreenTransferController) {
mScreenTransferController = (ScreenTransferController) context;
} else {
throw new IllegalStateException(
context.getClass() + "Activity must implement ScreenTransferController");
}
}
public InflatedOnMenuItemClickListener(Context context, String methodName) {
mContext = context;
Class<?> c = context.getClass();
try {
mMethod = c.getMethod(methodName, PARAM_TYPES);
} catch (Exception e) {
InflateException ex = new InflateException(
"Couldn't resolve menu item onClick handler " + methodName +
" in class " + c.getName());
ex.initCause(e);
throw ex;
}
}
private String getVersionName(Context ctx) {
try {
ComponentName comp = new ComponentName(ctx, ctx.getClass());
PackageInfo pinfo = ctx.getPackageManager().getPackageInfo(comp.getPackageName(), 0);
return pinfo.versionName;
} catch (android.content.pm.PackageManager.NameNotFoundException e) {
return "unknown";
}
}
public Notification createNotification(Context context,
MediaSessionCompat mediaSession,
long capabilities
) {
int appIcon = context.getResources().getIdentifier("ic_app_icon", "drawable", context.getPackageName());
// Media metadata
MediaControllerCompat controller = mediaSession.getController();
MediaMetadataCompat mediaMetadata = controller.getMetadata();
MediaDescriptionCompat description = mediaMetadata.getDescription();
// Notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,
NOTIFICATION_CHANNEL_ID);
builder.setContentTitle(description.getTitle())
.setContentText(description.getSubtitle())
.setSubText(description.getDescription())
.setLargeIcon(description.getIconBitmap())
.setOnlyAlertOnce(true)
.setStyle(new MediaStyle()
.setMediaSession(mediaSession.getSessionToken())
.setShowActionsInCompactView(0))
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSmallIcon(appIcon > 0 ? appIcon : R.drawable.ic_jw_developer)
.setDeleteIntent(getActionIntent(context, KeyEvent.KEYCODE_MEDIA_STOP));
// Attach actions to the notification.
if ((capabilities & PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) != 0) {
builder.addAction(R.drawable.ic_previous, "Previous",
getActionIntent(context,
KeyEvent.KEYCODE_MEDIA_PREVIOUS));
}
if ((capabilities & PlaybackStateCompat.ACTION_PAUSE) != 0) {
builder.addAction(R.drawable.ic_pause, "Pause",
getActionIntent(context,
KeyEvent.KEYCODE_MEDIA_PAUSE));
}
if ((capabilities & PlaybackStateCompat.ACTION_PLAY) != 0) {
builder.addAction(R.drawable.ic_play, "Play",
getActionIntent(context,
KeyEvent.KEYCODE_MEDIA_PLAY)
);
}
if ((capabilities & PlaybackStateCompat.ACTION_SKIP_TO_NEXT) != 0) {
builder.addAction(R.drawable.ic_next, "Next",
getActionIntent(context,
KeyEvent.KEYCODE_MEDIA_NEXT));
}
// We want to resume the existing VideoActivity, over creating a new one.
Intent intent = new Intent(context, context.getClass());
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
builder.setContentIntent(pendingIntent);
Notification notification = builder.build();
mNotificationManager.notify(NOTIFICATION_ID, notification);
return notification;
}
private StandOutWindowManager(Context context) {
this.mContext = context;
this.mServClass = context.getClass();
this.mSysWindowManager = (android.view.WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
}