Main:
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class mainActivity extends Activity { Button nextbu; EditText ip; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); nextbu = (Button) findViewById( R.id.change_layout ); ip = (EditText) findViewById( R.id.ip ); nextbu.setOnClickListener(click_next); } private OnClickListener click_next = new OnClickListener() { public void onClick(View v) { nextActivity(); } }; public void nextActivity(){ //建立一個Bundle Bundle bundle = new Bundle(); //將EditText(ip)的值傳入Bundle裡並命名為ip bundle.putString("ip", ip.getText().toString()); //建立一個intent Intent intent = new Intent(); //設定下一個Activity intent.setClass(mainActivity.this, sec.class); //將bundle傳入 intent.putExtras(bundle); //開啟Activity startActivity(intent); } }
second:
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class secondActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second); TextView con = (TextView) findViewById(R.id.con); Button back = (Button) findViewById(R.id.backButton); //取得intent裡的bundle Bundle bundle = getIntent().getExtras(); //將bundle的值取出來並寫到TextView(con)裡 con.setText(bundle.getString("ip")); back.setOnClickListener(new Button.OnClickListener(){ public void onClick(View view){ //結束此Activity finish(); } }); } }
注意:記得在AndroidManifest.xml註冊另一個Activity
<activity android:name=".SecondActivity" ></activity>
Intent還有其他簡易的用法
在Intent中,指定要應用程式執行的動作:ACTION_VIEW、ACTION_EDIT、ACTION_DIAL、ACTION_CALL等等(Action就是Android內建的軟體元件)
超連結網址
Uri uri = Uri.parse("http://google.com"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent);
切換到撥打電話畫面(已輸入號碼)
Uri uri = Uri.parse("tel:12345678"); Intent intent = new Intent(Intent.ACTION_DIAL, uri); startActivity(intent);
撥打電話
Manifest需先加入權限
<uses-permission android:name="android.permission.CALL_PHONE"/>
Uri uri = Uri.parse("tel:12345678"); Intent intent = new Intent(Intent.ACTION_CALL, uri); startActivity(intent);
開啟通訊錄
Intent intent = new Intent(Intent.ACTION_VIEW, People.CONTENT_URI); startActivity(intent);
呼叫另外的Activity,再將值傳回(例如透過BarCode Scanner分析BarCode再將值傳回原本的Activity)
FirstActivity
Bundle bundle = new Bundle(); bundle.putString("test", "Hello"); Intent intent = new Intent(); //設定下一個Actitity intent.setClass(FirstActivity.this, SecondActivity.class); intent.putExtras(bundle); //使用startActivityForResult等待回傳,1為自行定義的成功回傳的回傳值 startActivityForResult(intent, 1);
另外FirstActivity在收到回傳的intent之後,會直接傳入onActivityResult方法
public void onActivityResult(int requestCode, int resultCode, Intent intent) { //requestCode在startActivityForResult傳入參數時決定的,如果成功的話會傳回相同的值 if (requestCode == 1) { //成功回傳值 if (resultCode == RESULT_OK) { //取得回傳過來的值 String contents = intent.getStringExtra("test"); //做其他事 } else if (resultCode == RESULT_CANCELED) { //當還沒完成動作時,使用者倒退回上一步會判定為cancel } } }
SecondActivity
//取得FitstActivity傳過來的Bundle物件 Bundle bundle = getIntent().getExtras(); //新增新的intent, Intent i=new Intent(); Bundle b=new Bundle(); //將前一個傳過來的值加入HAHAHA字串 b.putString("test", bundle.getString("test") + "HAHAHA"); i.putExtras(b); //回傳參數和intent(intent部份也可以不用回傳,回傳RESULT_OK確定完成即可) setResult(RESULT_OK,i); //結束Activity finish();
2 Comments
[Android]使用Intent在Activity間傳值| 佛祖球球 - 熱點新聞網 · 12 10 月, 2013 at 4:08 下午
[…] http://blog.johnsonlu.org/android%E4%BD%BF%E7%94%A8intent%E5%9C%A8activity%E9%96%93%E5%82%B3%E5%80%B… […]
[Android]使用Intent在Activity間傳值| 佛祖球球 - 熱點新聞網 · 12 10 月, 2013 at 4:08 下午
[…] http://blog.johnsonlu.org/android%E4%BD%BF%E7%94%A8intent%E5%9C%A8activity%E9%96%93%E5%82%B3%E5%80%B… […]
Comments are closed.