2011年6月29日

我们都知道用"adb install filename.apk"命令可以安装一个android程序,那你知道在安装后如何启动你的程序吗?

试试下面的命令吧。

adb shell am start -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -n breakan.test/breakan.test.TestActivity

或简单一些。

adb shell am start -n breakan.test/breakan.test.TestActivity

其中"breakan.test/breakan.test.TestActivity"中的"breakan.test"是程序的包名,"TestActivity"是程序Activity类的类名。

我们来看下adb shell am命令的帮助。

usage: am [subcommand] [options]

start an Activity: am start [
-D] [-W] <INTENT>
-D: enable debugging
-W: wait for launch to complete

start a Service: am startservice
<INTENT>

send a broadcast Intent: am broadcast
<INTENT>

start an Instrumentation: am instrument [flags]
<COMPONENT>
-r: print raw results (otherwise decode REPORT_KEY_STREAMRESULT)
-e <NAME> <VALUE>: set argument <NAME> to <VALUE>
-p <FILE>: write profiling data to <FILE>
-w: wait for instrumentation to finish before returning

start profiling: am profile
<PROCESS> start <FILE>
stop profiling: am profile
<PROCESS> stop

<INTENT> specifications include these flags:
[
-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]
[
-c <CATEGORY> [-c <CATEGORY>] ...]
[
-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]
[
--esn <EXTRA_KEY> ...]
[
--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]
[
-e|--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]
[
-n <COMPONENT>] [-f <FLAGS>]
[
--grant-read-uri-permission] [--grant-write-uri-permission]
[
--debug-log-resolution]
[
--activity-brought-to-front] [--activity-clear-top]
[
--activity-clear-when-task-reset] [--activity-exclude-from-recents]
[
--activity-launched-from-history] [--activity-multiple-task]
[
--activity-no-animation] [--activity-no-history]
[
--activity-no-user-action] [--activity-previous-is-top]
[
--activity-reorder-to-front] [--activity-reset-task-if-needed]
[
--activity-single-top]
[
--receiver-registered-only] [--receiver-replace-pending]
[
<URI>]
posted @ 2011-06-29 18:21 breakan 阅读(1208) 评论(0) 编辑

2011年6月28日

不知到为什么在开发文档中并没有注明MediaPlayer类的setVolume方法的参数的范围

public void setVolume (float leftVolume, float rightVolume)

Since: API Level
1
Sets the volume on
this player. This API is recommended for balancing the output of audio streams within an application. Unless you are writing an application to control user settings, this API should be used in preference to setStreamVolume(int, int, int) which sets the volume of ALL streams of a particular type. Note that the passed volume values are raw scalars. UI controls should be scaled logarithmically.
Parameters

leftVolume left volume scalar
rightVolume right volume scalar

网友们的说法也是很多不同版本,不过经过测试发现和SoundPool的setVolume方法的参数的范围是一样的,也是0.0到1.0

public final void setVolume (int streamID, float leftVolume, float rightVolume)

Since: API Level
1
Set stream volume. Sets the volume on the stream specified by the streamID. This is the value returned by the play() function. The value must be in the range of
0.0 to 1.0. If the stream does not exist, it will have no effect.
Parameters

streamID a streamID returned by the play() function
leftVolume left volume value (range
= 0.0 to 1.0)
rightVolume right volume value (range
= 0.0 to 1.0)

一般使用这个100个就行了

1 for (float i = 0.01F; i <= 1.00F; i += 0.01F) {
2 System.out.println(new DecimalFormat("#.##").format(i));
3 }
posted @ 2011-06-28 11:02 breakan 阅读(1046) 评论(0) 编辑

2011年6月25日

前几天写了一个Activity+View的软件开头动画效果,因为觉得以后可能还会用到,于是就给这个Activity(LogoActivity)给写了一个abstract方法showMain()以便以后继承LogoActivity实现showMain()使用。

在修改的过程中因为使用到了InputStream,但没有将它关闭,所以就在finally中吧InputStream给close()了 。突然看到前面也用到了AssetManager assetMgr=AssetManager assetMgr = this.getAssets();于是就顺手把assetMgr也close()掉了。

代码片段:

1 try {
2 strs = assetMgr.list("bgimgs");
3 bgimgs = new Bitmap[strs.length];
4 for(int i=0;i<strs.length;i++){
5 is = assetMgr.open("bgimgs/" + strs[i]);
6 datas = new byte[is.available()];
7 is.read(datas);
8 bgimgs[i] = BitmapFactory.decodeByteArray(datas, 0, datas.length);
9 }
10
11 } catch (IOException e) {
12 e.printStackTrace();
13 }finally{
14 try{
15 is.close();
16 }catch(IOException e){
17 e.printStackTrace();
18 }
19 assetMgr.close();
20 }

本来以为都是写过的代码没什么问题就没有再调试,最后写好了一个子类继承LogoActivity,实现了showMain(),但是问题出现了,程序直接挂掉。

错误提示如下:

