显示标签为“android”的博文。显示所有博文
显示标签为“android”的博文。显示所有博文

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" />

2014年6月27日星期五

Android sdk manger cannot download anything in China

因为goagent不能用了,所以只能用改hosts了。
1.先选中Force https://... sources to be fetched using http://...
2. hosts里面添加:
203.208.46.146 dl.google.com
203.208.46.146 dl-ssl.google.com

2014年6月15日星期日

proguard与gson,searchview (v7)

与proguard同时使用要注意:

使用gson时(参见https://code.google.com/p/google-gson/source/browse/trunk/examples/android-proguard-example/proguard.cfg):
##---------------Begin: proguard configuration for Gson  ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature

# For using GSON @Expose annotation
-keepattributes *Annotation*

# Gson specific classes
-keep class sun.misc.Unsafe { *; }
#-keep class com.google.gson.stream.** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }

##---------------End: proguard configuration for Gson  ----------

使用android support v7中的searchview时:
-keep class android.support.v7.widget.SearchView { *; }

2013年9月2日星期一

判断statusbar是否存在

private boolean checkStatusbar(){
   Rect rect=new Rect();
   getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
   //屏幕高度还有别的办法得到,不赘述
   return (rect.bottom-rect.top)
           ==getResources().getDisplayMetrics().heightPixels;
}

另一种计算一般statusbar高度的方法(固定值,不是实时的高度):
(参考http://mrtn.me/blog/2012/03/17/get-the-height-of-the-status-bar-in-android/)
private int getStatusBarHeight() {
   int result = 0;
   int resourceId = getResources()
           .getIdentifier("status_bar_height", "dimen", "android");
   if (resourceId > 0) {
         result = getResources().getDimensionPixelSize(resourceId);
   }
 return result;
}

2013年8月31日星期六

onTouchEvent, onClick及onLongClick中各事件的顺序

(首先要知道:Returns true if the listener consumed event, false otherwise.)

onTouchEvent的一般过程:
   down-- move --up

全返回false:
down--move--longClick--move--up--onClick

onLongClick返回true:
down--move--longClick--move--up

up返回true:
短按:
down--move--up------longClick(??why?)
长按:
down--move--longClick--move--up

move返回true:
无影响

down返回true:  (不是点击事件)
短按:
没有click
长按:
没有click和longClick

2013年8月23日星期五

Show screen of details about a particular application

public static void showDetailsOfApp(Context context,
 String packageName) {
 Intent intent = new Intent();
 final int apiLevel = Build.VERSION.SDK_INT;
 if (apiLevel >= 9) {
 intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
//  .setData(Uri.fromParts("package", packageName, null));
  .setData(Uri.parse("package:"+packageName));
 } else {
  final String appPkgName = (apiLevel == 8 ? "pkg"
    : "com.android.settings.ApplicationPkgName");
    intent.setAction(Intent.ACTION_VIEW)
    .setClassName("com.android.settings",
      "com.android.settings.InstalledAppDetails")
    .putExtra(appPkgName, packageName);
    }
  context.startActivity(intent);
  }

2013年7月20日星期六

2013年6月8日星期六

onPageStarted,shouldOverrideUrlLoading

页面第一次加载:
onPageStarted()

点击页面上的超链接:
onPageStarted()   此时,参数中的url仍是之前的地址
shouldOverrideUrlLoading()   这个url参数才是新的
onPageStarted()   也是新的

2013年6月4日星期二

problem:The type android.support.v4.app.ListFragment cannot be resolved

导入一个项目后错误提示(不完整,引用了ActionbarSherlock):
android The project was not built since its build path is incomplete.
The type android.support.v4.app.ListFragment cannot be resolved.

IDE是eclipse,查了半天也没什么头绪。最后看项目的目录结构有点不对劲,library排在了src前面,不知道是不是这个有影响,修改了下Order and Export的顺序,果然好了:

2013年5月4日星期六

eclipse crashed when exporting apk

#
# An unexpected error has been detected by Java Runtime Environment:
#
#  SIGBUS (0x7) at pc=0x00007f99ab324031, pid=14598, tid=140296281274112
#
# Java VM: Java HotSpot(TM) 64-Bit Server VM (10.0-b22 mixed mode linux-amd64)
# Problematic frame:
# C  [libzip.so+0xb031]
#
# If you would like to submit a bug report, please visit:
#   http://java.sun.com/webapps/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

暂时解决方法:
取消自动编译

2013年3月27日星期三

queryBroadcastReceivers ACTION_MEDIA_MOUNTED

ACTION_MEDIA_MOUNTED等一系列与external media状态有关的broadcast actions都需要在intent-filter中设置scheme为file。

要查询这些事件的receiver,queryBroadcastReceivers的参数intent也必须匹配data的scheme属性.

implicit intents需要匹配:(参见intent 以及 intents-filters)

1.action
2.data (both URI and data type)
3.category

其中data的uri格式是:
scheme://host:port/path

在ACTION_MEDIA_*事件中,scheme为"file",host和port省略,path是external media的路径。

2013年3月25日星期一

RelativeLayout中的overlaping

今天用RelativeLayout,左边是上下两层textview,右边是一个checkbox,出现了overlapping,以前没怎么用过相对布局,才知道和FrameLayout一样也会overlapping.

具体:

前者不设置,或者与parent左对齐,这里没什么必要。
后者如下设置:
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/left"
结果以前者为参照,后者会被覆盖。

改为:
前者如下设置:
android:layout_toLeftOf="@+id/right"

后者只设置与parent右对齐:
android:layout_alignParentRight="true"
这样结果就会以后者为参照,左边的textview如果过长会自动换行。