zoukankan      html  css  js  c++  java
  • Android应用基础学习记录

    01_前言

    前言,了解了Android的情况。这里也介绍一下本文。本文是记录学习Android应用程序开发过程,视频中使用的Android2.2版本号,我以4.2版本号为基础,找出当中的差异并记录下来。会不断的更新。

    一直苦于编写后底层代码后,须要编写Apk来測试。所以学的Android应用。

    记录时环境记录:

    系统环境:Ubuntu 12.10

        Eclipse:   ingigo-sr2

           Adt  :    22.3


    02_搭建Android开发环境

    我用的是Ubuntu,所以有了这文章:

    一.Eclipse的安装

    安装Eclipse有两种方法:1.apt-get install eclipse 2.下载安装包。

    假设採用第一种方法安装的Eclipse在进行以下的步骤时遇到错误。别和错误计较。赶紧採用另外一种安装方法又一次安装Eclipse。关于Eclipse版本号的选择,我选用eclipse-java-indigo-SR2-linux-gtk.tar.gz3.7的用的时候出现解析xml极为卡的问题。

    二.ADT的安装

    ADT推荐离线安装,版本号我选择的是ADT-22.3.0.zip,我是选择过22.6.*不好用的之后才回退到22.3.0的。

    这个真心好用。这里简单说下22.6.*版本号出现的问题:1.会默认创建fragment_main.xml 2.假设要兼容2.3的程序每一个应用会多出一个附带应用appcompat_v7。

    导入ADTzip包的时候,不要勾选在线检測

    三.SDK的安装

    SDK选择和ADT配套版本号:android-sdk_r22.3-linux.tgz。用起来感觉还不错。下载其他SDK包的时候不建议将SDK更新到22.6.*版本号。应用开发版本号选择4.2。虚拟机的话,不建议用4.4的,4.4的easy崩溃。

    03_创建与启动手机模拟器和学习使用ANDROID操作系统

    一.假设实在要使用模拟器,注意下面几点

    1.不想让模拟器占满屏幕,手动启动并设置大小

    2.想要模拟器执行的快一些选择x86版本号CPU(似乎眼下仅仅有4.4的能够支持)

    3.模拟器能调试的功能有:(列出曾经觉得不能调试的东西)

      A.拨号

     B...

    4.老罗的博客上有很多其它关于模拟器的使用。

    二.怎样使用真机

    1.常常使用adb。将adb加入到环境变量中。不推荐使用apt-get install android-platform-tools安装。

    2.连接真机參考《Linux下安装Androidadb驱动-解决不能识别的问题》。

    三.关于通过Wifi调试Android设备

    1.有两种方法:1.连接到普通的无线路由;2.Ubuntu设置wifi无线热点+Android手机怎样搜索连接到共享网络+补丁

    2.又一次挂载文件系统《Android改动system仅仅读权限:remount

    3.ADB调试《Android中使用WIFI来连接ADB

    关于使用

    .......


    4_开发执行第一个应用程序

    关于apk的安装,假设採用命令行的话,我使用的是:

    $ adb push Test.apk /data/app

    我且称它为覆盖安装,即假设已经存在该apk,直接覆盖不用再运行adb uninstall。


    05_项目的文件夹结构与安装及启动过程分析

    1.关于res/drawable-*文件夹

       在Eclipse中你放到drawable-*随意文件夹放入图片资源,在编译时都能找到,可是假设在Android源代码中编译,一定要放到drawable-ldpi中才干够编译通过。否则不能编译柔肠通过。假设你想程序能够同一时候能够在EclipseAndroid源代码中成功编译那么就请把图片资源放到drawable-ldpi中。

    2.关于string.xml

       假设软件没有考虑到语言国际化或者关键实现代码逻辑而非界面。那么有可能直接字串到Activity.xml中,这样在Eclipse中编译是ok了。可是在Android源代码中编译是闷骚地只是关的。会严格不同意这样做的。仅仅能在Android.mk中加入LOCAL_MODULE_TAGS := tests才干够编译过。


    06_电话拔号器

    1.隐藏启动列表中的应用图标

    术语:禁止luncher启动apk。凝视一行:

    <!--<category android:name="android.intent.category.LAUNCHER"/> -->

    2.id/变量命名规则

    1)Xml文件里:

         开头是凝视用“Cpies”(功能名)。labelForcopies_edittext(功能名+edittext-无须要改变的TextView能够不用id。可是用的话就是例如以下labelFor的名字),文字用”label_copies”(label+功能名)。(源代码位置:这里

                      <!-- Copies -->

                    <TextView

                        .....                    

                        android:labelFor="@+id/copies_edittext"

                        android:text="@string/label_copies">

                        .....

                    </TextView>

    2)变量名_idcopies_edittext为例(源代码位置:这里

    // Copies

    mCopiesEditText = (EditText) findViewById(R.id.copies_edittext);

    所以我认为电话拨号器中的命令应该是:

    MainActivity.java

    main_activity.xml

    string.xml

    -

    -

    label_mobile_number

    mMobileNumberEditText

    mobile_number_edittext

    -

    mDialButton

    dial_button

    dial


    (局部变量就无所谓了。这里intent

    3)关于查看资料

                主页https://developer.android.com/

    Permission:  reference/android/Manifest.permission.html(链接)

              Intent:  reference/android/content/Intent.html(链接

    4)代码优化

    拨号关键代码能够优化为例如以下

    String number = mMobileNumberEditText.getText().toString();

    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + number));

    startActivity(intent);


    07_查看应用输出的错误信息与怎样部署应用到真实手机

    1关于手机驱动

    连接到Linux上不须要额外驱动。直接改动adb的配置文件就可以,不能识别參考《Linux下安装Androidadb驱动-解决不能识别的问题》。

    Android Screen Monitor

    这个软件相同实现了Linux版本号,主页和下载地址。执行:

      $ java -jar asm.jar

    就能够查看并操作手机了。


    08_短信发送器

    1.变量名

     


    MainActivity.java

    main_activity.xml

    string.xml

    -

    -

    app_name

    -

    -

    label_mobile_number

    mMobileNumberEditText

    mobile_number_edittext

    -

    mSmsContentEditText

    sms_content_edittext

    -

    mSendSmsButton

    send_sms_button

    send_sms



    09_深入了解各种布局技术

    1.四种布局 a.线形布局 b.相对布局 c.表格布局 d.帧布局

    2.图片位置假设不做屏幕适配,建议放置到drawable-ldpi中。由于在系统源代码中编译的应用,是去ldpi中找图片资源的。

    10_相应用进行单元測试

    这个着实是有用的,曾经做一些业务类測试的时候都是使用在界面上实现一个button点击,因为是界面点击的,处理超时还会被警告超时关闭.单元測试确实是一个好方法,这点C语言的gdb都没有如此的强大.做一个模板,用到的时候直接測试:

    1. copy this content into your AndroidManifest.xml

             <uses-library android:name="android.test.runner" />
        </application>
        <instrumentation
            android:name="android.test.InstrumentationTestRunner"
            android:label="Demo for JUnittest"
            android:targetPackage="com.example.xml" />

    2. Change [com.example.xml] to your current package name

    3. Create a TestXXXService.java based AndroidTestCase.

    11_查看与输出日志信息

    1.Miui输出:Log too more...

    2.

    12_文件的保存与读取

    没有实现界面,直接使用单元測试完毕FileService的測试。ByteArrayOutputStream是第一次接触,攻克了C语言中读文件不便利的问题。

    13_文件的操作模式

    关于MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE。

     

    都在API-17(4.2.2)时给禁用了,能够使用java/io来解决:

        File f = new File(pdfPath);

    file.setReadable(true, false); //使得该文件能够读

    file.setWriteable(true, false); //使得该文件能够写

    getCacheDir()方法获取当前包下的cache文件夹路径。

    getFilesDir()方法获取当前包下的files文件夹路径。

    14_把文件存放在SDCard

    重点:

    1.APK卸载后。这个APKSdcard中创建的文件夹和文件怎样被删除掉呢?

    2.不能使用mContext.openFileOutput();

    3.4.2.2sdcard实质是/data/media/

    4.放到sdcard中的文件能够被其他应用所读取

    16_採用SharedPreferences保存用户偏好设置參数

    样例:

    public class PreferencesService {

        Context mContext = null;

     

        public PreferencesService(Context context) {

            mContext = context;

        }

     

        public void save(String name, Integer age) {

            SharedPreferences mSharedPreferences = mContext.getSharedPreferences("itcast", Context.MODE_PRIVATE);

            Editor mEditor = mSharedPreferences.edit();

            mEditor.putString("name", name);

            mEditor.putInt("age", age);

            mEditor.commit();

        }

        

        public Map<String, String> getPreferences() {

            Map<String, String> params = new HashMap<String, String>();

            SharedPreferences mSharedPreferences = mContext.getSharedPreferences("itcast", Context.MODE_PRIVATE);

            params.put("name", mSharedPreferences.getString("name"""));

            params.put("age", String.valueOf(mSharedPreferences.getInt("age", 0)));

            return params;

        }

     

    }

    17.

    23_网络图片查看器

    依照视频上写出来的代码在4.0上执行出现例如以下异常:

     android.os.NetworkOnMainThreadException

    stackoverflow给出的建议是用AsyncTask,介于AsyncTask还没有学习。决定用新的线程还解决问题:

        /**

         * 下载线程

         */

        Runnable downloadRun = new Runnable() {

            @Override

            public void run() {

                try {

                    String path = image_path.getText().toString();

                    Log.i("FUCK""path:" + path);

                    byte[] data = ImageService.getImage(path);

                    bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

                    // 处理完毕后给handler发送消息

                    Message msg = new Message();

                    msg.what = 1;

                    handler.sendMessage(msg);

                } catch (Exception e) {

                    e.printStackTrace();

                }

            }

        };

        private Handler handler = new Handler() {

            @Override

            public void handleMessage(Message msg) {

                if (msg.what == 1) {

                    imageview.setImageBitmap(bitmap);

                }

            }

        };

    将按键中处理的任务,放到一个新的线程中,按键后启动该线程:

            public void onClick(View v) {

                new Thread(downloadRun).start();

            }

    当然处理图片显示的要放到Handler中显示。

    这样就攻克了这个问题。

    还有因为不关心Java Web开发,所以选择百度的Logo作为攻取图片的路径:

    http://www.baidu.com/img/bdlogo.gif

    显示结果例如以下:

     

    52_发送状态栏通知

    本课过程中,没有遇到错误,只是在发送通知的时候提示用的API已经过时了,所以找了一下新的用法:(參考)

    原始API实现:

    public void send(View v){

        String tickkerText = shorttitileText.getText().toString();

        String title = titleText.getText().toString();

        String content = contentText.getText().toString();

        int icon = android.R.drawable.stat_notify_chat;

        Notification notification = new Notification(icon, tickkerText, System.currentTimeMillis());

        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:194949494"));

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 10, intent, 0);

        notification.setLatestEventInfo(this, title, content, pendingIntent);

        notification.defaults = Notification.DEFAULT_SOUND;

        notification.flags = Notification.FLAG_AUTO_CANCEL;

        

        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        manager.notify(100, notification);

    }

    API实现:

    public void sendNew(View v){

        String tickkerText = shorttitileText.getText().toString();

        String title = titleText.getText().toString();

        String content = contentText.getText().toString();

        int icon = android.R.drawable.stat_notify_chat;

        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:194949494"));

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 10, intent, 0);

        Notification notification = new Notification.Builder(this)

        .setSmallIcon(icon)                   // => Notification         第一个參数

        .setTicker(tickkerText)                 // => Notification         第二个參数

        .setWhen(System.currentTimeMillis())   // => Notification         第三个參数

        .setContentTitle(title)                  // => setLatestEventInfo   第二个參数

        .setContentText(content)               // => setLatestEventInfo   第三个參数

        .setContentIntent(pendingIntent)        // => setLatestEventInfo   第四个參数

        .setDefaults(Notification.DEFAULT_SOUND)    

                              // => notification.defaults = Notification.DEFAULT_SOUND

        .setAutoCancel(true)                      

                              // => notification.flags = Notification.FLAG_AUTO_CANCEL

        .build();

        

        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        manager.notify(100, notification);

    }

    学习OK。


    53_採用网页设计软件界面

    一般键盘的測试程序的界面就是用JS做出来的。网页要比xml布局easy一些。只是仅仅适用简单一个的。比方表格等等。


    54_tween动画

    帮助文档路径:docs/guide/topics/resources/animation-resource.html

    关于translate的文档样例没有错误了。默认会停留在移动结束后的位置。且使用

    animation.setFillAfter(true);

    不管true或者false都无效。

    学习OK。

    55_frame动画的实现

    在没有加入例如以下代码的时候,直接调用drawable.start()动画仍然是能够開始的:

            getMainLooper().myQueue().addIdleHandler(

                    new MessageQueue.IdleHandler() {

                        @Override

                        public boolean queueIdle() {

                            drawable.start();

                            return false;

                        }

                    });

    关于getMainLooper().myQueue().addIdleHandler的详细作用有行研



  • 相关阅读:
    animation动画应用--android游戏开发
    Codeforces Round #203 (Div. 2)
    Codeforces Round #206 (Div. 2)
    Codeforces Round #204 (Div. 2): C
    Codeforces Round #204 (Div. 2): B
    Codeforces Round #204 (Div. 2): A
    Codeforces Round #205 (Div. 2) : D
    Codeforces Round #205 (Div. 2) : C
    Codeforces Round #205 (Div. 2) : B
    Codeforces Round #205 (Div. 2) : A
  • 原文地址:https://www.cnblogs.com/jzssuanfa/p/6817847.html
Copyright © 2011-2022 走看看