本文整理汇总了Java中android.content.ContextWrapper类的典型用法代码示例。如果您正苦于以下问题:Java ContextWrapper类的具体用法?Java ContextWrapper怎么用?Java ContextWrapper使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ContextWrapper类属于android.content包,在下文中一共展示了ContextWrapper类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: resolveMethod
import android.content.ContextWrapper; //导入依赖的package包/类
@NonNull
private void resolveMethod(@Nullable Context context, @NonNull String name) {
while (context != null) {
try {
if (!context.isRestricted()) {
Method method = context.getClass().getMethod(this.mMethodName, new Class[]{View.class});
if (method != null) {
this.mResolvedMethod = method;
this.mResolvedContext = context;
return;
}
}
} catch (NoSuchMethodException e) {
}
if (context instanceof ContextWrapper) {
context = ((ContextWrapper) context).getBaseContext();
} else {
context = null;
}
}
int id = this.mHostView.getId();
throw new IllegalStateException("Could not find method " + this.mMethodName + "(View) in a parent or ancestor Context for android:onClick " + "attribute defined on view " + this.mHostView.getClass() + (id == -1 ? "" : " with id '" + this.mHostView.getContext().getResources().getResourceEntryName(id) + "'"));
}
示例2: checkOnClickListener
import android.content.ContextWrapper; //导入依赖的package包/类
/**
* android:onClick doesn't handle views with a ContextWrapper context. This method
* backports new framework functionality to traverse the Context wrappers to find a
* suitable target.
*/
private void checkOnClickListener(View view, AttributeSet attrs) {
final Context context = view.getContext();
if (!(context instanceof ContextWrapper) ||
(Build.VERSION.SDK_INT >= 15 && !ViewCompat.hasOnClickListeners(view))) {
// Skip our compat functionality if: the Context isn't a ContextWrapper, or
// the view doesn't have an OnClickListener (we can only rely on this on API 15+ so
// always use our compat code on older devices)
return;
}
final TypedArray a = context.obtainStyledAttributes(attrs, sOnClickAttrs);
final String handlerName = a.getString(0);
if (handlerName != null) {
view.setOnClickListener(new DeclaredOnClickListener(view, handlerName));
}
a.recycle();
}
示例3: getActivity
import android.content.ContextWrapper; //导入依赖的package包/类
/**
* Get the Activity of a context. This is typically used to determine the hosting activity of a
* {@link View}
*
* @param context The context
* @return The Activity or throws an Exception if Activity couldnt be determined
*/
@NonNull public static Activity getActivity(@NonNull Context context) {
if (context == null) {
throw new NullPointerException("context == null");
}
if (context instanceof Activity) {
return (Activity) context;
}
while (context instanceof ContextWrapper) {
if (context instanceof Activity) {
return (Activity) context;
}
context = ((ContextWrapper) context).getBaseContext();
}
throw new IllegalStateException("Could not find the surrounding Activity");
}
示例4: wrap
import android.content.ContextWrapper; //导入依赖的package包/类
@SuppressWarnings("deprecation")
public static ContextWrapper wrap(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String lang = prefs.getString(Constants.PREF_APP_LANGUAGE, "");
Locale locale = StringUtils.getLocale(lang);
locale = (locale == null) ? Locale.getDefault() : locale;
Configuration configuration = context.getResources().getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
LocaleList localeList = new LocaleList(locale);
LocaleList.setDefault(localeList);
Locale.setDefault(locale);
configuration.setLocale(locale);
configuration.setLocales(localeList);
context = context.createConfigurationContext(configuration);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLocale(locale);
context = context.createConfigurationContext(configuration);
} else {
configuration.locale = locale;
context.getResources().updateConfiguration(configuration, null);
}
return new ShowChatContextWrapper(context);
}
示例5: wrap
import android.content.ContextWrapper; //导入依赖的package包/类
@SuppressWarnings("deprecation")
public static ContextWrapper wrap(Context context, String language) {
Configuration config = context.getResources().getConfiguration();
Locale sysLocale = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
sysLocale = getSystemLocale(config);
} else {
sysLocale = getSystemLocaleLegacy(config);
}
if (!language.equals("") && !sysLocale.getLanguage().equals(language)) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
setSystemLocale(config, locale);
} else {
setSystemLocaleLegacy(config, locale);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
context = context.createConfigurationContext(config);
} else {
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
}
}
return new MyContextWrapper(context);
}
示例6: unbindFromService
import android.content.ContextWrapper; //导入依赖的package包/类
/**
* @param token The {@link ServiceToken} to unbind from
*/
public static void unbindFromService(final ServiceToken token) {
if (token == null) {
return;
}
final ContextWrapper mContextWrapper = token.mWrappedContext;
final ServiceBinder mBinder = mConnectionMap.remove(mContextWrapper);
if (mBinder == null) {
return;
}
mContextWrapper.unbindService(mBinder);
if (mConnectionMap.isEmpty()) {
mService = null;
}
}
示例7: unbindFromService
import android.content.ContextWrapper; //导入依赖的package包/类
/**
* @param token The {@link ServiceToken} to unbind from
*/
public static void unbindFromService(final ServiceToken token) {
if (token == null) {
return;
}
final ContextWrapper mContextWrapper = token.mWrappedContext;
final ServiceBinder mBinder = mConnectionMap.remove(mContextWrapper);
if (mBinder == null) {
return;
}
mContextWrapper.unbindService(mBinder);
if (mConnectionMap.isEmpty()) {
mService = null;
}
}
示例8: onCreate
import android.content.ContextWrapper; //导入依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_whitelist);
if (getSupportActionBar() != null) {
getSupportActionBar().setElevation(0.0f);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
new Prefs.Builder()
.setContext(this)
.setMode(ContextWrapper.MODE_PRIVATE)
.setPrefsName(getPackageName())
.setUseDefaultSharedPreference(true)
.build();
whitelistArray = Prefs.getOrderedStringSet("whitelistArray", new LinkedHashSet<String>());
listView = (RecyclerView) findViewById(R.id.listView1);
RecyclerView.LayoutManager manager = new LinearLayoutManager(this);
listView.setLayoutManager(manager);
listView.setHasFixedSize(true);
listView.setAdapter(new RecyclerAdapter(whitelistArray.toArray(new String[whitelistArray.size()])));
}
示例9: resolveMethod
import android.content.ContextWrapper; //导入依赖的package包/类
@NonNull
private void resolveMethod(@Nullable Context context, @NonNull String name) {
while (context != null) {
try {
if (!context.isRestricted()) {
final Method method = context.getClass().getMethod(mMethodName, View.class);
if (method != null) {
mResolvedMethod = method;
mResolvedContext = context;
return;
}
}
} catch (NoSuchMethodException e) {
// Failed to find method, keep searching up the hierarchy.
}
if (context instanceof ContextWrapper) {
context = ((ContextWrapper) context).getBaseContext();
} else {
// Can't search up the hierarchy, null out and fail.
context = null;
}
}
final int id = mHostView.getId();
final String idText = id == View.NO_ID ? "" : " with id '"
+ mHostView.getContext().getResources().getResourceEntryName(id) + "'";
throw new IllegalStateException("Could not find method " + mMethodName
+ "(View) in a parent or ancestor Context for android:onClick "
+ "attribute defined on view " + mHostView.getClass() + idText);
}
示例10: extractActivity
import android.content.ContextWrapper; //导入依赖的package包/类
private Activity extractActivity(Context context) {
while (true) {
if (context instanceof Application) {
return null;
} else if (context instanceof Activity) {
return (Activity) context;
} else if (context instanceof ContextWrapper) {
Context baseContext = ((ContextWrapper) context).getBaseContext();
// Prevent Stack Overflow.
if (baseContext == context) {
return null;
}
context = baseContext;
} else {
return null;
}
}
}
示例11: testNullDeviceIdKit
import android.content.ContextWrapper; //导入依赖的package包/类
@Test @SuppressLint("HardwareIds") public void testNullDeviceIdKit() {
final CondomContext condom = CondomContext.wrap(new ContextWrapper(context), "NullDeviceId",
new CondomOptions().addKit(new NullDeviceIdKit()));
final TelephonyManager tm = (TelephonyManager) condom.getSystemService(Context.TELEPHONY_SERVICE);
assertTrue(condom.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE).getClass().getName().startsWith(NullDeviceIdKit.class.getName()));
assertPermission(condom, READ_PHONE_STATE, true);
assertNull(tm.getDeviceId());
if (SDK_INT >= M) assertNull(tm.getDeviceId(0));
assertNull(tm.getImei());
assertNull(tm.getImei(0));
if (SDK_INT >= O) assertNull(tm.getMeid());
if (SDK_INT >= O) assertNull(tm.getMeid(0));
assertNull(tm.getSimSerialNumber());
assertNull(tm.getLine1Number());
assertNull(tm.getSubscriberId());
}
示例12: getAppContext
import android.content.ContextWrapper; //导入依赖的package包/类
Context getAppContext(final String packageName) {
final Resources resources = getResources(packageName);
Context context = null;
try {
context = getHostContext().createPackageContext(packageName,
Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
} catch (PackageManager.NameNotFoundException e) {
context = getHostContext();
}
return new ContextWrapper(context) {
@Override
public Resources getResources() {
return resources;
}
@Override
public String getPackageName() {
return packageName;
}
};
}
示例13: testInitDifferentEngines
import android.content.ContextWrapper; //导入依赖的package包/类
@SmallTest
@Feature({"Cronet"})
public void testInitDifferentEngines() throws Exception {
// Test that concurrently instantiating Cronet context's upon various
// different versions of the same Android Context does not cause crashes
// like crbug.com/453845
mTestFramework = startCronetTestFramework();
CronetEngine firstEngine =
new CronetUrlRequestContext(mTestFramework.createCronetEngineBuilder(getContext()));
CronetEngine secondEngine = new CronetUrlRequestContext(
mTestFramework.createCronetEngineBuilder(getContext().getApplicationContext()));
CronetEngine thirdEngine = new CronetUrlRequestContext(
mTestFramework.createCronetEngineBuilder(new ContextWrapper(getContext())));
firstEngine.shutdown();
secondEngine.shutdown();
thirdEngine.shutdown();
}
示例14: extractActivity
import android.content.ContextWrapper; //导入依赖的package包/类
private Activity extractActivity(Context context) {
while (true) {
if (context instanceof Application) {
return null;
} else if (context instanceof Activity) {
return (Activity) context;
} else if (context instanceof ContextWrapper) {
Context baseContext = ((ContextWrapper) context).getBaseContext();
// Prevent Stack Overflow.
if (baseContext == context) {
return null;
}
context = baseContext;
} else {
return null;
}
}
}
示例15: popFromActivityStack
import android.content.ContextWrapper; //导入依赖的package包/类
public void popFromActivityStack(Activity activity) {
if(sReminderDialog!=null &&
(sReminderDialog.getContext()==activity ||
(sReminderDialog.getContext() instanceof ContextWrapper && ((ContextWrapper)sReminderDialog.getContext()).getBaseContext()==activity))){
try{
sReminderDialog.dismiss();
}catch (Throwable e){}finally {
sReminderDialog = null;
}
}
for(int x=0; x<activityList.size(); x++){
WeakReference<Activity> ref = activityList.get(x);
if(ref!=null && ref.get()!=null && ref.get()==activity){
activityList.remove(ref);
}
}
}