Android中Intent的顯示和隱式使用

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   io   os   ar   使用   

Android應用程式中組件之間的通訊都少不了Intent的使用,Intent負責對應用中一次操作的動作、動作涉及資料、附加資料進行描述,Android則根據此Intent的描述,負責找到對應的組件,將 Intent傳遞給調用的組件,並完成組件的調用。intent就是意圖的意思。Intent分兩種:顯式(Explicit intent)和隱式(Implicit intent)。

顯示調用Intent

簡單的Demo從一個Activity轉到另外一個Aactivity:

Mainactivity的布局檔案

    <EditText        android:id="@+id/edt_content"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <Button        android:id="@+id/btn_login"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/edt_content"        android:onClick="login"        android:text="查詢" />

 

 Mainactivity中調用點擊事件:

EditText  contentEditText=(EditText) findViewById(R.id.edt_content);    Intent intent=new Intent(this,PersonActivity.class);    intent.putExtra(EXTRA,contentEditText.getText().toString());    startActivity(intent);

  這個時候的Intent就是顯示調用,直接指定了接收參數的Activity,可以唯一確定一個Activity,意圖特別明確,這個時候需要在PersonActivity接收參數:

public class PersonActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_person);TextView  textView=(TextView) findViewById(R.id.txt_content);Intent intent=getIntent();String  str=intent.getStringExtra(MainActivity.EXTRA);textView.setText(str);textView.setTextSize(20);textView.setTextColor(Color.RED);}

另外這個時候傳遞的參數使用的方法是putExtra,如果傳遞的參數比較多可以使用Bundle類似於map。

隱式調用

隱式,即不是像顯式的那樣直接指定需要調用的Activity,隱式不明確指定啟動哪個Activity,而是設定Action、Data、Category,讓系統來篩選出合適的Activity。篩選是根據所有的<intent-filter>來篩選。

這個時候需要在AndroidManifest.xml中設定一下intent-filter中去設定一下,如下,Category直接使用預設的就行:

    <intent-filter>                <action android:name="com.example.googleone.Peson" />                <category android:name="android.intent.category.DEFAULT" />            </intent-filter>

 Mainactivity中的調用使用,這個時候的調用:

  Intent intent=new  Intent("com.example.googleone.Peson");   startActivity(intent);

 這個自己定義的Action字串可以調用自身程式的Activity,還可以其他應用程式的Action,比如說常用的撥號面板:

Intent intent = new Intent(Intent.ACTION_DIAL);    startActivity(intent);

如果這個時候在AndroidManifest.xml檔案中給PersonActivity, 加一個Action,如下:

  <activity            android:name=".PersonActivity"            android:label="@string/title_activity_person" >            <intent-filter>                <action android:name="android.intent.action.DIAL"/>                  <action android:name="com.example.googleone.Peson" />                <category android:name="android.intent.category.DEFAULT" />            </intent-filter>        </activity>

Mainactivity中的調用:

Intent intent=new Intent(Intent.ACTION_DIAL);    if(intent.resolveActivity(getPackageManager()) == null)    {    view.setEnabled(false);    }    try {startActivity(intent);} catch (ActivityNotFoundException e) {// TODO Auto-generated catch blockToast.makeText(this,"找不到對應的Activity",Toast.LENGTH_SHORT).show();}

結果:

  

 Intent.ACTION_DIAL是系統常量字串,等價於android.intent.action.DIAL,調用的時候通過這個action的名稱,去尋找具有這個action的activity~

Android中Intent的顯示和隱式使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.