每天一点小知识【4】

以下是本人在工作过程中的一些小知识点总结,其中有代码片段,也有知识点,经验总结和分享。

1、如何根据传入的包名来显示对应应用的详细信息?

 private static final String SCHEME = "package";  
private static final String APP_PKG_NAME_21 = "com.android.settings.ApplicationPkgName";
private static final String APP_PKG_NAME_22 = "pkg";
private static final String APP_DETAILS_PACKAGE_NAME = "com.android.settings";
private static final String APP_DETAILS_CLASS_NAME = "com.android.settings.InstalledAppDetails";
private void showInstalledAppDetails(Context context, String packageName) {
Intent intent = new Intent();
final int apiLevel = Build.VERSION.SDK_INT;
if (apiLevel >= 9) { // 2.3(ApiLevel 9)以上,使用SDK提供的接口
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts(SCHEME, packageName, null);
intent.setData(uri);
} else { // 2.3以下,使用非公开的接口(查看InstalledAppDetails源码)
// 2.2和2.1中,InstalledAppDetails使用的APP_PKG_NAME不同。
final String appPkgName = (apiLevel == 8 ? APP_PKG_NAME_22
: APP_PKG_NAME_21);
intent.setAction(Intent.ACTION_VIEW);
intent.setClassName(APP_DETAILS_PACKAGE_NAME,
APP_DETAILS_CLASS_NAME);
intent.putExtra(appPkgName, packageName);
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent);
if(context instanceof Activity){
((Activity) context).overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
}

2、如何获取所有的应用图标?

private void saveAppIcons() {
// TODO Auto-generated method stub
new Thread(){
@Override
public void run() {
super.run();
ArrayList<AppLauncher> infos = new ArrayList<AppLauncher>();
//获取所有的launcher应用
Intent launcherIntent = new Intent("android.intent.action.MAIN", null);
launcherIntent.addCategory("android.intent.category.LAUNCHER");
PackageManager packageManager = getPackageManager();
//查询launcher应用信息
List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(launcherIntent, 0);
int size = resolveInfos.size();
for (int i = 0; i < size; i++) {
//通过ResolveInfo得到所有的应用信息,包括包名,应用程序名,图标等信息
ResolveInfo resolveInfo = resolveInfos.get(i);
String packageName = resolveInfo.activityInfo.packageName;
String acname = resolveInfo.activityInfo.name;
Drawable icon = resolveInfo.activityInfo.loadIcon(packageManager);
//todo save or use
}
}
}.start();
}

3、获取状态栏的高度的方法。

这里总结了两种方法,可以根据不同情况选择适合自己的。

(1)、直接调用系统函数

private int getStatusBarHeight(){
Rect rectangle = new Rect();
Window window =getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
int statusBarHeight = rectangle.top;
return statusBarHeight;
}

(2)、通过反射的方法

public static int getStatusBarHight(Context context) {
int statusBarHeight = -1;
try {
Class<?> clazz = Class.forName("com.android.internal.R$dimen");
Object object = clazz.newInstance();
int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
statusBarHeight = context.getResources().getDimensionPixelSize(height);
} catch (Exception e) {
e.printStackTrace();
}
return statusBarHeight;
}

4、如何在activity运行时正确的获取某个view的宽高信息?

我们知道,获取view信息必须在view绘制完成后,也就是Activity彻底运行起来后才能获取,不然getWidth(),getHight()得到都为0,这里个人总结了两种方法,可以准确获取view宽高信息。

(1)、利用ViewTreeObserver这个类来获取view的宽高信息

view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Log.i("ViewTreeObserver : view.getWidth():" + view.getWidth()+ " view.getHeight():" + view.getHeight());
}
}

这个类是用来注册当view tree全局状态改变时的回调监听器,这些全局事件包括很多,比如整个view tree视图的布局,视图绘制的开始,点击事件的改变等。

注意:不要在应用程序中实例化ViewTreeObserver对象,因为该对象仅是由视图提供的。

(2)、还有一个方法是onPostCreate( )Activity彻底运行起来之后的会回调此方法。我们可以重写onPostCreate()来获取某个view的宽高

5、 项目推荐。

