編輯:關於Android編程
前面的博文介紹了Activity,可能學的還不太爽哦,所以接下來學習android的Intent,它是android的信使,可以進行各個組件之間進行通信了。我依然在前面的基礎上進行修改開發,免的啰嗦過多的東西。沒看過前面的博文的,可以先看android學習二(Activity),好了,接下來開始吧。
使用顯示的Intent
1.在前面的ActivityTest基礎上,我們在創建一個布局文件,文件名為second_layout.xml,代碼如下:
package com.wj.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.second_layout);
}
}
3.進行注冊活動,在AndroidManifest.xml中進行活動的注冊,代碼如下:
button1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/* Toast.makeText(FirstActivity.this, "you clicked button1",
Toast.LENGTH_SHORT).show();*/
/*
* Intent的使用分為顯示的Intent和隱式的Intent,下面的是顯示的Intent
* Intent有多個構造函數,我使用的是一個簡單的的,第一個參數Context要求提供一個啟動
* 活動的上下文,第二個參數Class則是指定想要啟動的目標活動,通過這個構造函數構建出Intent
* 的“意圖”,然後使用startActivity(intent);啟動目標活動。
* Intent是android程序中個組件之間進行交互的一種重要的方式,它不緊可以指明當前組想要執行
* 的動作,還可以在不同組件之間傳遞數據。Intent一般可被用於啟動活動,服務,以及發送廣播等。
* */
Intent intent=new Intent(FirstActivity.this,
SecondActivity.class);
startActivity(intent);
}
});
5.重寫運行程序,點擊button1,將跳轉到SecondActivity