错误提示
1 06-25 03:18:13.464: WARN/dalvikvm(1035): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
2  06-25 03:18:13.494: ERROR/AndroidRuntime(1035): FATAL EXCEPTION: main
3  06-25 03:18:13.494: ERROR/AndroidRuntime(1035): java.lang.RuntimeException: Unable to start activity ComponentInfo{breakan.util/breakan.test.BreakanActivity}: android.content.res.Resources$NotFoundException: File res/layout/screen_title.xml from xml type layout resource ID #0x1090058
4  06-25 03:18:13.494: ERROR/AndroidRuntime(1035): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
5  06-25 03:18:13.494: ERROR/AndroidRuntime(1035): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
6 06-25 03:18:13.494: ERROR/AndroidRuntime(1035): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
7 06-25 03:18:13.494: ERROR/AndroidRuntime(1035): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
8 06-25 03:18:13.494: ERROR/AndroidRuntime(1035): at android.os.Handler.dispatchMessage(Handler.java:99)
9 06-25 03:18:13.494: ERROR/AndroidRuntime(1035): at android.os.Looper.loop(Looper.java:123)
10 06-25 03:18:13.494: ERROR/AndroidRuntime(1035): at android.app.ActivityThread.main(ActivityThread.java:4627)
11 06-25 03:18:13.494: ERROR/AndroidRuntime(1035): at java.lang.reflect.Method.invokeNative(Native Method)
12 06-25 03:18:13.494: ERROR/AndroidRuntime(1035): at java.lang.reflect.Method.invoke(Method.java:521)
13 06-25 03:18:13.494: ERROR/AndroidRuntime(1035): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
14 06-25 03:18:13.494: ERROR/AndroidRuntime(1035): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
15 06-25 03:18:13.494: ERROR/AndroidRuntime(1035): at dalvik.system.NativeStart.main(Native Method)
16 06-25 03:18:13.494: ERROR/AndroidRuntime(1035): Caused by: android.content.res.Resources$NotFoundException: File res/layout/screen_title.xml from xml type layout resource ID #0x1090058
17 06-25 03:18:13.494: ERROR/AndroidRuntime(1035): at android.content.res.Resources.loadXmlResourceParser(Resources.java:1916)
18 06-25 03:18:13.494: ERROR/AndroidRuntime(1035): at android.content.res.Resources.loadXmlResourceParser(Resources.java:1871)
19 06-25 03:18:13.494: ERROR/AndroidRuntime(1035): at android.content.res.Resources.getLayout(Resources.java:731)
20 06-25 03:18:13.494: ERROR/AndroidRuntime(1035): at android.view.LayoutInflater.inflate(LayoutInflater.java:318)
21 06-25 03:18:13.494: ERROR/AndroidRuntime(1035): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
22 06-25 03:18:13.494: ERROR/AndroidRuntime(1035): at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:2165)
23 06-25 03:18:13.494: ERROR/AndroidRuntime(1035): at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:2220)
24 06-25 03:18:13.494: ERROR/AndroidRuntime(1035): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:213)
25 06-25 03:18:13.494: ERROR/AndroidRuntime(1035): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
26 06-25 03:18:13.494: ERROR/AndroidRuntime(1035): at android.app.Activity.setContentView(Activity.java:1658)
27 06-25 03:18:13.494: ERROR/AndroidRuntime(1035): at breakan.util.LogoActivity.showLogo(LogoActivity.java:61)
28 06-25 03:18:13.494: ERROR/AndroidRuntime(1035): at breakan.util.LogoActivity.onCreate(LogoActivity.java:27)
29 06-25 03:18:13.494: ERROR/AndroidRuntime(1035): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
30 06-25 03:18:13.494: ERROR/AndroidRuntime(1035): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
31 06-25 03:18:13.494: ERROR/AndroidRuntime(1035): ... 11 more
32 06-25 03:18:13.494: ERROR/AndroidRuntime(1035): Caused by: java.lang.RuntimeException: Assetmanager has been closed
33 06-25 03:18:13.494: ERROR/AndroidRuntime(1035): at android.content.res.AssetManager.openXmlBlockAsset(AssetManager.java:483)
34 06-25 03:18:13.494: ERROR/AndroidRuntime(1035): at android.content.res.Resources.loadXmlResourceParser(Resources.java:1898)
35 06-25 03:18:13.494: ERROR/AndroidRuntime(1035): ... 24 more
36 06-25 03:18:13.514: WARN/ActivityManager(59): Force finishing activity breakan.util/breakan.test.BreakanActivity
37 06-25 03:18:14.024: WARN/ActivityManager(59): Activity pause timeout for HistoryRecord{4504b648 breakan.util/breakan.test.BreakanActivity}
38 06-25 03:18:15.514: INFO/Process(1035): Sending signal. PID: 1035 SIG: 9
39 06-25 03:18:15.554: WARN/InputManagerService(59): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@44f62c80
40 06-25 03:18:15.944: INFO/ActivityManager(59): Process breakan.util (pid 1035) has died.
41 06-25 03:18:24.075: WARN/ActivityManager(59): Activity destroy timeout for HistoryRecord{4504b648 breakan.util/breakan.test.BreakanActivity}

