【android】关于getBaseContext();this等-----项目中遇到的问题

      项目中要添加收藏功能,操作其实并不难:长按视频后弹出对话框,选择是否收藏,若选择是,则把视频的信息存入数据库中。在“我的收藏”页面中用listview载入数据库中的收藏的视频信息。

     完成这个功能期间,写了如下代码

<span style="font-size:18px;">PpVodItem selectedlikes=(PpVodItem)view.getTag();  
OperateDAO newdao =new OperateDAO(getBaseContext());
newdao.insertVodIntoPlayLikes(selectedlikes);</span>
    之前在getBaseContext()的位置是用this代替,eclipse始终报错,使用getBaseContext()却能正常进行,困扰我的问题用getBaseContext()轻易解决了。其实获得context的方法至少有三种:

Context = this; 
Context = getParent(); 
Context = getBaseContext(); 
Context = getParent(); 


三种方法的异同整理如下:


1.getApplicationContext ()方法在应用程序要被摧毁时,返回整个应用程序生命周期的应用上下文,生命周期是整个应用。


2.this应用上下文返回activity的当前上下文,属于activity。当它被摧毁时,activity也被摧毁。


3.getBaseContext()是 ContextWrapper中的方法,返回由构造函数指定或setBaseContext()设置的上下文。


4.getParent():若是子类,则返回子activity的上下文


getApplicationContext() Application context is associated with the Applicaition and will always be the same
throughout the life cycle.


getBasecontext() should not be used just use Context instead of it which is associated with the activity and could
possible be destroyed when the activity is destroyed.


getApplication() is available to Activity and Services only. Although in current Android Activity and Service
implementations, getApplication() and getApplicationContext() return the same object, there is no guarantee that


this will always be the case (for example, in a specific vendor implementation). So if you want the Application class you registered in the Manifest, you should never callgetApplicationContext() and cast it to your application, because it may not be the application instance (which you obviously experienced with the test framework).


getParent() returns object of the activity if the current view is a child..In other words returns the activity object hosting the child view when called within the child.


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Android 7.1 AOSP添加屏幕密度选项并实现功能选项的功能,可以按照以下步骤进行操作: 1. 在AOSP源代码找到Settings应用程序的代码目录,路径为packages/apps/Settings。 2. 在该目录下找到res/values/arrays.xml文件,并打开该文件。 3. 在该文件添加一个新的数组,用于表示屏幕密度选项的值。例如: ``` <string-array name="screen_density_values"> <item>120</item> <item>160</item> <item>240</item> <item>320</item> <item>480</item> <item>640</item> </string-array> ``` 4. 在该文件添加一个新的数组,用于表示屏幕密度选项的标签。例如: ``` <string-array name="screen_density_labels"> <item>@string/screen_density_120</item> <item>@string/screen_density_160</item> <item>@string/screen_density_240</item> <item>@string/screen_density_320</item> <item>@string/screen_density_480</item> <item>@string/screen_density_640</item> </string-array> ``` 5. 在该文件添加对应的字符串资源,例如: ``` <string name="screen_density_120">Low Density (120)</string> <string name="screen_density_160">Medium Density (160)</string> <string name="screen_density_240">High Density (240)</string> <string name="screen_density_320">Extra High Density (320)</string> <string name="screen_density_480">Extra Extra High Density (480)</string> <string name="screen_density_640">Extra Extra Extra High Density (640)</string> ``` 6. 在Settings应用程序的代码,找到DisplaySettings.java文件,并打开该文件。 7. 在该文件找到屏幕密度选项的相关代码,在onCreate方法添加以下代码: ``` mScreenDensityPreference = (ListPreference) findPreference(KEY_SCREEN_DENSITY); mScreenDensityPreference.setEntries(R.array.screen_density_labels); mScreenDensityPreference.setEntryValues(R.array.screen_density_values); mScreenDensityPreference.setValue(String.valueOf(currentDensity)); mScreenDensityPreference.setOnPreferenceChangeListener(this); ``` 其,KEY_SCREEN_DENSITY是一个常量,表示屏幕密度选项对应的键值。currentDensity是当前屏幕密度的值。 8. 在该文件实现功能选项的功能,在onPreferenceChange方法添加以下代码: ``` if (preference == mScreenDensityPreference) { int density = Integer.parseInt((String) newValue); DisplayMetrics metrics = getResources().getDisplayMetrics(); metrics.densityDpi = density; getBaseContext().getResources().updateConfiguration(getResources().getConfiguration(), metrics); try { IWindowManager wm = IWindowManager.Stub.asInterface(ServiceManager.getService("window")); if (wm != null) { wm.updateSettings(); } } catch (RemoteException e) { Log.e(TAG, "Unable to update window manager settings", e); } return true; } ``` 其,newValue是用户选择的屏幕密度的值。该代码将用户选择的屏幕密度值更新到系统的DisplayMetrics,并更新配置信息。然后,它调用IWindowManager接口更新窗口管理器的设置。 9. 构建AOSP源代码并运行生成的系统镜像。 10. 在系统设置,找到显示选项,即可看到屏幕密度选项,并且可以根据用户选择的屏幕密度值来改变屏幕的显示效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值