Android实用代码七段(五)
前言
每次分享意味着每次都有进步,本系列以实用为主,欢迎和我分享和推荐好用的代码段~~
声明
欢迎转载,但请保留文章原始出处:)
博客园:http://www.cnblogs.com
农民伯伯: http://over140.cnblogs.com
正文
1、展开、收起状态栏
public static final void collapseStatusBar(Context ctx) {
Object sbservice = ctx.getSystemService("statusbar");
try {
Class<?> statusBarManager = Class.forName("android.app.StatusBarManager");
Method collapse;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
collapse = statusBarManager.getMethod("collapsePanels");
} else {
collapse = statusBarManager.getMethod("collapse");
}
collapse.invoke(sbservice);
} catch (Exception e) {
e.printStackTrace();
}
}
public static final void expandStatusBar(Context ctx) {
Object sbservice = ctx.getSystemService("statusbar");
try {
Class<?> statusBarManager = Class.forName("android.app.StatusBarManager");
Method expand;
if (Build.VERSION.SDK_INT >= 17) {
expand = statusBarManager.getMethod("expandNotificationsPanel");
} else {
expand = statusBarManager.getMethod("expand");
}
expand.invoke(sbservice);
} catch (Exception e) {
e.printStackTrace();
}
}
Object sbservice = ctx.getSystemService("statusbar");
try {
Class<?> statusBarManager = Class.forName("android.app.StatusBarManager");
Method collapse;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
collapse = statusBarManager.getMethod("collapsePanels");
} else {
collapse = statusBarManager.getMethod("collapse");
}
collapse.invoke(sbservice);
} catch (Exception e) {
e.printStackTrace();
}
}
public static final void expandStatusBar(Context ctx) {
Object sbservice = ctx.getSystemService("statusbar");
try {
Class<?> statusBarManager = Class.forName("android.app.StatusBarManager");
Method expand;
if (Build.VERSION.SDK_INT >= 17) {
expand = statusBarManager.getMethod("expandNotificationsPanel");
} else {
expand = statusBarManager.getMethod("expand");
}
expand.invoke(sbservice);
} catch (Exception e) {
e.printStackTrace();
}
}
用途:可用于点击Notifacation之后收起状态栏
2、 获取状态栏高度
public static int getStatusBarHeight(Context context){
Class<?> c = null;
Object obj = null;
Field field = null;
int x = 0, statusBarHeight = 0;
try {
c = Class.forName("com.android.internal.R$dimen");
obj = c.newInstance();
field = c.getField("status_bar_height");
x = Integer.parseInt(field.get(obj).toString());
statusBarHeight = context.getResources().getDimensionPixelSize(x);
} catch (Exception e1) {
e1.printStackTrace();
}
return statusBarHeight;
}
Class<?> c = null;
Object obj = null;
Field field = null;
int x = 0, statusBarHeight = 0;
try {
c = Class.forName("com.android.internal.R$dimen");
obj = c.newInstance();
field = c.getField("status_bar_height");
x = Integer.parseInt(field.get(obj).toString());
statusBarHeight = context.getResources().getDimensionPixelSize(x);
} catch (Exception e1) {
e1.printStackTrace();
}
return statusBarHeight;
}
3、ListView使用ViewHolder极简写法
public static <T extends View> T getAdapterView(View convertView, int id) {
SparseArray<View> viewHolder = (SparseArray<View>) convertView.getTag();
if (viewHolder == null) {
viewHolder = new SparseArray<View>();
convertView.setTag(viewHolder);
}
View childView = viewHolder.get(id);
if (childView == null) {
childView = convertView.findViewById(id);
viewHolder.put(id, childView);
}
return (T) childView;
}
SparseArray<View> viewHolder = (SparseArray<View>) convertView.getTag();
if (viewHolder == null) {
viewHolder = new SparseArray<View>();
convertView.setTag(viewHolder);
}
View childView = viewHolder.get(id);
if (childView == null) {
childView = convertView.findViewById(id);
viewHolder.put(id, childView);
}
return (T) childView;
}
用法:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_feed_item, parent, false);
}
ImageView thumnailView = getAdapterView(convertView, R.id.video_thumbnail);
ImageView avatarView = getAdapterView(convertView, R.id.user_avatar);
ImageView appIconView = getAdapterView(convertView, R.id.app_icon);
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_feed_item, parent, false);
}
ImageView thumnailView = getAdapterView(convertView, R.id.video_thumbnail);
ImageView avatarView = getAdapterView(convertView, R.id.user_avatar);
ImageView appIconView = getAdapterView(convertView, R.id.app_icon);
用起来非常简练,将ViewHolder隐于无形。
4、 设置Activity透明
<style name="TransparentActivity" parent="AppBaseTheme">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
说明:AppBaseTheme一般是你application指定的android:theme是啥这里就是啥,否则Activity内部的空间风格可能不一致。
用途:用于模拟Dialog效果,比如再Service中没法用Dialog,就可以用Activity来模拟
5、 代码切换全屏
//切换到全屏
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
//切换到非全屏
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
//切换到非全屏
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
注意:切换到全屏时,底部的虚拟按键仍然是显示的。次方法可多次调用用于切换
用途:播放器界面经常会用到
6、调用开发者选项中显示触摸位置功能
android.provider.Settings.System.putInt(getContentResolver(), "show_touches", 1);
设置1显示,设置0不显示。
7、获取设备上已安装并且可启动的应用列表
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> activities = getPackageManager().queryIntentActivities(intent, 0)
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> activities = getPackageManager().queryIntentActivities(intent, 0)
注意:使用getInstalledApplications会返回很多无法启动甚至没有图标的系统应用。ResolveInfo.activityInfo.applicationInfo也能取到你想要的数据。
系列