Android学习笔记

2010-11-12 星期四

 

: 建立HELLO WORLD 工程。

 

: 编写activity 需要注意什么?

 

: 资源相关

   1: 获取资源:findViewById ,在使用这个函数之前,必须要在setContentView 指定的布局文件中他给这个资源添加资源ID 号。以button 为例, 方法是:

     <Button

         android:id="@+id/btn_open_search"

         ...

         />

    

      Note:

     But findViewById() can only be used with Views that are placed within the Layout that was loaded to that Activity using setContentView()!

        

   2: 使用资源:R.resource_type.resource_name android.R.resource_type.resource_name.( 可以看R.Java 文件)

 

   3:layout 布局文件中:@ 符号前缀的意思: [ 书写格式: @[package:]type/name]

    Note here the use of the '@' prefix to introduce a resource reference -- the text following that is the name of a resource

    in the form of @[package:]type/name. In this case we didn't need to specify the package because we are referencing a

    resource in our own package. To reference a system resource, you would need to write:

 

    <EditText android:layout_width="fill_parent"

              android:layout_height="fill_parent"

              android:textColor="@android:color/opaque_red"

              android:text="Hello, World!" />

  

   4: 提供多语言版本支持:          

   在资源文件MyApp/ res / values 文件夹下创建多个语言版本映射。

   values-en/

     strings.xml

   values-en/

     strings.xml

 

   5: R.Java 是自动生成的资源文件。不可以更改,他始终是和res 中的资源文件相对应。

  

: 如何利用view 来代替 setContentView(R.layout.main.xml)?

    因为setContentView 也接收view 作为参数.

   

五:调试

    android 中不能使用System.out.printIn(...) 因为android 不是运行在java vm 上而是运行在DalvikVM 上。android 提供LogCat.

   

    The LogCat is a part of the DDMS (Dalvik Debug Monitor service) that provides a mechanism for collecting and

    viewing system debug output. Logs from various applications and portions of the system are collected in the

    LogCat, which then can be viewed and filtered.

   

    如何在代码中使用LogCat

    import android.util.Log;

    Then you are able to use debugging statement.like this;

    Log.d("DEBUGTAG", "My debug-message.");

   

    遇到异常可以使用log.e 堆栈信息:

    try

    {

       throw new Exception();

    }

    catch(Exception e)

    {

       Log.e("DEBUGTAG", "Error occured", e);   

    }

   

六:intent 如何在activity 之间传递信息。

    比如监视一个button

   

    启动子activity 方法并使用intent 将数据传递给它:

    Button b = (Button)findViewById(R.id.button01);

     b.setClickOnListener(new OnClickListener)(){

       public void onClick(View view)

       {

           Intent i = new Intent(StartupActivity.this, SecondActivity.class);

          

           // 利用intent 来传递数据

           // Create a bundle which will be attach to the intent, carry info to our subactivity.

           Bundle b = new Bundle()

           b.putString(MY_DEFAULTSTRING_ID, "anddev.org");

           i.putExtras(b);

          

           //startup subactivity.          

           startSubActivity(i, 0x1);// 第二个参数,在当我们需要返回信息的时候会很有用。/

          

       }

    });

   

    activity 如何获取主activity 传递过来的数据信息以及返回结果信息:返回结果信息需要覆盖activity 的一个方法onActivityResult(...)

    获取主activity 传递过来的数据信息

      Now we have to extract that information within our SubActivity. Every Activity can access the original Intent it was started with, by calling getIntent().

      If there was a Bundle attached to the Intent we can grab that by using getIntent().getExtras().

     

      subactivity.onCreate(...) 代码大致如下:

      Bundle b = getIntent().getExtras();

      string strEdit = b.getString(StartupActivity.MY_DEFAULTSTRING_ID);

     

      this.et_keyword = (EditText)this.findViewById((R.id.et_keyword);

      this.et_keyword.setText(strEdit);

     

     返回结果信息

    @Override

    protected void onActivityResult(int requestCode, int resultCode, String data, Bundle extras)

    {

          // retrieve the text from EditText

          String keyword = SecondActivity.this.et_keyword.getText().toString();

          SecondActivity.this.setResult(1, keyword);

          SecondActivity.this.finsih();

          super.onActivity(requestCode, resultCode, data, extras);

    }

   

    Note:eclipse supprot featured to find override method:

           source-->Override/Implement methods.

  

   

    

: 几个重要的工程文件。

   gen/R.java:

   res/layout/main.xml:

   res/values/strings.xml:

 

 

: 其他:

   在调用findViewById 的时候必须要先调用 Activity setContentView 方法。

 

2010-11-16 星期二  

1: 列出所有可用的android SDK 命令:

  android list targets

2:android 创建方法( 可以创建avd,project,test-project,lib-project)

  android create  

  利用这个方法创建avdandroid virtual device )的话,xp 系统,会在C:/Documents and Settings/<user>/.android/

  产生android.iniandroid.avd 文件。

  android.ini 文件记录着android.avd 保存的路径。

  android.avd 记录着AVD 配置文件,用户数据文件,SD 卡映像数据等,

3 :启动android 程序。

   启动选项:运行run->run configurationrun->debug configuration.

   启动:Run->Run | Android ApplicationRun->Debug | Android Application

4: 命令行创建工程文件

  ADT 自动建立android 程序结构。

  HelloAndroid

   -src

      -edu.hrbeu.HelloAndroid

        +HelloAndroid.java

   -gen       

      -edu.hrbeu.HelloAndroid

        +R.java

   -Android 1.5

      +android.jar

    assets

   -res

      -drawable

         icon.jpg    

      -layout

         main.xml

      -values

         strings.xml

     AndroidManifest.xml

     default.properties  

 

   android create project

  

5: 编译工程文件

  所有的android 应用程序都必须要进行数字签名以后才能放到模拟器上或手机上.

  eclipse 环境中,在ADT 为将android 程序安装至模拟器前,已经由内置的debug keyapk 文件自动做了数字签名。

  必须注意,在发布应用程序的时候,不能使用debug key, 而是要使用私有密钥对android 程序进行数字签名。

 

  apache ant 支持debugrelease 两种模式。但是release 没有数字签名的apk 文件。

 

  apk 文件是Android 系统的安装程序,其本身是一个zip 的压缩文件。

  apk 文件内容介绍

     res-- 资源文件

     androidManifest.xml--android 声明文件

     classes.dexs--Dalvik 虚拟机的可执行程序

     resources.arsc-- 编译后的二进制资源文件

    

7: 模拟器启动及上传工程文件至模拟器(adb.exe)

 

  模拟器启动:

    首先要有avd name: 可以使用android list avd 列出所建立的avd. 记住Name 的值。

    然后使用命令 emulator -avd <avd name>

 

  上传文件:

    adb.exe 工具除了能够在android 模拟器中上传和下载文件,还能够管理模拟器的状态,是调试程序的好工具。

    cmd 中:在 <projectName>/bin 目录下:adb install <projectName>-debugapk.

 

  启动应用程序(apk 文件上传后,需要手动启动):

    在模拟器正下方选择应用程序按钮然后选择你应用程序的图标进行点击。

   

  编译和打包应用程序:

    修改了工程文件需要使用apache ant 进行重新编译和打包把apk 文件上传至模拟器

    如果新程序的包名称没有改变,则在使用adb.exe 上传apk 文件到模拟器时,会出现错误提示。

     Failure [INSTALL_FAILED_ALREADY_EXISTS]

    此时需要删除原有的apk 文件,再使用adb.exe 工具上传新的apk 文件。

   

    删除apk 文件的方法:( 注意在使用以下命令时必须要使用 emulator -avd <avd name> 启动模拟器。否则你会看到 wait for device]

 

      1 :使用adb uninstall < 包名称 提示“success ”则表示删除成功。

      2adb shell rm /data/app/< 包名称> 没有提示表明删除成功。

     

命令行创建,编译,运行总结:

1:)android create project -n -t -p -k -a

    Action "create project":

            Creates a new Android Project.

        Options:

            -n --name     Project name

            -t --target   Target id of the new project [required]

                 -p --path     Location path of new project [required]

            -k --package  Package name [required]

            -a --activity Activity name [required]

 

要得到

  <-p>, 用于ant

  <-n> ,用于adb 上传

  <-k>, 用于adb 重新建立apk

  就好了

 

  (android%ANDROID_HOME%/tools 下)

  <-p> 路径下>android create project -n gameOne

                                     -t 7 // 这个参数是通过android list target 命令得到的id

                                     -p d:/test/android/gamefolder

                                     -k gamefolder.game

                                     -a gameOneActivity

 

  如果工程已经存在但是没有build.xml 文件, 可以如下使用命令:

  android update -p <prjPath> -t <androidID>[ 前提该工程必须要有AndroidManifest.xml 文件]

 

2:<-p> 路径下>ant debug 这个时候就在 <-p>/bin 生成了<-n>-debug.apk 文件。

    在执行ant debug 这个命令的时候就是要查找build.xml 文件,而这个文件就在<-p> 路径下。

    <-p> 路径下>ant debug ant%ANT_HOME%/bin 下)

   

3:) 启动模拟器:emulator%ANDROID_HOME%/tools 下)

    <-p> 路径下>emulator -avd <avdname>  //avdname 是通过android list avd 命令得到到name 值。 

 

4:) 利用adb 工具上传apk 至模拟器。(adb%ANDROID_HOME%/tools 下)

    <-p 路径下/bin>adb install <-n>-debug.apk

    运行这个命令有时候会出现失败的情况,不要紧,多试几次。

   

5:) 重建apk 文件方法(adb%ANDROID_HOME%/tools 下)

    <-p> 路径下>adb uninstall <-k> adb shell rm /data/app/<-k>

    <-p> 路径下>adb install <-n>-debug.apk

     

2010-11-25 星期四

1android 例子代码:

   LunarLander :它使用SurfaceView ,这对于一个每秒需要处理最多帧的游戏来说是合适的。

   GLView :可以处理3D 显示的很多初始化工作。

 

2 :性能是任何游戏的主要问题。我们的目标是使得游戏的反应越快越好,看起来越流畅越好。某些方法如Canvas.drawLine 比较慢

 

3: 3D 游戏开发书籍

  3D 游戏编程和计算机图形学数学》

 

 

2010-11-29 星期一

1 :运行snake 出现 The application snake on a phone(process com.example.android.snake)has stopped unexpectedly.Please try again.

   Force close. 按钮。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值