運行結果如上圖所示,上面的就是顯示的進行Intent顯示的使用,應為指明了將跳轉到哪一個類。下面我們在說說,怎麼使用隱式的使用。<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGJyPgo8L3A+CjxwPjxicj4KPC9wPgo8cD48YnI+CjwvcD4KPHA+yrnTw9L+yr21xEludGVudDwvcD4KPHA+ICAgICAgz+CxyNPrz9TKvrXESW50ZW50o6zS/sq9tcRJbnRlbnS6rNDuwcu63Lbgo6zL/LKisrvWuMP3ztLDx8/r0qrG9LavxMTSu7j2u+62r6OstvjKx9a4tqjBy9K7z7XB0LXEYWNpdG9uus1jYXRlZ29yebXI0MXPoqOsyLu6872708nPtc2zyKW31s721eK49mludGVudKOssqKw787Sw8fV0rP2us/KyrXEu+62r8ilxvS2r6GjPC9wPgo8cD4xLtTayc/D5rXEu/m0ocnPztLDx9DeuMSzydL+yr21xEludGVudKOsytfPyLjE0LRBbmRyb2lkTWFuaWZlc3QueG1swO/D5rXEPGFjdGl2aXR5IGFuZHJvaWQ6bmFtZT0=".SecondActivity">,為該注冊的活動,進行intent-filter的配置,配置的內容如下:
注意 裡面的android:name,是可以自己定義的,指不過在創建Intent的時候要保證和這裡的字符串一致就ok了。在aciton標簽中我們指明了當前活動可以響應com.wj.test.activitytest.ACTION_START這個action,而2.修改FirstActivity中按鈕的點擊事件,代碼如下:
button1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/* Toast.makeText(FirstActivity.this, "you clicked button1",
Toast.LENGTH_SHORT).show();*/
/*
* Intent的使用分為顯示的Intent和隱式的Intent,下面的是顯示的Intent
* Intent有多個構造函數,我使用的是一個簡單的的,第一個參數Context要求提供一個啟動
* 活動的上下文,第二個參數Class則是指定想要啟動的目標活動,通過這個構造函數構建出Intent
* 的“意圖”,然後使用startActivity(intent);啟動目標活動。
* Intent是android程序中個組件之間進行交互的一種重要的方式,它不緊可以指明當前組想要執行
* 的動作,還可以在不同組件之間傳遞數據。Intent一般可被用於啟動活動,服務,以及發送廣播等。
* */
/*Intent intent=new Intent(FirstActivity.this,
SecondActivity.class);
startActivity(intent);*/
//使用隱式的Intent
Intent intent=new Intent("com.wj.test.activitytest.ACTION_START");
startActivity(intent);
}
});重寫運行程序,將出現和顯示的Intent一樣的效果。
注意:每個Intent中只能指定一個action,但卻能指定多個category。下面我們在添加一個category吧。
3.修改FirstActivity中按鈕的點擊事件,代碼如下:
button1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/* Toast.makeText(FirstActivity.this, "you clicked button1",
Toast.LENGTH_SHORT).show();*/
/*
* Intent的使用分為顯示的Intent和隱式的Intent,下面的是顯示的Intent
* Intent有多個構造函數,我使用的是一個簡單的的,第一個參數Context要求提供一個啟動
* 活動的上下文,第二個參數Class則是指定想要啟動的目標活動,通過這個構造函數構建出Intent
* 的“意圖”,然後使用startActivity(intent);啟動目標活動。
* Intent是android程序中個組件之間進行交互的一種重要的方式,它不緊可以指明當前組想要執行
* 的動作,還可以在不同組件之間傳遞數據。Intent一般可被用於啟動活動,服務,以及發送廣播等。
* */
/*Intent intent=new Intent(FirstActivity.this,
SecondActivity.class);
startActivity(intent);*/
//使用隱式的Intent
Intent intent=new Intent("com.wj.test.activitytest.ACTION_START");
intent.addCategory("com.wj.activitytest.MY_CATEGORY");
startActivity(intent);
}
});4.修改後你在運行一下程序,就會發現程序崩潰了,看錯誤信息,你會發現10-27 01:03:47.937: E/AndroidRuntime(2824): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.wj.test.activitytest.ACTION_START
cat=[com.wj.activitytest.MY_CATEGORY] },提示我們沒有任何活動可以響應我們的Intent,這是因為我們添加了intent.addCategory("com.wj.activitytest.MY_CATEGORY");,而SecondActivity的
更多隱式Intent的用法
打開一個指定的頁面的浏覽器
1.修改FirstActivity中按鈕點擊事件的代碼,主要代碼如下
button1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/* Toast.makeText(FirstActivity.this, "you clicked button1",
Toast.LENGTH_SHORT).show();*/
/*
* Intent的使用分為顯示的Intent和隱式的Intent,下面的是顯示的Intent
* Intent有多個構造函數,我使用的是一個簡單的的,第一個參數Context要求提供一個啟動
* 活動的上下文,第二個參數Class則是指定想要啟動的目標活動,通過這個構造函數構建出Intent
* 的“意圖”,然後使用startActivity(intent);啟動目標活動。
* Intent是android程序中個組件之間進行交互的一種重要的方式,它不緊可以指明當前組想要執行
* 的動作,還可以在不同組件之間傳遞數據。Intent一般可被用於啟動活動,服務,以及發送廣播等。
* */
/*Intent intent=new Intent(FirstActivity.this,
SecondActivity.class);
startActivity(intent);*/
//使用隱式的Intent
/*Intent intent=new Intent("com.wj.test.activitytest.ACTION_START");
intent.addCategory("com.wj.activitytest.MY_CATEGORY");
startActivity(intent);*/
//更多隱式的Intent的用法
//打開浏覽器
/*
* 這裡我們指定了Intent的action是Intent.ACTION_VIEW,這是一個android系統內置的動作,
* 其常量為android.intent.action.VIEW,然後通過Uri.parse方法,將一個網址字符串解析成一個
* Uri對象,在調用Intent的setData()方法將這個對象傳遞進去。
* */
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent);
}
});android:scheme用於指定數據的協議部分,如上列中的http部分
android:host用於指定數據的主機名部分,如上列中的www.baidu.com
android:port用於指定數據的端口不,一般緊隨在主機名之後
android:path用於指定主機名和端口之後的部分,如一段網址中跟在域名之後的內容
android:mineType用於指定可以處理的數據類型,允許使用通配符的方式進行指定。
只有標簽中指定的內容和Intent中攜帶的Data完全一致時,當前活動才能夠響應該Intent。
下面建立一個活動,讓它也能響應打開網頁的Intent。
新建一個布局文件名為third_layout.xml代碼如下:
package com.wj.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class ThirdActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.third_layout);
}
}
注冊activity

