搜尋此網誌

2016年4月21日 星期四

2016年4月18日 星期一

【Note】AssertManager

ref:
http://fecbob.pixnet.net/blog/post/38385109
http://blog.csdn.net/guanmjie/article/details/6369831
http://developer.android.com/intl/zh-cn/reference/android/content/res/AssetManager.html

Provides access to an application's raw asset files; see Resources for the way most applications will want to retrieve their resource data. This class presents a lower-level API that allows you to open and read raw files that have been bundled with the application as a simple stream of bytes.




Android 系統為每個新設計的程序提供了/assets目錄,這個目錄保存的文件可以打包在程序裡。/res 和/assets的不同點是,android不為/assets下的文件生成ID。如果使用/assets下的文件,需要指定文件的路徑和文件名。

【Android】無題 純紀錄(ComponentName )

public ComponentName (String pkg, String cls)

Added in API level 1
Create a new component identifier.
Parameters
pkgString: The name of the package that the component exists in. Can not be null.
clsString: The name of the class inside of pkg that implements the component. Can not be null.

public ComponentName (Context pkg, String cls)

Added in API level 1
Create a new component identifier from a Context and class name.
Parameters
pkgContext: A Context for the package implementing the component, from which the actual package name will be retrieved.
clsString: The name of the class inside of pkg that implements the component.

public ComponentName (Context pkg, Class<?> cls)

Added in API level 1
Create a new component identifier from a Context and Class object.
Parameters
pkgContext: A Context for the package implementing the component, from which the actual package name will be retrieved.
clsClass: The Class object of the desired component, from which the actual class name will be retrieved.

ComponentName c = new ComponentName(resolveInfo.activityInfo.packagename,
resolveInfo.activityInfo.name);
Intent i = new Intent();
i.setComponent(c);
startActivity(i);


REF:
http://blog.csdn.net/feng88724/article/details/6198446
http://chihweiwu.blogspot.tw/2013/02/androidandroid.html

通過 PackageInfo  獲取具體信息方法:

包名獲取方法:packageInfo.packageName
icon獲取獲取方法:packageManager.getApplicationIcon(applicationInfo)
應用名稱獲取方法:packageManager.getApplicationLabel(applicationInfo)
使用權限獲取方法:packageManager.getPackageInfo(packageName,PackageManager.GET_PERMISSIONS)
.requestedPermissions

通過 ResolveInfo 獲取具體信息方法:

包名獲取方法:resolve.activityInfo.packageName
icon獲取獲取方法:resolve.loadIcon(packageManager)
應用名稱獲取方法:resolve.loadLabel(packageManager).toString()

REF:
http://www.cnblogs.com/yydcdut/p/4287077.html

public boolean isIntentAvailable(Context context, String action) {  
    final PackageManager packageManager = context.getPackageManager();  
    final Intent intent = new Intent(action);  
    List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);  
   if (resolveInfo.size() > 0) {  
        return true;  
    }  
   return false;  
}
一般APP的manifest裡面要設置為launcher的activity總是要設置intent-filter:
<intent-filter>  
                <action android:name="android.intent.action.MAIN" />  
                <category android:name="android.intent.category.LAUNCHER" />  
</intent-filter>
可以通過queryIntentActivities來獲得有哪些launcher:
private void loadApps() {           
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);           
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);             
        mApps = getPackageManager().queryIntentActivities(mainIntent, 0);       
    }

public static final int COMPONENT_ENABLED_STATE_DEFAULT

Added in API level 1
Flag for setApplicationEnabledSetting(String, int, int) and setComponentEnabledSetting(ComponentName, int, int): This component or application is in its default enabled state (as specified in its manifest).
Constant Value: 0 (0x00000000)

public static final int MATCH_ALL

Added in API level 23
Querying flag: if set and if the platform is doing any filtering of the results, then the filtering will not happen. This is a synonym for saying that all results should be returned.
Constant Value: 131072 (0x00020000)

public static final int MATCH_DEFAULT_ONLY

Added in API level 1
Resolution and querying flag: if set, only filters that support the CATEGORY_DEFAULT will be considered for matching. This is a synonym for including the CATEGORY_DEFAULT in your supplied Intent.
Constant Value: 65536 (0x00010000)

UsageStats

Added in API level 21
Public Methods
voidadd(UsageStats right)
Add the statistics from the right UsageStats to the left.
intdescribeContents()
Describe the kinds of special objects contained in this Parcelable's marshalled representation.
longgetFirstTimeStamp()
Get the beginning of the time range this UsageStats represents, measured in milliseconds since the epoch.
longgetLastTimeStamp()
Get the end of the time range this UsageStats represents, measured in milliseconds since the epoch.
longgetLastTimeUsed()
Get the last time this package was used, measured in milliseconds since the epoch.
StringgetPackageName()
longgetTotalTimeInForeground()
Get the total time this package spent in the foreground, measured in milliseconds.
voidwriteToParcel(Parcel dest, int flags)
Flatten this object in to a Parcel.

2016年4月9日 星期六

【Android Studio】SDK 更新至Android N後,在xml預覽視窗出現Rendering Problem



"Failed to load the LayoutLib: com/android/layoutlib/bridge/Bridge : Unsupported major.minor version 52.0"

解決:
Usually you get error: Unsupported major.minor version 52.0
If you have installed Android N, change Android rendering version with older one and the problem will disappear.

ref:
http://stackoverflow.com/a/35917202

2016年4月5日 星期二

【Android】減少App icon容量的方法

ref:
http://developer.android.com/intl/zh-tw/tools/help/vector-asset-studio.html

Icon 下載(建議存成.svg檔,why? 為了後面步驟)

