[Android]Intent 實現頁面跳轉的方式[轉]

來源:互聯網
上載者:User

標籤:

第一種方式,用action來跳轉。

1、使用Action跳轉,如果有一個程式的 AndroidManifest.xml中的某一個Activity的IntentFilter段中定義了包含了相同的Action那麼這個Intent 就與這個目標Action匹配。如果這個IntentFilter段中沒有定義 Type,Category,那麼這個 Activity就匹配了。但是如果手機中有兩個以上的程式匹配,那麼就會彈出一個對話可框來提示說明。

Action的值在Android中有很多預定義,如果你想直接轉到你自己定義的Intent接收者,你可以在接收者的 IntentFilter中加入一個自訂的Action值(同時要設定 Category值為"android.intent.category.DEFAULT"),在你的Intent中設定該值為Intent的 Action,就直接能跳轉到你自己的Intent接收者中。因為這個Action在系統中是唯一的。

2,data/type,你可以用Uri來做為data,比如Uri uri = Uri.parse(http://www.google.com);

Intent i = new Intent(Intent.ACTION_VIEW,uri);手機的Intent分發過程中,會根據http://www.google.com 的scheme判斷出資料類型type

手機的Brower則能匹配它,在Brower的Manifest.xml中的IntenFilter中首先有ACTION_VIEW Action,也能處理http:的type。

3,至於分類Category,一般不要去在Intent中設定它,如果你寫Intent的接收者,就在Manifest.xml的 Activity的 IntentFilter中包含android.category.DEFAULT,這樣所有不設定 Category(Intent.addCategory(String c);)的Intent都會與這個Category匹配。

4,extras(附加資訊),是其它所有附加資訊的集合。使用extras可以為組件提供擴充資訊,比如,如果要執行“寄送電子郵件”這個動作,可以將電子郵件的標題、本文等儲存在extras裡,傳給電子郵件發送組件。

Java代碼 package com.android.edit_text; 

import android.app.Activity; 

import android.content.Intent; 

import android.os.Bundle; 

import android.view.KeyEvent; 

import android.view.View; 

import android.widget.EditText; 

public class MyEditText extends Activity { 

private TextView m_TextView; 

private EditText m_EditText; 

@Override 

public void onCreate(Bundle savedInstanceState) { 

super.onCreate(savedInstanceState); 

setContentView(R.layout.main); 

m_EditText = (EditText) this.findViewById(R.id.EditText01); 

m_EditText.setOnKeyListener(editTextKeyListener); 

private EditText.OnKeyListener editTextKeyListener = new EditText.OnKeyListener() { 

@Override 

public boolean onKey(View arg0, int arg1, KeyEvent arg2) { 

// action跳轉,需要在AndroidManifest.xml中配置action 

Intent i = new Intent("android.intent.action.mydialog"); 

MyEditText.this.startActivity(i); 

return false; 

}; 

複製代碼Xml代碼 <?xml version="1.0" encoding="utf-8"?> 

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 

package="com.android.edit_text" android:versionCode="1" 

android:versionName="1.0"> 

<application android:icon="@drawable/icon" android:label="@string/app_name"> 

<activity android:name=".MyEditText" android:label="@string/app_name"> 

<intent-filter> 

<action android:name="android.intent.action.MAIN" /> 

<category android:name="android.intent.category.LAUNCHER" /> 

</intent-filter> 

</activity> 

<!--配置跳轉activity--> 

<activity android:name="com.android.dialog.MyDialog"> 

<intent-filter> 

<!--配置action路徑--> 

<action android:name="android.intent.action.mydialog" /> 

<category android:name="android.intent.category.DEFAULT" /> 

</intent-filter> 

</activity> 

</application> 

<uses-sdk android:minSdkVersion="7" /> 

</manifest> 

複製代碼第二種方式,用類名跳轉。

Intent負責對應用中一次操作的動作、動作涉及資料、附加資料進行描述,Android則根據此Intent的描述, 負責找到對應的組件,將 Intent傳遞給調用的組件,並完成組件的調用。Intent在這裡起著實現調用者與被調用者之間的解耦作用。

Intent傳遞過程中,要找到目標消費者(另一個Activity,IntentReceiver或Service),也就是Intent的響應者。

Java代碼 package com.Android; 

import android.app.Activity; 

import android.content.Intent; 

import android.os.Bundle; 

import android.view.View; 

import android.view.View.OnClickListener; 

public class FormStuff extends Activity { 

@Override 

public void onCreate(Bundle savedInstanceState) { 

super.onCreate(savedInstanceState); 

setContentView(R.layout.formstuff); 

final ImageButton button = (ImageButton) findViewById(R.id.android_button); 

button.setOnClickListener(new OnClickListener() { 

public void onClick(View v) { 

// 用類名跳轉,需要在AndroidManifest.xml中申明activity 

Intent intent = new Intent(FormStuff.this, HelloTabWidget.class); 

startActivity(intent); 

}); 

複製代碼Xml代碼 <?xml version="1.0" encoding="utf-8"?> 

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 

package="com.Android" android:versionCode="1" android:versionName="1.0"> 

<application android:icon="@drawable/icon" android:theme="@android:style/Theme.NoTitleBar"> 

<activity android:name=".FormStuff" android:label="@string/app_name"> 

<intent-filter> 

<action android:name="android.intent.action.MAIN" /> 

<category android:name="android.intent.category.LAUNCHER" /> 

</intent-filter> 

</activity> 

<!--申明activity--> 

<activity android:name="HelloTabWidget"></activity> 

</application> 

<uses-sdk android:minSdkVersion="4" /> 

</manifest> 

複製代碼一些Intent的常用發:

Java代碼 顯示網頁 

1. Uri uri = Uri.parse("http://google.com"); 

2. Intent it = new Intent(Intent.ACTION_VIEW, uri); 

3. startActivity(it); 

顯示地圖 

1. Uri uri = Uri.parse("geo:38.899533,-77.036476"); 

2. Intent it = new Intent(Intent.ACTION_VIEW, uri); 

3. startActivity(it); 

4. //其他 geo URI 範例 

5. //geo:latitude,longitude 

6. //geo:latitude,longitude?z=zoom 

7. //geo:0,0?q=my+street+address 

8. //geo:0,0?q=business+near+city 

9. //google.streetview:cbll=lat,lng&amp;cbp=1,yaw,,pitch,zoom&amp;mz=mapZoom  

路徑規劃 

1. Uri uri = Uri.parse("http://maps.google.com/maps?f=d&amp;saddr=startLat startLng&amp;daddr=endLat endLng&amp;hl=en"); 

2. Intent it = new Intent(Intent.ACTION_VIEW, uri); 

3. startActivity(it); 

4. //where startLat, startLng, endLat, endLng are a long with 6 decimals like: 50.123456 

打電話 

1. //叫出撥號程式

2. Uri uri = Uri.parse("tel:0800000123"); 

3. Intent it = new Intent(Intent.ACTION_DIAL, uri); 

4. startActivity(it); 

1. //直接打電話出去 

2. Uri uri = Uri.parse("tel:0800000123"); 

3. Intent it = new Intent(Intent.ACTION_CALL, uri); 

4. startActivity(it); 

5. //用這個,要在 AndroidManifest.xml 中,加上 

6. //&lt;uses-permission id="android.permission.CALL_PHONE" /&gt; 

傳送SMS/MMS 

1. //調用簡訊程式

2. Intent it = new Intent(Intent.ACTION_VIEW, uri); 

3. it.putExtra("sms_body", "The SMS text"); 

4. it.setType("vnd.android-dir/mms-sms"); 

5. startActivity(it);  

1. //傳送訊息  

2. Uri uri = Uri.parse("smsto://0800000123"); 

3. Intent it = new Intent(Intent.ACTION_SENDTO, uri); 

4. it.putExtra("sms_body", "The SMS text"); 

5. startActivity(it);  

1. //傳送 MMS 

2. Uri uri = Uri.parse("content://media/external/images/media/23"); 

3. Intent it = new Intent(Intent.ACTION_SEND); 

4. it.putExtra("sms_body", "some text"); 

5. it.putExtra(Intent.EXTRA_STREAM, uri); 

6. it.setType("image/png"); 

7. startActivity(it); 

傳送 Email 

1. Uri uri = Uri.parse("mailto:[email protected]"); 

2. Intent it = new Intent(Intent.ACTION_SENDTO, uri); 

3. startActivity(it); 

1. Intent it = new Intent(Intent.ACTION_SEND); 

2. it.putExtra(Intent.EXTRA_EMAIL, "[email protected]"); 

3. it.putExtra(Intent.EXTRA_TEXT, "The email body text"); 

4. it.setType("text/plain"); 

5. startActivity(Intent.createChooser(it, "Choose Email Client")); 

1. Intent it=new Intent(Intent.ACTION_SEND);  

2. String[] tos={"[email protected]"};  

3. String[] ccs={"[email protected]"};  

4. it.putExtra(Intent.EXTRA_EMAIL, tos);  

5. it.putExtra(Intent.EXTRA_CC, ccs);  

6. it.putExtra(Intent.EXTRA_TEXT, "The email body text");  

7. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");  

8. it.setType("message/rfc822");  

9. startActivity(Intent.createChooser(it, "Choose Email Client"));  

1. //傳送附件 

2. Intent it = new Intent(Intent.ACTION_SEND); 

3. it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text"); 

4. it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3"); 

5. sendIntent.setType("audio/mp3"); 

6. startActivity(Intent.createChooser(it, "Choose Email Client"));  

播放多媒體 

Uri uri = Uri.parse("file:///sdcard/song.mp3"); 

Intent it = new Intent(Intent.ACTION_VIEW, uri); 

it.setType("audio/mp3"); 

startActivity(it);  

Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1"); 

Intent it = new Intent(Intent.ACTION_VIEW, uri); 

startActivity(it);  

Market 相關 

1.   //尋找某個應用  

2.   Uri uri = Uri.parse("market://search?q=pname:pkg_name");  

3.   Intent it = new Intent(Intent.ACTION_VIEW, uri); 

4.   startActivity(it); 

5.   //where pkg_name is the full package path for an application  

1.   //顯示某個應用的相關資訊  

2.   Uri uri = Uri.parse("market://details?id=app_id"); 

3.   Intent it = new Intent(Intent.ACTION_VIEW, uri);  

4.   startActivity(it); 

5.   //where app_id is the application ID, find the ID 

6.   //by clicking on your application on Market home 

7.   //page, and notice the ID from the address bar  

1.   Uri uri = Uri.fromParts("package", strPackageName, null);  

2.   Intent it = new Intent(Intent.ACTION_DELETE, uri); 

3.   startActivity(it);

[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.