免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3606 | 回复: 2
打印 上一主题 下一主题

有些问题与答案 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-11 17:15 |只看该作者 |倒序浏览
有些问题与答案
 








 学习Android的过程中,碰到很多问题,从问题中也学到了很多。

单位
写一个视频播放器的时候,时间尺度的把握让我想了一段时间,进度条的刷新,字幕的刷新都要注意些。
  1. MediaPlayer.getduration() return int/millisecond
  2. SeekBar.getProgress() return int/scecond
复制代码
static import
javadoc的static import文档在这里。
为了获取静态成员,必须以所在类修饰引它,例如: double r = Math.cos(Math.PI * theta);
为了避开这个问题,把静态成员写入接口并继承它会是一个坏主意,这个主义的名字叫Constant Interface Antipattern,问题是一个类使用另外一个类的静态成员只不过是执行上的细节,当一个类实现了一个接口,那么它成了这个类的public API,而执行细节不应该向public API泄漏的。static import 可以免去这一麻烦,只要
import static java.lang.Math.PI; // import static CLASS_NAME.MEMBER_NAME;
或者                                         // Alternatively
import static java.lang.Math.*; // import static CLASS_NAME.*;
一旦导入了静态成员,就可以不加修饰地引用了。   double r = cos(PI * theta);
最后,你必须谨慎使用 static import ,very sparingly 。 当需要频繁引用时,可以使用该特性;滥用会导致你的程序变得不可读不好维护,污染命名空间,



java断言
如何开启java断言,问题英文答案在这里,中文的在下面。
导入JUnit Assert类,注意不要导入Junit4 framework,那个是org.junit包,在Android设备或模拟器上你必须使用junit.framework包
import static junit.framework.Assert.*;
然后尽情使用assertTrue, assertEquals, assertNull吧!
开启断言有两种方法:
1. 通过adb shell setprop debug.assert 1来设置系统属性 debug.assert
2. 给Dalvik虚拟机添加命令行参数 --enable-assert 或简写 -ea



