2015年8月3日星期一

Handle the new uri when upload a file picked from "ACTION_GET_CONTENT"

ACTION_GET_CONTENT一般返回的uri格式是:content://com.speedsoftware.rootexplorer.content/storage/emulated/0/Download/***.png
而新版的文件选择返回的类似下面:
content://com.android.providers.downloads.documents/document/152
这种无法通过uri.getPath()获取文件路径。

2015年6月30日星期二

Relativelayout中layout_alignParentBottom和layout_marginBottom同时使用无效

  在RelativeLayout中,child view如果同时使用layout_alignParentBottom和layout_marginBottom时,marginBottom没有任何作用。

2015年3月27日星期五

android:try to insert module into kernel

1.make menuconfig
  配置界面是从各目录下的Kconfig文件中读取的,各目录下的Makefile指明了配置项生成的文件。
  配置界面press m会编译成ko文件,或者直接去Makefile里面将对应值改成m 

2.android带有insmod命令.
  insmod的时候会对模块进行2层校验:vermagic和各symbol的crc. 可以参考:http://www.ibm.com/developerworks/cn/linux/l-cn-kernelmodules/
  报错的话可以用dmesg命令查看具体出错原因。
①vermagic值类似:“3.4.39 SMP preempt mod_unload modversions ARMv7 p2v8”
 用modinfo *.ko可以查看.
 前面是版本号,在kernel代码根目录的Makefile里面。
 后面和内核编译配置有关,像modversions,在.config里面可以找到“CONFIG_MODVERSIONS=y”这一项.
 如果insmod后报错提示:version magic ‘****' should be ‘***',就可以按情况修改。
②用modprobe --dump-modversions *.ko可以查看模块内各symbol的crc值.
 如果报错提示:disagrees about version of symbol **** unknown symbol **** 则可以用hex editor之类的工具可以直接修改里面的crc值,方法来自:http://stackoverflow.com/a/11115750/1263423

2014年10月29日星期三

判断虚拟按键是否存在

不用考虑emulator的话,直接用hasPermanentMenuKey就可以了.
    
    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    private boolean hasNavBar(Context context) {
 Resources res = context.getResources();
 int resourceId = res.getIdentifier("config_showNavigationBar", 
             "bool","android");
 if (resourceId != 0) {
     boolean hasNav = res.getBoolean(resourceId);
     String sNavBarOverride = null;
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
     try {
  Class c = Class.forName("android.os.SystemProperties");
  Method m = c.getDeclaredMethod("get", String.class);
  m.setAccessible(true);
  sNavBarOverride = (String) m.invoke(null,"qemu.hw.mainkeys");
  } catch (Throwable e) {
     sNavBarOverride = null;
     }
  }

  // check override flag (see static block)
  if ("1".equals(sNavBarOverride)) {
      hasNav = false;
  } else if ("0".equals(sNavBarOverride)) {
      hasNav = true;
  }
      return hasNav;
  } else { // fallback
             return !ViewConfiguration.get(context).hasPermanentMenuKey();
  }
 }

2014年7月17日星期四

DexClassLoader加载未安装的apk

参考:  http://wangleyiang.iteye.com/blog/1791947

用DexClassLoader加载jar没什么难度,不过用无参数的构造行数反射调用apk中的Activity就有问题,被调用的Activity中
super.onCreate(savedInstanceState);
会报错,这个Activity没有被正常的初始化,Activity的父父类ContextWrapper的构造函数需要Context。只能修改要被调用的Activity了,注释掉上面出错的那行,然后提供一个有参构造函数,传一个Context过去,以后被调用的Activity里面只能使用这个Context了.
       String dexPath = Environment.getExternalStorageDirectory()
  + "/Test/AAA.apk";
 PackageInfo packageInfo = getPackageManager().getPackageArchiveInfo(
  dexPath, PackageManager.GET_ACTIVITIES);
 String activityName = packageInfo.activities[0].name;
 File dirFile = getDir("whocares", 0);
 DexClassLoader loader = new DexClassLoader(dexPath,
  dirFile.getAbsolutePath(), null,
  ClassLoader.getSystemClassLoader());
 try {
      Class clz = loader.loadClass(activityName);
      Constructor constructor = clz
   .getConstructor(new Class[] { Context.class });
      Activity activity = (Activity) constructor
   .newInstance(new Object[] { this });
      Method create = clz.getDeclaredMethod("onCreate",
   new Class[] { Bundle.class });
      create.setAccessible(true);
      create.invoke(activity, new Object[] { null });
 } catch (ClassNotFoundException e) {
  e.printStackTrace();
 } catch (NoSuchMethodException e) {
  e.printStackTrace();
 } catch (InstantiationException e) {
  e.printStackTrace();
 } catch (IllegalAccessException e) {
  e.printStackTrace();
 } catch (IllegalArgumentException e) {
  e.printStackTrace();
 } catch (InvocationTargetException e) {
  e.printStackTrace();
 }

2014年7月16日星期三

get the URI of an image resource in android

Reference:http://androidbook.blogspot.jp/2009/08/referring-to-android-resources-using.html

Two methods:

  1. android.resource://[package]/[res id]
  2. android.resource://[package]/[res type]/[res name]

Example:

  1. Uri.parse("android.resource://com.example.test/“ + R.drawable.ic_launcher)
  2. Uri.parse("android.resource://com.example.test/drawable/ic_launcher")

2014年7月15日星期二

修改长按home后出现的app

长按home默认出现的是Google Search,可以改成选择任意的Activity:

   <intent-filter>
      <action android:name="android.intent.action.ASSIST" />
      <category android:name="android.intent.category.DEFAULT" />
   </intent-filter>

     <meta-data
        android:name="com.android.systemui.action_assist_icon"
        android:resource="@android:drawable/sym_def_app_icon" />