2014年11月9日 星期日

NFC in Android

All my informations learned from this book

Professional NFC Application Development for Android – Vedat Coskun, Kerem Ok, busra Ozdenizci

I recommended developers in NFC Android to buy one, no matter app layer or driver layer.
It helps me learning from no basic knowledge in hardware to fully understand NFC technology in Android.
Some of my document sentences make no sense because all figures are in the book.
You have to buy one of find it out on the internet.

https://drive.google.com/file/d/0B6BL3rjr8LORVEpHYkJmdVdhMG8

2014年8月1日 星期五

android調用framework函數的用法。


最近剛好要用到android安裝apk所產生的permission granting view 如下


那其實這個view是在framework中產生的,我看光產這個view的class就四千多行,其中相關連的class更是不可能整個移植過來,上網google一下才發現(應該說是忘了)在一般app也能調用framework函數,只是你要確定安裝的device有著正規的framework,否則class會not found。

直接貼出我使用的function。這樣一來就能抓到這個view。

public static View getPermissionView(final Context context) {
    try {
    // android.widget.AppSecurityPermissions
    // AppSecurityPermissions asp = new AppSecurityPermissions(this,
    // packageName);
    // asp.getPermissionsView()
    // 原本在framework的使用方法
  
    // 使用reflection得到class
    Class clazz = Class.forName("android.widget.AppSecurityPermissions");
    Constructor[] constructors = clazz.getConstructors();
        for (Constructor c : constructors) {
        Class[] parameterTypes = c.getParameterTypes();
            for (Class cls : parameterTypes) {
                System.out.println(cls.getName());
        }
    }
    // loop找出constructor結構

    Constructor c = constructors[1];
    Object obj = c.newInstance(new Object[] { context, context.getPackageName() });
    Method method = clazz.getMethod("getPermissionsView");
    View view = (View) method.invoke(obj);
    // ll_list_permission.addView(view);

    ScrollView scrollview = new ScrollView(context);
    ScrollView.LayoutParams params = new ScrollView.LayoutParams(
        ScrollView.LayoutParams.MATCH_PARENT,
        ScrollView.LayoutParams.MATCH_PARENT);
    scrollview.setLayoutParams(params);

    LinearLayout linearLayout = new LinearLayout(context);
    LinearLayout.LayoutParams paramsLinear = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.MATCH_PARENT);
    linearLayout.setLayoutParams(paramsLinear);
    linearLayout.setOrientation(LinearLayout.HORIZONTAL);
    linearLayout.addView(view);
    scrollview.addView(linearLayout);

    return scrollview;

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

使用的機制是java的refection,然後抓出constructor來建構這個class,而後invoke他的method,這文章對於reflection寫的很詳盡。

http://godleon.blogspot.tw/2007/09/class-class-java-class-class-jvm-class.html

最後就能傳出漂亮的permission view了。


2014年1月24日 星期五

android view draggable , rotatable , scaleable by android-gesture-detectors framework

nice work by this https://github.com/Almeros/android-gesture-detectors

Basically you just imply this framework and make some flexible modified to a ImageView . there is a sample of his project .

The change i did is made the ImageView to not match the parent to whole screen instead just wrap the content . the hardest part should be rotation . you have to modify the width and height to wrap the dynamic content . it took me some hours to figure out but turned out it was just junior high school math .. bummer .