每天一點小知識【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面試知識點爲主,也會不定期將開發老司機日常工作中踩過的坑,平時自學的一些知識總結出來進行分享。每天一點乾貨小知識把你的碎片時間充分利用起來。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章