Android Intent教程含代碼

    Android Intent教程

    Android Intent 是在組件之間傳遞的消息,例如活動、內容提供者、廣播接收器、服務等。愛掏網 - it200.com

    通常與startActivity()方法一起使用,用于調用活動、廣播接收器等。愛掏網 - it200.com

    intent的字典意義 是意圖或目的。愛掏網 - it200.com因此,可以將其描述為執行操作的意圖。愛掏網 - it200.com

    LabeledIntent是android.content.Intent類的子類。愛掏網 - it200.com

    Android意圖主要用于:

    • 啟動服務
    • 啟動活動
    • 顯示網頁
    • 顯示聯系人列表
    • 廣播消息
    • 撥打電話等

    Android有兩種類型的意圖:隱式意圖和顯示意圖。愛掏網 - it200.com

    1) 隱式意圖

    隱式意圖 不指定組件。愛掏網 - it200.com在這種情況下,意圖提供了系統提供的可調用組件的信息。愛掏網 - it200.com

    例如,您可以編寫以下代碼來查看網頁。愛掏網 - it200.com

    Intent intent=new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse("http://www.javatpoint.com"));
    startActivity(intent);
    

    2) 明確意圖

    明確意圖 指定了要調用的組件。愛掏網 - it200.com在這種情況下,意圖提供了要調用的外部類。愛掏網 - it200.com

    Intent i = new Intent(getApplicationContext(), ActivityTwo.class);
    startActivity(i);
    

    要獲取顯示顯式意圖的完整代碼,請訪問下一頁。愛掏網 - it200.com

    Android隱式意圖示例

    讓我們看一個簡單的隱式意圖示例,顯示一個網頁。愛掏網 - it200.com

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>  
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        xmlns:app="http://schemas.android.com/apk/res-auto"  
        xmlns:tools="http://schemas.android.com/tools"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        tools:context="example.javatpoint.com.implicitintent.MainActivity">  
    
        <EditText  
            android:id="@+id/editText"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_marginEnd="8dp"  
            android:layout_marginStart="8dp"  
            android:layout_marginTop="60dp"  
            android:ems="10"  
            app:layout_constraintEnd_toEndOf="parent"  
            app:layout_constraintHorizontal_bias="0.575"  
            app:layout_constraintStart_toStartOf="parent"  
            app:layout_constraintTop_toTopOf="parent" />  
    
        <Button  
            android:id="@+id/button"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:layout_marginRight="8dp"  
            android:layout_marginLeft="156dp"  
            android:layout_marginTop="172dp"  
            android:text="Visit"  
            app:layout_constraintEnd_toEndOf="parent"  
            app:layout_constraintHorizontal_bias="0.0"  
            app:layout_constraintStart_toStartOf="parent"  
            app:layout_constraintTop_toBottomOf="@+id/editText" />  
    </android.support.constraint.ConstraintLayout>
    

    Activity類

    package example.javatpoint.com.implicitintent;
    
    import android.content.Intent;
    import android.net.Uri;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    
    public class MainActivity extends AppCompatActivity {
    
        Button button;
        EditText editText;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            button = findViewById(R.id.button);
            editText =  findViewById(R.id.editText);
    
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String url=editText.getText().toString();
                    Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(intent);
                }
            });
        }
    }
    

    輸出:

    聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。
    發表評論
    更多 網友評論0 條評論)
    暫無評論

    返回頂部

    主站蜘蛛池模板: 久久久老熟女一区二区三区| 日产精品久久久一区二区| 久久亚洲中文字幕精品一区| 在线播放国产一区二区三区 | 3d动漫精品成人一区二区三| 国产婷婷一区二区三区| 国产视频一区在线播放| 亚洲字幕AV一区二区三区四区| 国产午夜精品一区二区三区漫画| 韩国精品一区视频在线播放| 久久精品免费一区二区| 无码国产精品一区二区免费模式| 中文字幕一区二区三区永久| 无码AV动漫精品一区二区免费| 日本一区二区三区日本免费| 亚洲国产成人久久一区二区三区| 一区二区三区精品| 亚洲一区二区三区不卡在线播放| 亲子乱AV视频一区二区| 色婷婷AV一区二区三区浪潮| 成人区人妻精品一区二区不卡网站| 日本韩国黄色一区二区三区| 午夜精品一区二区三区在线观看| 免费高清av一区二区三区| 中文激情在线一区二区| 无码aⅴ精品一区二区三区 | 综合久久一区二区三区| 岛国精品一区免费视频在线观看 | 农村人乱弄一区二区 | 成人精品视频一区二区三区不卡| jizz免费一区二区三区| 在线观看免费视频一区| 久草新视频一区二区三区| 无码日韩精品一区二区三区免费 | 国产精品一区电影| 一区二区三区在线| 亚洲日韩激情无码一区| 国产精品一区二区久久| 精品国产一区在线观看| 精品伦精品一区二区三区视频| 99久久精品国产高清一区二区 |