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 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 美女露胸视频网站| 日韩精品中文字幕在线| 亚洲视频www| 成人欧美一区二区三区的电影| 日本免费人成在线网站| 国产人伦视频在线观看| 日韩欧美高清在线| 青青青青青青久久久免费观看| 九九久久99综合一区二区| 国产精品久久久久免费视频| 欧美办公室系列观看丝袜| 亚洲人成网站看在线播放| 久久精品国产精品国产精品污| 国产成人久久一区二区三区 | 天天av天天翘天天综合网| 浪潮AV色综合久久天堂 | 国产69精品久久久久777| 成年性生交大片免费看| 精品亚洲成a人片在线观看| a级在线观看免费| 亚洲欧美另类一区| 国产无套粉嫩白浆在线| 毛片网在线观看| 国产麻豆91网在线看| 久久久久久久国产a∨| 国产欧美日韩一区二区加勒比| 无限看片在线版免费视频大全 | 国产香蕉在线精彩视频| 中文字幕亚洲第一| 亚洲欧美激情在线| 国产在线无码视频一区| 妓女嫖客叫床粗话对白| 欧美大交乱xxxx| 精品国产欧美一区二区| 91人成在线观看网站| 么公的好大好深视频好爽想要| 又黄又爽又色又刺激的视频| 国产精品女人在线观看| 成人午夜视频网站| 欧美xxxxbbb| 精品99在线观看|