1. 程式人生 > >Android應用基礎學習記錄

Android應用基礎學習記錄

應用 ctp 例如 case 推薦 都沒有 變量命名規則 bytearray href

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驅動-解決不能識別的問題》。

2 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的詳細作用有行研



Android應用基礎學習記錄