十、Android學習第九天——小結

來源:互聯網
上載者:User

標籤:android   blog   http   io   os   使用   ar   檔案   資料   

(轉自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html)

十、Android學習第九天——小結

通過這段時間的學習,今晚上來做個小小的總結~~~

最早之前我們就瞭解到,Android四個重要的部分:

一、Activity —— 門面,就是我們看到的使用者介面

二、Intent —— 在整個應用程式間傳送資料

三、Service —— 不可見,為整個應用程式提供一個服務支援

四、Content Provider —— 為應用程式提供資料的介面

至此,這四個重要的部分我們都已經有了初步的認識。但是並不是每一個Android應用程式都需要這四個部分,這不是必需的。某些時候,我們只需要這四種中的幾種組合成我們的應用。

Activity

至此,Activity我們已經很清楚了,需要注意的是Activity的使用,需要在Manifest.xml中進行註冊。

Intent

Android中提供了Intent機制來協助應用間的互動與通訊,Intent負責對應用中一次操作的動作、資料、附加資料進行描述,Android則根據這些Intent的描述,負責找到相應的組件,將Intent傳遞給調用的組件,並完成對組件的調用。

Intent不僅可用於應用程式之間,也可用於應用程式內部的Activity/Service之間的互動。因此,Intent在這裡起的是一個媒體中介的作用,專門提供組件互相調用的相關資訊,實現調用者與被調用者之間的解耦。

理解Intent的關鍵之一是理解Intent的兩種基本用法:

1、顯式的Intent——即在構造Intent對象的時候就指定接受者

2、隱式的Intent——在構造Intent對象時,並不知道也並不關心接受者是誰,有利於降低寄件者與接受者之間的耦合。

以下是Intent幾種常用的用法:


Intent intentOne = new Intent(IntentActivity.this,
ResultActivity.class);
IntentActivity.this.startActivity(intentOne);

------------------------------------------------------------------------


Intent intentTwo = new Intent(IntentActivity.this,
ResultActivity.class);

// 把資料放到Bundle中,進行傳遞
Bundle bundle = new Bundle();
bundle.putString("name", "chenzheng");
// intent.putExtra可以用來放索引值對
intentTwo.putExtras(bundle);
IntentActivity.this.startActivity(intentTwo);

------------------------------------------------------------------------

// ..............對於資料接收................

Bundle resultBundle = getIntent().getExtras();
String name = resultBundle.getString("name");

------------------------------------------------------------------------


Intent resultIntent = getIntent();
Bundle resultBundleTwo = new Bundle();
resultBundleTwo.putString("name", "This is from ShowMsg");
resultIntent.putExtras(resultBundleTwo);
setResult(RESULT_OK, resultIntent);

Service

Service是一個應用程式組件

Service沒有圖形化介面

Service通常用來處理一些耗時比較長的操作,例如下載,播放MP3檔案等操作。

可以使用Service更新ContentProvider,發送Intent以及啟動系統通知等等。

我們要注意,Service不是一個單獨的進程,也不是一個線程。

這裡別忘記寫完一個Service要去Manifest.xml中進行註冊。

ContentProvider

應用程式能夠將它們的資料儲存到檔案中、SQL資料庫中、甚至是任何有效裝置中。當你想將你的資料與其他應用共用時,Content Provider就將變的很有用了。一個Content Provider類實現了一組標準的方法,從而能夠將其他的應用儲存或者讀取此Content Provider處理的各種資料類型。

--------------------------------------------------------------------------

Toast

俺一直對這個Toast很有興趣,所以從網上找了個例子自己練習了下,與大家分享:

下面的例子一共有五種Toast的用法

第一種:預設樣式

// 預設的Toast樣式
Toast.makeText(ToastActivity.this, "預設Toast樣式", Toast.LENGTH_SHORT)
.show();

運行結果如下:

第二種:自訂顯示位置

// 自訂位置的Toast
Toast toast = Toast.makeText(ToastActivity.this, "自訂位置的Toast",
Toast.LENGTH_LONG);
// 自訂Toast的顯示位置
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();

運行結果如下:

第三種:帶圖片的Toast

// 帶圖片的Toast
Toast toast = Toast.makeText(ToastActivity.this, "帶圖片的Toast",
Toast.LENGTH_LONG);
// 設定Toast的顯示位置
toast.setGravity(Gravity.CENTER, 0, 0);

LinearLayout toastView = (LinearLayout) toast.getView();
System.out.println("getApplicationContext:"
+ getApplicationContext());

ImageView imageCodeProject = new ImageView(getApplicationContext());
// 找到需要顯示的圖片檔案
imageCodeProject.setImageResource(R.drawable.mickey);
toastView.addView(imageCodeProject, 0);
toast.show();

運行結果如下:

第四種:完全自訂的Toast

// 完全自訂的Toast,每一個選項都需要我們去手動設定
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom,
(ViewGroup) findViewById(R.id.llToast));
ImageView image = (ImageView) layout
.findViewById(R.id.tvImageToast);
image.setImageResource(R.drawable.mickey);
TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
title.setText("Attention");
TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
text.setText("完全自訂Toast");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

運行結果如下:

第五種:其他線程

// 其他線程的Toast
new Thread(new Runnable() {

@Override
public void run() {
showToast();
}
}).start();

public void showToast() {
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "我來自其他線程!",
Toast.LENGTH_SHORT).show();

}
});
}

運行結果如下:

 

十、Android學習第九天——小結(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.