指定的APK打开
如果是自己写的播放器,当然知道名字,直接 startActivity(new Intent(HereActivity.this,ThereActivity.class));就行了。如果是用其他人写的apk,就不清楚名字了,怎么办呢?
如果装了多个应用程序,如播放器RockPlayer, VPlayer等等, 但是就是想用RockPlayer打开。

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.redirectin.rockplayer.android.unified", "com.redirectin.rockplayer.android.OpenRockPlayerActivity"));
// 包名 类名,解压apk文件,利用网上流传的XMLprinter.jar东西java -jar XMLPrinter AndroidManifest.xml,
// 查AndroidManifest.xml文件内容,当然这样有点小题大做了。
// 简单一点,可以播放视频,选择RockPlayer打开,查看logcat信息,然后提取里面字符串,cmp是ComponentName。
  1. Uri name = Uri.parse("http://192.168.1.122/video.flv");
  2. intent.setData(name);
  3. startActivity(intent);

  4. I/ActivityManager( 1079): Starting activity: Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/video/Crysis2DirectX11UltraUpgradeTrailer_H.264%20AVC_1280x720.flv typ=video/* cmp=android/com.android.internal.app.ResolverActivity }
  5. I/ActivityManager( 1079): Displayed activity android/com.android.internal.app.ResolverActivity: 414 ms (total 414 ms)
  6. I/ActivityManager( 1079): Starting activity: Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/video/Crysis2DirectX11UltraUpgradeTrailer_H.264%20AVC_1280x720.flv typ=video/* flg=0x3800000 cmp=com.redirectin.rockplayer.android.unified/com.redirectin.rockplayer.android.OpenRockPlayerActivity }
  7. I/ActivityManager( 1079): Starting activity: Intent { cmp=com.redirectin.rockplayer.android.unified/com.redirectin.rockplayer.android.RockPlayer (has extras) }
  8. D/dalvikvm(10505): Trying to load lib /data/data/com.redirectin.rockplayer.android.unified/lib/librockplayer_8.so 0x4a3cbe90
  9. D/dalvikvm(10505): Shared lib '/data/data/com.redirectin.rockplayer.android.unified/lib/librockplayer_8.so' already loaded in same CL 0x4a3cbe90
  10. W/drawable(10505): Bad element under <shape>: background
  11. D/NativeLibName(10505): plugin .so path /data/data/com.redirectin.rockplayer.android.unified/lib/libffmpeg
  12. D/NativeLibName(10505): ARMv7, idx=8 vfpv3d32=false
  13. D/NativeLibName(10505): degressionLevel=0, path is /data/data/com.redirectin.rockplayer.android.unified/lib/libffmpeg_7n.so
  14. D/RockPlayer(10505): open optimized ffmpeg lib: /data/data/com.redirectin.rockplayer.android.unified/lib/libffmpeg_7n.so
  15. D/RockPlayer(10505): open video height = 720,width=1280
  16. D/RockPlayer(10505): Surface hasn't been initialized! NativeSurfaceView.setSurfaceChanged never got called?
  17. D/RockPlayer(10505): NativeSurfaceView.getVideoHeight: 720
  18. D/RockPlayer(10505): NativeSurfaceView.getVideoWidth: 1280
复制代码
复制代码


image/*的处理

  1. Intent intent = new Intent();
  2. Uri uri = Uri.parse("http://www.xxx.com/image/20111126/beautiful.jpg");
  3. intent.setDataAndType(uri, "image/*"); // video/* 或者 audio/* 两种试过可以的。
  4. intent.setAction(android.content.Intent.ACTION_GET_CONTENT);
  5. startActivity(intent);
复制代码
运行的话会抛出异常,logcat信息:
ERROR/AndroidRuntime(232): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.GET_CONTENT dat=http://www.xxx.com/image/20111126/beautiful.jpg typ=image/* }

intent.setDataAndType(uri, "image/*");将其改成intent.setData(uri);会启动浏览器打开图片。


  1. final

  2. public boolean onItemLongClick(AdapterView<?> parent, View view,int position,long id)
  3. {
  4.     // final int pos=position;
  5.     @Override
  6.     public void onClick(DialogInterface dialog, int which)
  7.     {
  8.         // do something here..
  9.         // st_color.remove(pos);
  10.     }
  11. }
复制代码
内部匿名类访问外部类变量需要添加final属性。
参数添加final属性不算函数重载,试着将上面的参数修改了成 final int postion,final long id
发现,运行的结果与预期不一致,表现在 lst_color.remove(position); 删除不掉数据
其中,lst_color为ArrayList<String>型数据。
然后关掉注释,即添加 final int pos=position; lst_color.remove(pos); 成功删除。
  1. ArrayList<T>与T[]

  2. ArrayList<T>与T[]之间的转换,用get()方法获取然后对数组赋值显得冗余。

  3. ArrayList<T> lst_color;
  4. T[] color=(T[])lst_color.toArray(new String[lst_color.length());
  5. lst_color=Arrays.asList(color);
复制代码
  查阅javadoc,ArrayList提供public <T> T[] toArray(T[] a)方法返回一个按照正确的顺序包含此列表中所有元素的数组;返回数组的运行时类型就是指定数组的运行时类型。如果列表能放入指定的数组,则返回放入此列表元素的数组。否则,将根据指定数组的运行时类型和此列表的大小分配一个新的数组。
  如果指定的数组能容纳列表并有剩余空间(即数组的元素比列表的多),那么会将数组中紧跟在集合末尾的元素设置为 null。这对确定列表的长度很有用,但只在调用方知道列表中不包含任何 null 元素时才有用。
  asList方法返回一个受指定数组支持的固定大小的列表,此方法同 Collection.toArray 一起,充当了基于数组的 API 与基于 collection 的 API 之间的桥梁。返回的列表是可序列化的,并且实现了 RandomAccess。



Activity与Service数据交互

通过Intent来传递数据
  1. Activity使用intent.putExtras(KEY,value);
  2. Service使用public void onStartCommand(Intent data, int startId, int flags),
  3. public void onStart (Intent intent, int startId) This method is deprecated.
复制代码
APK的安装与卸载

// 安装程序的apk文件路径
  1. String fileName = Environment.getExternalStorageDirectory() + apkName;
  2. Uri uri = Uri.fromFile(new File(fileName));
  3. Intent intent = new Intent(Intent.ACTION_VIEW);
  4. intent.setDataAndType(Uri, application/vnd.android.package-archive");
  5. startActivity(intent);

  6. // 卸载apk,通过程序的包名创建URI
  7. Uri packageURI = Uri.parse("package: poet.android.app.id");
  8. Intent intent = new Intent(Intent.ACTION_DELETE);
  9. startActivity(intent);
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-12-23 22:04 |只看该作者
谢谢分享  希望于楼主多多交流

论坛徽章:
0
3 [报告]
发表于 2011-12-23 22:04 |只看该作者
谢谢分享  希望于楼主多多交流
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP