Android学习笔记

来源:互联网 发布:王珮瑜 冯蕴 知乎 编辑:程序博客网 时间:2024/04/30 12:27

2010-11-12 星期四

 

:建立HELLO WORLD工程。

 

:编写activity需要注意什么?

 

:资源相关

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

     <Button

        android:id="@+id/btn_open_search"

         ...

         />

    

     Note:

     ButfindViewById() can only be used with Views that are placed within theLayout 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 herethe use of the '@' prefix to introduce a resource reference -- the textfollowing that is the name of a resource

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

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

 

    <EditTextandroid: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 LogCatis a part of the DDMS (Dalvik Debug Monitor service) that provides a mechanismfor collecting and

    viewingsystem debug output. Logs from various applications and portions of the systemare collected in the

    LogCat,which then can be viewed and filtered.

   

    如何在代码中使用LogCat

    importandroid.util.Log;

    Then you areable to use debugging statement.like this;

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

   

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

    try

    {

       throw newException();

    }

   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)(){

       publicvoid 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 oursubactivity.

          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 wehave to extract that information within our SubActivity. Every Activity canaccess the original Intent it was started with, by calling getIntent().

      If therewas a Bundle attached to the Intent we can grab that by usinggetIntent().getExtras().

     

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

      Bundle b =getIntent().getExtras();

      stringstrEdit = b.getString(StartupActivity.MY_DEFAULTSTRING_ID);

     

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

     this.et_keyword.setText(strEdit);

     

    返回结果信息

    @Override

    protectedvoid onActivityResult(int requestCode, int resultCode, String data, Bundleextras)

    {

          //retrieve the text from EditText

          Stringkeyword = SecondActivity.this.et_keyword.getText().toString();

         SecondActivity.this.setResult(1, keyword);

         SecondActivity.this.finsih();

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

    }

   

    Note:eclipsesupprot 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 listtargets

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

  androidcreate  

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

  产生android.iniandroid.avd文件。

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

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

3:启动android程序。

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

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

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  

 

   androidcreate 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)

 

  模拟器启动:

    首先要有avdname: 可以使用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 <avdname>启动模拟器。否则你会看到 wait for device]

 

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

      2adbshell 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

                                     -pd:/test/android/gamefolder

                                     -k gamefolder.game

                                     -agameOneActivity

 

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

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

 

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

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

    <-p>路径下>antdebug 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>adbinstall <-n>-debug.apk

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

   

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

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

    <-p>路径下>adbinstall <-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 stoppedunexpectedly.Please try again.

   Force close.按钮。

 

原创粉丝点击