GitHub 有一个十分详细的针对数十种项目及语言的 .gitignore 文件列表,地址为:
https://github.com/github/gitignore目前star达到了50K+,有需要的可以拿走。
这里写图片描述

6、无线AP是什么?

无线AP为Access Point简称,翻译为“无线访问节点”,它主要是提供无线工作站对有线局域网和从有线局域网对无线工作站的访问,在访问接入点覆盖范围内的无线工作站可以通过它进行相互通信。通俗的讲,无线AP是无线网和有线网之间沟通的桥梁。

7、音频采样率。

音频采样率是指录音设备在一秒钟内对声音信号的采样次数,采样频率越高声音的还原就越真实越自然。在当今的主流采集卡上,采样频率一般共分为22.05KHz、44.1KHz、48KHz三个等级。22.05KHz只能达到FM广播的声音品质,44.1KHz则是理论上的CD音质界限,48KHz则更加精确一些。我们平时常用的采样率有:8,000 Hz - 电话所用采样率, 对于人的说话已经足够。11,025 Hz 22,050 Hz - 无线电广播所用采样率。

8、音频比特率(码率)。

比特率这个词有多种翻译,比如码率等,表示经过编码(压缩)后的音频数据每秒钟需要用多少个比特来表示,而比特就是二进制里面最少的单位,要么是0,要么是1。比特率与音频压缩的关系简单的说就是比特率越高音质就越好,但编码后的文件就越大;如果比特率越少则情况刚好翻转。
比特率使用音频数据每秒钟需要用多少个比特来表示,与它们密切相关的是表示每个PCM样点的bit位数。比如8bit,16bit。
以电话为例,每秒3000次取样,每个取样是7比特,那么电话的比特率是21000。而CD是每秒44100次取样,两个声道,每个取样是13位PCM编码,所以CD的比特率是44100*2*13=1146600,也就是说CD每秒的数据量大约是144KB,而一张CD的容量是74分等于4440秒,就是639360KB=640MB。
比特率可以通过采样率算出来, 前提知道采样率,声道数,每个采样的编码位数,如果采样编码位数为16为,声道为1,并且1秒钟8000次采样,则采样率为8000*1*16bps。比特率也代表了每1秒的数据量是多少,通过时间,可以计算容量,或者由容量推导出播放时间。

9、小规范。

写XML布局文件时,要使用错行分层的方法,增加新的控件时,如果此控件带有id,那id应该和此空间放在第一行,没有id的要另起一行进行书写,例如,

带id的LinearLayout写法:
<LinearLayout android:id="@+id/check_result_discription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="2dip"
android:background="@drawable/check_head_background"
android:orientation="vertical"
android:visibility="gone">

</LinearLayout>
不带id的LinearLayout写法:
<LinearLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical">

<Button android:id="@+id/check_result_button_fix"
style="@style/checkButtonStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/check_button_fix"/>

<Button android:id="@+id/check_result_button_again"
style="@style/checkButtonStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/check_button_again"/>

</LinearLayout>

其他控件类似处理,这样层次分明,便于阅读和维护。控件与控件之前以空行分割。

10、AlarmManager的使用。

使用如下语句创建定时发送广播的闹钟后,

AlarmManager am = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(****);
PendingIntent sender = PendingIntent.getBroadcast(
mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
//设置周期
am.setRepeating(AlarmManager.RTC_WAKEUP, atTimeInMillis, interval, sender);

在手动修改系统时间后,应该会收到android.intent.action.DATE_CHANGED的广播,但是经过测试发现,如果修改的系统时间大于之前修改的时间,这个广播是可以收到的;但如果往前修改,即修改的系统时间小于当前值,将收不到此广播。但可以收到这个广播–android.intent.action.TIME_SET,所以应该使用它来进行监控系统时间的改变。

这里写图片描述

欢迎您扫一扫上面的微信公众号,订阅我的个人公众号!
本公众号将以推送Android各种碎片化小知识或小技巧,以及整理Android面试知识点为主,也会不定期将开发老司机日常工作中踩过的坑,平时自学的一些知识总结出来进行分享。每天一点干货小知识把你的碎片时间充分利用起来。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值