intent的作用就类似于快递员,在Activity之间传送信息。
intent将联系人的信息带到了“联系人详情页”中
intent工作流程:
intent的基本作用:
- btn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent();
- ComponentName componentName = new ComponentName("com.example.intent",
- "com.example.intent.DetailActivity");
- intent.setComponent(componentName);
- startActivity(intent);
- }
- });
左边是Action,右边是Data。
第一行的意思是打开编辑联系人第一条的信息
第二行的意思是拨打电话10086
第三行的意思是打开mingribook.com网站
- package com.example.actiondata;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.content.Intent;
- import android.media.Image;
- import android.net.Uri;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.ImageView;
-
- public class MainActivity extends AppCompatActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- ImageView phone = findViewById(R.id.phone);
- ImageView email = findViewById(R.id.email);
-
- View.OnClickListener cl = new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- ImageView imageView = (ImageView)v;
- Intent intent = new Intent();
- switch(imageView.getId()) {
- case R.id.phone:
- intent.setAction(Intent.ACTION_DIAL);
- intent.setData(Uri.parse("tel:123456789"));
- startActivity(intent);
- break;
- case R.id.email:
- intent.setAction(Intent.ACTION_SENDTO);
- intent.setData(Uri.parse("smsto:5554"));
- startActivity(intent);
- break;
- default:
- break;
- }
- }
- };
- phone.setOnClickListener(cl);
- email.setOnClickListener(cl);
-
- }
- }
- <uses-permission android:name="android.permission.CALL_PHONE"/>
- <uses-permission android:name="android.permission.SEND_SMS"/>
- package com.example.actioncategory;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ImageButton;
-
- public class MainActivity extends AppCompatActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- ImageButton imageButton = findViewById(R.id.imageButton);
- imageButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent();
- intent.setAction(Intent.ACTION_MAIN);
- intent.addCategory(Intent.CATEGORY_HOME);
- startActivity(intent);
- }
- });
- }
- }
Extras:就像前面的putExtras()和getExtras()一样,可以在Activity之间传递信息
Flags:emmm,啥栈啊啥玩意儿的,如下设置后可以不保存历史。
- public class MainActivity extends AppCompatActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- Button button = findViewById(R.id.btn);
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent(MainActivity.this, DetailActivity.class);
- intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
- startActivity(intent);
- }
- });
- }
- }
明确知道要显示的是啥
- package com.example.implicitintent;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
-
- public class MainActivity extends AppCompatActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- Button btn = findViewById(R.id.btn);
- btn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent();
- intent.setAction(Intent.ACTION_VIEW);
- intent.setData(Uri.parse("http://www.mingrisoft.com"));
- startActivity(intent);
- }
- });
- }
- }
在 Manifest文件中编写intent-filter
一般通过action+category或者action+data这两种方式来进行过滤
-
- android:name=".ShowActivity"
- android:exported="true">
-
-
"android.intent.action.VIEW"/> -
"android.intent.category.DEFAULT"/> -
-
- public class MainActivity extends AppCompatActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- Button btn = findViewById(R.id.btn);
- btn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent();
- intent.setAction(Intent.ACTION_VIEW);
- intent.addCategory(Intent.CATEGORY_DEFAULT);
- startActivity(intent);
- }
- });
-
- }
- }