SVG轉 Vector xml(以向量圖呈現xml檔)

1.res -> right click -> New -> Vector Assert
2-1.可透過"Choose"選擇內建的PNG檔。
2-2.或是點選Local SVG file,指定PNG檔。
3.在這以指定的PNG檔作示範
4.就會在/drawable下產生,向量圖。
5.<!-- intrinsic size of the drawable -->
android:width="24dp"
android:height="24dp"
6. <!-- size of the virtual canvas -->
android:viewportHeight="16.0"
android:viewportWidth="16.0"
7.<!-- fill color -->
android:fillColor="#FF000000"

或是將多個svg/vector image包裝成ttf檔(字型檔)

使用上述工具https://icomoon.io/,可將多筆svg檔轉成icon font的ttf檔,可以做成同樣的效果。

原因:可參考Android — 讓APK裡的切圖容量減少95%的好方法

依照wiki上描述向量圖具有
  • 儲存最少的資訊,檔案大小比位圖要小,並且檔案大小與物體的大小無關
  • 在圖像處理軟體中,任意放大向量圖形,不會遺失細節或影響清晰度,因為向量圖形是與解析度無關的。無限地放大這個圓,它仍然保持平滑;用多邊形表示的曲線將會顯現出不是真正的曲線
  • 在放大的時候,直線與曲線都不會成比例地變粗,它只會保持不變或者要小於縮放比例;為了看起來比較平滑,使用簡單幾何形狀表示的不規則曲線將會成比例地變粗,並且看起來不再像這些幾何形狀
  • 儲存的物體參數可以在後面修改。這也就是說物體的運動縮放旋轉、填充等都不會降低繪製的精度。
...等優點。

或是使用Third Party Library (Android-Iconics)

這個套件內含五種圖庫(或稱字型):
此library包裝了不少icon的字型,可直接使用,省去自己加工包裝的時間。
詳細介紹,可參考[Android] Android-Iconics 圖形套件



【Mobile】UI Patterns(Android IPhone)

ref:
Best Practices for User Interface ( Android )

http://www.mobile-patterns.com/detail-views
http://android.inspired-ui.com/
https://stories.uplabs.com/the-material-design-countdown-for-2015-7c9e4d4e88fb#.hrkw9clid
http://www.android-app-patterns.com/


2016年4月4日 星期一

【Android】Third Party Library(Icon Generators)

ref:
http://romannurik.github.io/AndroidAssetStudio/index.html

【Android】如何取得目前前景(foreground) activity

快速筆記筆記,如有錯誤,歡迎底下留言告知

範例[1]
ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE) ;    
  
//獲得系統運行的process 
List<ActivityManager.RunningAppProcessInfo> appList1 = mActivityManager.getRunningAppProcesses();  
for (RunningAppProcessInfo running : appList1) {  
    System.out.println(running.processName);  
}  
System.out.println("================");  
  
//獲得當前正在運行的service  
List<ActivityManager.RunningServiceInfo> appList2 = mActivityManager.getRunningServices(100);  
for (ActivityManager.RunningServiceInfo running : appList2) {  
    System.out.println(running.service.getClassName());  
}  
  
System.out.println("================");  
  
//獲得當前正在運行的activity  
List<ActivityManager.RunningTaskInfo> appList3 = mActivityManager.getRunningTasks(1000);  
for (ActivityManager.RunningTaskInfo running : appList3) {  
    System.out.println(running.baseActivity.getClassName());  
}  
System.out.println("================");  
  
//獲得最近運行的應用  
List<ActivityManager.RecentTaskInfo> appList4 = mActivityManager.getRecentTasks(100, 1);  
for (ActivityManager.RecentTaskInfo running : appList4) {  
    System.out.println(running.origActivity.getClassName());  
}

===================================================================

  • 方法二ActivityLifecycleCallbacks
    • 優點:不需額外權限,Android 4+適用
    • 缺點:只能取得自身foreground activity,無法取得其他foreground activity。
===================================================================


  • 方法三:讀取/proc底下訊息( linux kernal core會把process info儲存在/proc目錄下,可用shell獲取此資訊,再透過屬性判斷為foreground process)
    • 優點:不需額外權限
    • 可獲得其他activity。
    • 缺點:當/proc目錄下,檔案過多,此方法耗時。
    • 此方法可能算是android下的漏洞,有可能被改掉。
範例[2]:AndroidProcesses(Third Party Library@github.io)


===================================================================
===================================================================
  • 方法五:協助工具
    • 續~等我測試後成功再補上

2016年4月3日 星期日

【Android】Third Party Library(Others)

ref:
https://github.com/Trinea/android-open-project
https://github.com/lightSky/Awesome-MaterialDesign

一些不常用,但有趣的library。






  • android-ago (自動更新以文字顯示的時間)
    • https://github.com/curioustechizen/android-ago#android-ago




【Android】Third Party Library(Dialogs)

ref:
https://github.com/Trinea/android-open-project
https://github.com/lightSky/Awesome-MaterialDesign

【Android】Third Party Library(View & Widget)

ref:
https://github.com/Trinea/android-open-project
https://github.com/lightSky/Awesome-MaterialDesign

View


Widget



【Android】Third Party Library(Menu & Drawer)

ref:
https://github.com/Trinea/android-open-project
https://github.com/lightSky/Awesome-MaterialDesign





  • Material Design Android Library

  • 【Android】Third Party Library(FloatingActionButton)

    ref:
    https://github.com/Trinea/android-open-project
    https://github.com/lightSky/Awesome-MaterialDesign

    實在不想找每次library時,都由上述網址,從頭到尾掃完一次。挺花時間的,所以整理一下,可能會用到的。