1月 162017
 

直接上代码:

 

 Posted by on 2017-01-16
1月 162017
 

  有时我们需要用户给我们的应用进行五星好评,这就需要app给用户提示信息后,自动打开app在应用市场中的详情页,那么我们可以通过下面的代码打开详情页面

  其中com.jeoe.cloudnote代表你的app的包名

  Android中获取应用市场列表的方法

 Posted by on 2017-01-16

greenDAO混淆配置

 分类:Android, Java 阅读 (3,884)  No Responses »
1月 092017
 

如果你的项目中使用了greenDAO并且使用了混淆,那么请为greenDAO增加混淆配置,如下:

官方原文地址:http://greenrobot.org/greendao/documentation/updating-to-greendao-3-and-annotations/#Step_1_Update_dependencies_to_V3

Eventbus混淆配置

 分类:Android, Java 阅读 (3,070)  No Responses »
1月 092017
 

如果你的项目使用了Eventbus并且使用混淆,那么你需要为Eventbus配置混淆,代码如下:

官方原文地址:http://greenrobot.org/eventbus/documentation/proguard/

1月 092017
 

  今天写一个shape的drawable资源,实心部分是用放射形的渐变填充的,gradientRadius属性使用的百分比值来设定,如下代码:

  在小米手上运行正常,但是在三星Note3上报java.lang.IllegalArgumentException: radius must be > 0错误,经在网上搜索后将100%改成100%p就没问题了,修改后的代码如下

 

1月 072017
 

  Switch组件使用了android:track属性,设置了背景图片,但是宽度显示不全,经查track属性设置的图片并未参与Switch的高度的计算,所以当track图片的高度过高时就会显示不全.

  解决办法:

  1. 给Switch组件设置一个大的字体大小值,比如textSize=”25dp”,这样整体高度就会变大
  2.  给Switch设置一个大的padding值

  相比之下博主认为第一个方法比较合理一些

 

1月 042017
 

  Android提供一个命令行工具monkey可以对安卓程序进行测试,monkey会对应用生成随机的动作流,包括点击、触摸、手势、文字录入等,模拟人工操作应用,并在发生错误后发送报告给用户。使用方法:首先在avd manager中打开一个模拟器或者使用USB线连上真机,打开命令行窗口,输入下面的命令:

  其中com.bcoder.testapp是你的包的名字,5000代表要测试的次数

  如果你的命令行中不能执行adb命令,请找度娘给你配置一下adb

  monkey支持很多参数,下面是一个详细的参数列表:

类别 参数 描述
常规 --help 打印一个简单的使用向导(确实很简单!)。
-v

每增加一个-v就会增加输出的测试信息的详细度,共分为级别0、1、2,也就是最多输入两个-v

事件 -s <seed>

此参数后边可以跟随一个数字值,这个值用于生成随机操作的种子,当你下一次再用这个种子值操作时,会重复上一次一样的操作。

比如:你执行 adb shell monkey -p com.bcoder.testapp -s 10 1000后发现了程序的一个报错

可以在修复问题后重新执行上面的命令,monkey还会按和上次一样的顺序生成随机事件

作者注:貌似不是每次的操作完全一样

--throttle <milliseconds> 为每次的事件增加一个延时,用于放慢monkey的执行速度,单位毫秒
--pct-touch <percent> 设置触屏操作所占的百分比
--pct-motion <percent> 设置划屏操作所占的百分比
--pct-trackball <percent> 设置轨迹球操作所占的百分比
--pct-nav <percent>

设置方向键操作所占的百分比

Adjust percentage of “basic” navigation events. (Navigation events consist of up/down/left/right, as input from a directional input device.)

--pct-majornav <percent>

设置back键、menu键所占的百分比

Adjust percentage of “major” navigation events. (These are navigation events that will typically cause actions within your UI, such as the center button in a 5-way pad, the back key, or the menu key.)

--pct-syskeys <percent>

设置系统键所占的百分比

Adjust percentage of “system” key events. (These are keys that are generally reserved for use by the system, such as Home, Back, Start Call, End Call, or Volume controls.)

--pct-appswitch <percent>

Adjust percentage of activity launches. At random intervals, the Monkey will issue a startActivity() call, as a way of maximizing coverage of all activities within your package.

--pct-anyevent <percent> Adjust percentage of other types of events. This is a catch-all for all other types of events such as keypresses, other less-used buttons on the device, and so forth.
约束 -p <allowed-package-name>

指定一个或者多个包进行测试,比如-p com.bcoder.testapp,如果要指定多个包进行测试就要使用多个-p参数,比如adb shell monkey -p com.bcoder.testapp -p com.bcoder.testapp2 1000