選擇AndroidTest繼續運行結果如下:

下面使用Intent實現調用撥打電話的功能
修改button1的監聽程序,代碼如下:
button1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/* Toast.makeText(FirstActivity.this, "you clicked button1",
Toast.LENGTH_SHORT).show();*/
/*
* Intent的使用分為顯示的Intent和隱式的Intent,下面的是顯示的Intent
* Intent有多個構造函數,我使用的是一個簡單的的,第一個參數Context要求提供一個啟動
* 活動的上下文,第二個參數Class則是指定想要啟動的目標活動,通過這個構造函數構建出Intent
* 的“意圖”,然後使用startActivity(intent);啟動目標活動。
* Intent是android程序中個組件之間進行交互的一種重要的方式,它不緊可以指明當前組想要執行
* 的動作,還可以在不同組件之間傳遞數據。Intent一般可被用於啟動活動,服務,以及發送廣播等。
* */
/*Intent intent=new Intent(FirstActivity.this,
SecondActivity.class);
startActivity(intent);*/
//使用隱式的Intent
/*Intent intent=new Intent("com.wj.test.activitytest.ACTION_START");
intent.addCategory("com.wj.activitytest.MY_CATEGORY");
startActivity(intent);*/
//更多隱式的Intent的用法
//打開浏覽器
/*
* 這裡我們指定了Intent的action是Intent.ACTION_VIEW,這是一個android系統內置的動作,
* 其常量為android.intent.action.VIEW,然後通過Uri.parse方法,將一個網址字符串解析成一個
* Uri對象,在調用Intent的setData()方法將這個對象傳遞進去。
* */
/*Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent);*/
//實現撥打電話
Intent intent=new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:10086"));
startActivity(intent);
}
});
下面我將講解怎麼使用Intent在活動中傳遞數據
向下一個活動傳遞數據
1.修改button1的監聽按鈕代碼
//向下一個活動傳遞數據
/*
* Intent中提供了一系列putExtra()方法的重載,可以把我們想要傳遞的數據暫存在Intent中
* ,啟動了另一個活動後,只需把這些數據再從Intent中取出就可以了。
* */
String data="Hello SecondActivity";
Intent intent =new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("extra_data", data);
startActivity(intent);
2.修改SecondActivity中將傳遞的數據取出,並打印出來,代碼如下所示:
package com.wj.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.second_layout);
Intent intent=getIntent();
String data =intent.getStringExtra("extra_data");
Log.d("SecondActivity", data);
}
}
通過getIntent()方法獲取到用於啟動SecondeActivity的Intent,然後調用getStringExtra()方法,傳入相應的鍵值,就可以得到傳遞的數據了。
運行程序可以看到如下輸出Hello SecondActivity
返回數據給上一個活動:
1.修改button1監聽器的代碼:
button1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/* Toast.makeText(FirstActivity.this, "you clicked button1",
Toast.LENGTH_SHORT).show();*/
/*
* Intent的使用分為顯示的Intent和隱式的Intent,下面的是顯示的Intent
* Intent有多個構造函數,我使用的是一個簡單的的,第一個參數Context要求提供一個啟動
* 活動的上下文,第二個參數Class則是指定想要啟動的目標活動,通過這個構造函數構建出Intent
* 的“意圖”,然後使用startActivity(intent);啟動目標活動。
* Intent是android程序中個組件之間進行交互的一種重要的方式,它不緊可以指明當前組想要執行
* 的動作,還可以在不同組件之間傳遞數據。Intent一般可被用於啟動活動,服務,以及發送廣播等。
* */
/*Intent intent=new Intent(FirstActivity.this,
SecondActivity.class);
startActivity(intent);*/
//使用隱式的Intent
/*Intent intent=new Intent("com.wj.test.activitytest.ACTION_START");
intent.addCategory("com.wj.activitytest.MY_CATEGORY");
startActivity(intent);*/
//更多隱式的Intent的用法
//打開浏覽器
/*
* 這裡我們指定了Intent的action是Intent.ACTION_VIEW,這是一個android系統內置的動作,
* 其常量為android.intent.action.VIEW,然後通過Uri.parse方法,將一個網址字符串解析成一個
* Uri對象,在調用Intent的setData()方法將這個對象傳遞進去。
* */
/*Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent);*/
//實現撥打電話
/*Intent intent=new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:10086"));
startActivity(intent);*/
//向下一個活動傳遞數據
/*
* Intent中提供了一系列putExtra()方法的重載,可以把我們想要傳遞的數據暫存在Intent中
* ,啟動了另一個活動後,只需把這些數據再從Intent中取出就可以了。
* */
/*String data="Hello SecondActivity";
Intent intent =new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("extra_data", data);
startActivity(intent);*/
//返回數據給上一個活動
Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
/*
* startActivityForResult方法接收2個參數,一個參數是Intent,第二個參數是請求碼
* ,用於在之後的的回調中判斷數據的來源
* */
startActivityForResult(intent, 1);
}
});2.給button2進行監聽
package com.wj.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.second_layout);
/*Intent intent=getIntent();
String data =intent.getStringExtra("extra_data");
Log.d("SecondActivity", data);*/
//獲得按鈕組件
Button button2=(Button) findViewById(R.id.button_2);
button2.setOnClickListener(new OnClickListener(){//進行監聽
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.putExtra("data_return", "Hello FirstActivity");
//setResult()方法接收兩個參數,第一個參數用於向上一個活動返回處理結果,一般使用
//RESULT_OK or RESULT_CANCELED,第二個參數則是把帶有數據的Intent傳遞回去,然後調用
//finish方法來銷毀當前活動
setResult(RESULT_OK,intent);
finish();//銷毀當前活動
}
});
}
}
3.重寫FirstActivity的回調方法
//重寫onActivityResult方法獲取返回的結果數據
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
switch(requestCode){
case 1:
if(resultCode==RESULT_OK){
String returnedData=data.getStringExtra("data_return");
Log.d("FirstActivity", returnedData);
}
break;
default:break;
}
}運行程序點擊button1,然後點擊button2,觀看log的輸出你就會發現輸出了
10-28 12:09:09.396: D/FirstActivity(3755): Hello FirstActivity
注意在一個活動中調用startActivityForResult方法去啟動不同的活動,沒一個活動返回的數據都會調用onActivityResult這個方法。
如果你不想點擊button2的按鈕進行返回,你可以點擊back鍵進行返回只要重寫SecondActivity的onBackPressed()方法就行了,代碼如下:
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.putExtra("data_return", "Hello FirstActivity");
//setResult()方法接收兩個參數,第一個參數用於向上一個活動返回處理結果,一般使用
//RESULT_OK or RESULT_CANCELED,第二個參數則是把帶有數據的Intent傳遞回去,然後調用
//finish方法來銷毀當前活動
setResult(RESULT_OK,intent);
finish();//銷毀當前活動
}
轉載請注明:http://blog.csdn.net/j903829182/article/details/40502495
Retrofit Token過期自動刷新並重新請求接口
在有心課堂的群裡,有網友提出如下場景:當前開發的 App 遇到一個問題:當請求某個接口時,由於 token 已經失效,所以接口會報錯。但是產品經理希望 app 能夠馬上刷
細說JVM系列:JVM內存空間分區
java虛擬機基本結構:JVM是一個內存中的虛擬機,那它的存儲就是內存了,我們寫的所有類、常量、變量、方法都在內存中,因此明白java虛擬機的內存分配非常重要,本部分主要
實例探究Android應用編寫時Fragment的生命周期問題
管理fragment的生命周期有些像管理activity的生命周期。Fragment可以生存在三種狀態:Resumed:Fragment在一個運行中的activity中並
安卓動態調試七種武器之離別鉤 – Hooking(下)
0x00 序隨著移動安全越來越火,各種調試工具也都層出不窮,但因為環境和需求的不同,並沒有工具是萬能的。另外工具是死的,人是活的,如果能搞懂工具的原理再結合上自身的經驗,