Intent跳轉方式,intent跳轉

來源:互聯網
上載者:User

Intent跳轉方式,intent跳轉

1.顯跳轉

        a.一個介面就是一個Activity,因此新的介面類要繼承Activity類。且必須要在資訊清單檔中聲明。(主介面已經在資訊清單檔中聲明了)聲明中的android:name="包名.類名",其中的包名可以省略,稱為 ".類名"   但是前面的    .     不能省略
                setContentView(R.layout.activity)   設定介面
        b.在原介面中,建立意圖:Intent intent = new Intent();描述一些資訊
        c.intent.setClass(this,目標類.class);
        d.表時跳轉到哪個介面,這裡知道要跳轉到哪個介面,因此是顯示的。
        e.另一方式:intent.setClassName(包名,類名);包名是資訊清單檔中的那個package:  (this.getPackageName,類的包名.類名)
        f.startActivity(intent);開始意圖
        g.應用情境,在一個應用程式內部中,知道類名,最為常用:setClass(this,SecondActivity.class);   
        h.特點是:耦合性非常高

2.隱式意圖跳轉

        a.應用情境:在不知道某一個Activity的類名包名,並且引用不到時,用隱式意圖。隱式意圖跳轉到其他應用,跨應用跳躍
                跳轉到其他應用的介面
                跳轉到瀏覽器並開啟百度網址
        b.特點:耦合低
        c.原介面:
                Intent intent = new Intent();
                intent.setAction("aa.bb.cc.dd")    //這裡的參數與資訊清單檔中的要一致,並且category要是DEFAULT的,如果是LAUNCHER就會出錯
                startActivity(intent)
        d.在資訊清單檔的第二個Activity中,聲明一個動作action,並添加一個附加資訊category

        e.隱式意圖傳遞參數:在資訊清單檔中,如果有schema   那麼在代碼中也要設定
        f.Uri:統一資源識別項     URL  :統一資源定位器     URL 是Uri的一個子集
        j.原代碼中intent.setData(Uri.parse("abc:123"));
        h.資訊清單檔,注意scheme
        i.設定mimetype:
        j.原代碼中:intent.setType("list/person");    
        k.注意:setDate  setType 不能分開設定,要麼唯寫一個,要麼必須一起設定,如下
        l.intent.setDataAndType(Uri。parse("abc:123",“lsit....”);
        m.如果這麼寫
                setDate();內部會把type=null
                setType();內部會把data=null
                因此二者要合起來寫。

目標Activity:otherActivity.java

```java
package com.example.a0703_intent_external;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class otherActivity extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                TextView tv = new TextView(this);
                tv.setText("other999999");

                setContentView(tv);
        }
}
```
主Activity:MainActivity.java

```java
package com.example.a0703_intent_external;

import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.content.Intent;

public class MainActivity extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
        }

        public void onClick(View v){
                //顯示方式:知道目標Activity的名字
                
                //方式一:
                //參數二:建立Intent的時候就指定跳轉到哪個Activity
//                Intent intent = new Intent(this, otherActivity.class);
//                startActivity(intent);
                
                //方式二:
//                Intent intent = new Intent();
                //該方法作用就是指定跳轉到哪個Activity
//                intent.setClassName(this, "com.example.a0703_intent_external.otherActivity");
//                startActivity(intent);
                
                //隱式方式 不知道目標Activity的名稱,但是知道目標Activity的action和一些附加資訊
                Intent intent = new Intent();
                //設定action
                intent.setAction("android.intent.action.external");
                //intent.setDataAndType(Uri.parse("bbb:123"), "aaa/bbb");
                //設定附加資訊
                intent.setDataAndType(Uri.parse("person:fffff"), "list/person");
                //開始跳轉
                startActivity(intent);
        }

}
```
布局檔案:
        

```java
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="跳轉" />

</RelativeLayout>

other.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Text
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="other " />

</RelativeLayout>

otherActivity.java 需要在資訊清單檔中註冊
        <activity 
                android:name="com.example.a0703_intent_external.otherActivity"
                >
                
                <intent-filter>
                        <action android:name="android.intent.action.external"/>
                        <category android:name="android.intent.category.DEFAULT"/>
                        <data android:scheme="person"/>
                        <data android:mimeType="list/person"/>
                </intent-filter>
                
        </activity>

聯繫我們

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