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%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。