is it possible to show widget only for a certain android version?

淺唱寂寞╮ 提交于 2019-12-17 22:33:10

问题


is it possible to show widget in "Add to home screen > Widgets" Dialog only for a certain android version without uploading two APKs and assigning to proper SDK version numbers?


回答1:


I have a way to do it without any code e.g.:

In your res\values dir create a bools.xml file:

<?xml version="1.0" encoding="utf-8"?> <resources> <bool name="honeycombOrAbove">false</bool> </resources>

Create a values-v11 dir (for honeycomb or above values), copy the file to it and set the value to true.

Then in the manifest for the widget receiver use: android:enabled="@bool/honeycombOrAbove". I also used it in the configuration activity for the widget.

This means the widget is disabled at install time.




回答2:


You can do it with the componentEnableSetting. Just disable the widget you don't want to have listed. The change will get active after phone-restart.

Context context = getApplicationContext();
String str1 = "org.classname.to.widget.provider";
ComponentName componentName = new ComponentName(context, str1);
PackageManager packageManager = getPackageManager();

int versioncode = Integer.valueOf(android.os.Build.VERSION.SDK);    

//enable widget
packageManager.setComponentEnabledSetting(componentName, 1, 1);

//disable widget
packageManager.setComponentEnabledSetting(componentName, 2, 1);


来源:https://stackoverflow.com/questions/7064035/is-it-possible-to-show-widget-only-for-a-certain-android-version

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!