If you specify one or more packages this way, the Monkey will only allow the system to visit activities within those packages. If your application requires access to activities in other packages (e.g. to select a contact) you’ll need to specify those packages as well. If you don’t specify any packages, the Monkey will allow the system to launch activities in all packages. To specify multiple packages, use the -p option multiple times — one -p option per package.

-c <main-category> If you specify one or more categories this way, the Monkey will only allow the system to visit activities that are listed with one of the specified categories. If you don’t specify any categories, the Monkey will select activities listed with the category Intent.CATEGORY_LAUNCHER or Intent.CATEGORY_MONKEY. To specify multiple categories, use the -c option multiple times — one -c option per category.
调试 --dbg-no-events When specified, the Monkey will perform the initial launch into a test activity, but will not generate any further events. For best results, combine with -v, one or more package constraints, and a non-zero throttle to keep the Monkey running for 30 seconds or more. This provides an environment in which you can monitor package transitions invoked by your application.
--hprof If set, this option will generate profiling reports immediately before and after the Monkey event sequence. This will generate large (~5Mb) files in data/misc, so use with care. See Traceview for more information on trace files.
--ignore-crashes

正常情况下,当测试遇到应用崩溃时,monkey就停止测试,如果使用了此参数,monkey将继续给系统发送事件直到运行完count所设定的值。

Normally, the Monkey will stop when the application crashes or experiences any type of unhandled exception. If you specify this option, the Monkey will continue to send events to the system, until the count is completed.

--ignore-timeouts

正常情况下,当遇到ANR错误时,monkey就会停止测试,加上此参数后,monkey将会继续进行测试直到运行完所设定的count值。

Normally, the Monkey will stop when the application experiences any type of timeout error such as a “Application Not Responding” dialog. If you specify this option, the Monkey will continue to send events to the system, until the count is completed.

-f

指定测试脚本文件,示例文件见文末尾

如:adb shell monkey -v -v -f /mnt/sdcard/monkey_click.txt 1000

--ignore-security-exceptions

同上,忽略安全性异常错误。

Normally, the Monkey will stop when the application experiences any type of permissions error, for example if it attempts to launch an activity that requires certain permissions. If you specify this option, the Monkey will continue to send events to the system, until the count is completed.

--kill-process-after-error Normally, when the Monkey stops due to an error, the application that failed will be left running. When this option is set, it will signal the system to stop the process in which the error occurred. Note, under a normal (successful) completion, the launched process(es) are not stopped, and the device is simply left in the last state after the final event.
--monitor-native-crashes Watches for and reports crashes occurring in the Android system native code. If –kill-process-after-error is set, the system will stop.
--wait-dbg Stops the Monkey from executing until a debugger is attached to it.

 

-f参数脚本示例

注意事项:

  1. count参数一定要放到最后面
12月 272016
 

  Zxing是一个可以实现条形码和二维码扫描的开源代码,现在利用Android Studio可以很方便的集成该代码库。下面是详细的使用步骤:

一、通过Gradle将Zxing集成到你的项目中

  打开项目中的主build.gradle文件,将以下代码添加到dependencies节点中,然后点击编辑器右上角的Sync Now按钮

  效果如下图所示

 

二、为执行代码添加按钮

  在你的Activity设计窗体中放置两个按钮,分别对应条码和二维码的操作,设置它们的onClick事件分别为onScanBarcode和onScanQrCode,代码如下:

三、扫描代码的实现

  调用Zxing很简单,创建一个IntentIntegrator对象并设置相应的属性,然后调用该实例的initiateScan方法即可打开手机摄像头开始扫描。示例代码如下

  如果调用相机不成功,可以尝试在AndroidManifest.xml中添加相机的使用权限:

四、获取扫码结果

  我们可以在onActivityResult中获得扫码的结果,通过IntentResult实例来接收扫描结果,代码如下:

五、该例的java全部java代码

  执行结果的截屏如下图:

六、其他注意事项

IntentIntegrator有两个方法是forFragment和forSupportFragment应该是用于支持Fragment和支持库版本的Fragment,如果需要在Fragment中使用Zxing可以研究一下。

12月 182016
 

  为TextView设置高度为wrap_content后,TextView中的内容很多时,就会自动往下占用空间,但是设置Maxheight属性后,又不能查看全部的文字内容,这时候就需要使用TextView可滚动,下面就是设置MaxHeight并设置可滚动的代码。

 

 Posted by on 2016-12-18
11月 202016
 

1.必须设置setLayoutManager

2.在onCreateViewHolder方法中填充布局的时候,inflate的最后一参数必须为false

3.Adapter类的getItemCount方法返回的数量必须是大于0的数量

 Posted by on 2016-11-20