结果我调试了好久才发现,就是因为我把Activity的AssetManager给close()掉了才产生的错误。

看来以后关闭一个对象要先看清楚是不是自己生成的对象,会不会被隐式调用。

posted @ 2011-06-25 11:42 breakan 阅读(431) 评论(0) 编辑

2011年6月23日

  对象被序列化后可被保存或传输,这个例子实现了位图的序列化和反序列化。  

  这次就对Android工程自带的”icon.png”进行下手。

  程序运行后会在/data/data/breakan.serializable/目录下生成一个bitmap.bin文件,这个文件保存的就是MyBitmap的对象。

  

 

 

 

 

 

1 package breakan.serializable;
2
3  import java.io.ByteArrayOutputStream;
4  import java.io.FileInputStream;
5  import java.io.FileOutputStream;
6  import java.io.ObjectInputStream;
7  import java.io.ObjectOutputStream;
8  import java.io.Serializable;
9
10  import android.app.Activity;
11  import android.graphics.Bitmap;
12  import android.graphics.BitmapFactory;
13  import android.graphics.Bitmap.CompressFormat;
14  import android.graphics.drawable.BitmapDrawable;
15  import android.os.Bundle;
16  import android.widget.ImageView;
17  import android.widget.TextView;
18
19  public class SerializableActivity extends Activity {
20 private Bitmap bitmap = null;
21 private ObjectOutputStream oos = null;
22 private ObjectInputStream ois = null;
23 // myBitmap1是要被序列化的对象
24 private MyBitmap myBitmap1 = null;
25 // myBitmap2是反序列化后得到的对象
26 private MyBitmap myBitmap2 = null;
27 private TextView tv1 = null;
28 private ImageView img1 = null;
29
30 /** Called when the activity is first created. */
31 @Override
32 public void onCreate(Bundle savedInstanceState) {
33 super.onCreate(savedInstanceState);
34 setContentView(R.layout.main);
35 bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.icon))
36 .getBitmap();
37 myBitmap1 = new MyBitmap(BytesBitmap.getBytes(bitmap), "icon.png");
38 tv1 = (TextView) findViewById(R.id.tv1);
39 img1 = (ImageView) findViewById(R.id.img1);
40
41 try {
42 // 序列化myBitmap对象
43 oos = new ObjectOutputStream(new FileOutputStream(
44 "/data/data/breakan.serializable/bitmap.bin"));
45 oos.writeObject(myBitmap1);
46 oos.flush();
47 // 反序列化myBitmap对象
48 ois = new ObjectInputStream(new FileInputStream(
49 "/data/data/breakan.serializable/bitmap.bin"));
50 myBitmap2 = (MyBitmap) ois.readObject();
51 } catch (Exception e) {
52 e.printStackTrace();
53 } finally {
54 try {
55 if (oos != null) {
56 oos.close();
57 }
58 if (ois != null) {
59 ois.close();
60 }
61 } catch (Exception e) {
62 e.printStackTrace();
63 }
64 }
65
66 tv1.setText(myBitmap2.getName());
67 img1.setImageBitmap(BytesBitmap.getBitmap(myBitmap2.getBitmapBytes()));
68 }
69
70 }
71
72 /**
73 * 因为Bitmap没有实现序列化,所以不能直接在序列化类(MyBitmap)中使用
74 * BytesBitmap用于实现Bitmap和byte[]间的相互转换
75 * @author joran
76 *
77 */
78 class BytesBitmap {
79 public static Bitmap getBitmap(byte[] data) {
80 return BitmapFactory.decodeByteArray(data, 0, data.length);
81 }
82
83 public static byte[] getBytes(Bitmap bitmap) {
84 ByteArrayOutputStream baops = new ByteArrayOutputStream();
85 bitmap.compress(CompressFormat.PNG, 0, baops);
86 return baops.toByteArray();
87 }
88 }
89
90 /**
91 * MyBitmap是要被序列化的类
92 * 其中包含了通过BytesBitmap类得到的Bitmap中数据的数组
93 * 和一个保存位图的名字的字符串,用于标识图片
94 * @author joran
95 *
96 */
97 class MyBitmap implements Serializable {
98 /**
99 * serialVersionUID解释:
100 * http://www.blogjava.net/invisibletank/archive/2007/11/15/160684.html
101 */
102 private static final long serialVersionUID = 1L;
103 private byte[] bitmapBytes = null;
104 private String name = null;
105
106 public MyBitmap(byte[] bitmapBytes, String name) {
107 // TODO Auto-generated constructor stub
108 this.bitmapBytes = bitmapBytes;
109 this.name = name;
110 }
111
112 public byte[] getBitmapBytes() {
113 return this.bitmapBytes;
114 }
115
116 public String getName() {
117 return this.name;
118 }
119 }

posted @ 2011-06-23 16:21 breakan 阅读(903) 评论(1) 编辑

公告

导航

统计