Intent
- 用于启动Activity,启动Service,发送广播
显式Intent
Intent(Context, Class)
构造函数分别为应用和组件提供 Context
和 Class
对象。因此,此 Intent 将显式启动该应用中的 DownloadService
类。
| Intent downloadIntent = new Intent(this, DownloadService.class); |
| downloadIntent.setData(Uri.parse(fileUrl)); |
| startService(downloadIntent); |
| startActivity(new Intent(this, MyActivity.class)); |
隐式Intent
- 隐式 Intent 指定能够在可以执行相应操作的设备上调用任何应用的操作。如果您的应用无法执行该操作而其他应用可以,且您希望用户选取要使用的应用,则使用隐式 Intent 非常有用。
- 在第二个Activity的注册信息中添加Intent过滤器,表明此Activity可以响应哪些Intent
| <activity android:name=".MyActivity"> |
| <intent-filter> |
| |
| <action android:name="com.example.ACTION_START"/> |
| |
| <category android:name="android.intent.category.DEFAULT"/> |
| </intent-filter> |
| </activity> |
- 在第一个Activity中创建Intent用于启动第二个Activity
| |
| |
| Intent intent = new Intent("com.example.ACTION_START"); |
| startActivity(intent); |
| |
| intent.addCategory("com.example.MY_CATEGORY"); |
此时会报错:
Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent
因为第二个Activity的intentfilter中并没有声明可以响应这个category,添加上就能正常跳转界面。
| <activity android:name=".MyActivity"> |
| <intent-filter> |
| |
| <action android:name="com.example.ACTION_START"/> |
| |
| <category android:name="android.intent.category.DEFAULT"/> |
| <category android:name="com.example.MY_CATEGORY"/> |
| </intent-filter> |
| </activity> |
- 第一个Activity中创建可以访问浏览器的Intent
| Intent intent = new Intent(Intent.ACTION_VIEW); |
| intent.setData(Uri.parse("http://www.baidu.com")); |
| startActivity(intent); |
- 修改第三个Activity的intent过滤器,使他也能响应一个打开网页的Intent
| <intent-filter> |
| <action android:name="android.intent.action.VIEW"/> |
| <category android:name="android.intent.category.DEFAULT"/> |
| <data android:scheme="http"/> |
| </intent-filter> |
点击按钮后会弹出列表显示所有可以响应该Intent的Activity
基本类型传值
| Intent intent = new Intent(this, MainActivity2.class); |
| intent.putExtra("name", "wmj"); |
| intent.putExtra("sex", "female"); |
| startActivity(intent); |
| Intent intent = getIntent(); |
| Log.d("wmj", "onCreate: " + intent.getStringExtra("name") + intent.getStringExtra("sex")); |
返回数据给上一个Activity
| Intent intent = new Intent(MainActivity.this, MyActivity.class); |
| |
| startActivityForResult(intent, 1); |
- A中重写onActivityResult()用于接收返回的数据
| |
| |
| |
| |
| |
| |
| @Override |
| protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { |
| super.onActivityResult(requestCode, resultCode, data); |
| switch (requestCode) { |
| case 1: |
| if (resultCode == RESULT_OK) { |
| String returnedData = data.getStringExtra("data_return"); |
| Log.d(TAG, "onActivityResult: " + returnedData); |
| } |
| break; |
| default: |
| } |
| } |
- B中重写onBackPressed()通过点返回键,销毁B,并把数据返回给A
| @Override |
| public void onBackPressed() { |
| Intent intent = new Intent(); |
| intent.putExtra("data_return", "返回给上个界面的值"); |
| setResult(RESULT_OK, intent); |
| |
| finish(); |
| } |
Bundle
| Intent intent = new Intent(this, MainActivity2.class); |
| |
| Bundle bundle = new Bundle(); |
| bundle.putString("name", "wmj"); |
| bundle.putString("sex", "female"); |
| intent.putExtras(bundle); |
| |
| startActivity(intent); |
| Intent intent = getIntent(); |
| Log.d("wmj", "onCreate: " + intent.getStringExtra("name") + intent.getStringExtra("sex")); |
Serializable
- 必须实现Serializable接口
- 更适合jvm
- A页面
| Intent intent = new Intent(this, MainActivity2.class); |
| |
| |
| Student student = new Student("wmj", 7, 24); |
| intent.putExtra("student", student); |
| |
| startActivity(intent); |
| Intent intent = getIntent(); |
| |
| |
| Student student = (Student) intent.getSerializableExtra("student"); |
| Toast.makeText(this, student.name, Toast.LENGTH_SHORT).show(); |
Parcelable
- 必须实现Parcelable接口
- 更适合安卓
- A页面
| Intent intent = new Intent(this, MainActivity2.class); |
| |
| Teacher teacher = new Teacher("wmj", 24); |
| intent.putExtra("teac", teacher); |
| |
| startActivity(intent); |
| Teacher teacher = intent.getParcelableExtra("teac"); |
| Toast.makeText(this, teacher.name, Toast.LENGTH_SHORT).show(); |
| package com.example.myintent; |
| |
| import android.os.Parcel; |
| import android.os.Parcelable; |
| |
| public class Teacher implements Parcelable { |
| public String name; |
| public int age; |
| |
| public Teacher(String name, int age) { |
| this.name = name; |
| this.age = age; |
| } |
| |
| |
| protected Teacher(Parcel in) { |
| |
| name = in.readString(); |
| age = in.readInt(); |
| } |
| |
| |
| |
| @Override |
| public void writeToParcel(Parcel dest, int flags) { |
| |
| dest.writeString(name); |
| dest.writeInt(age); |
| } |
| |
| @Override |
| public int describeContents() { |
| return 0; |
| } |
| |
| public static final Creator<Teacher> CREATOR = new Creator<Teacher>() { |
| |
| |
| @Override |
| public Teacher createFromParcel(Parcel in) { |
| return new Teacher(in); |
| } |
| |
| @Override |
| public Teacher[] newArray(int size) { |
| return new Teacher[size]; |
| } |
| }; |
| } |
| |
this和MainActivity.this的区别
| protected void onCreate(Bundle savedInstenceState){ |
| super.onCreate(savedInstenceState); |
| |
| |
| Intent intent = new Intent(this,Another.class); |
| startActivity(intent); |
| |
| } |
| button.setOnClickListener(new View.OnClickListener() { |
| @Override |
| public void onClick(View view) { |
| |
| Intent intent = new Intent(this,LoginActivity.class); |
| startActivity(intent); |
| } |
| }); |
PendingIntent
| |
| |
| public static PendingIntent getActivity(Context context, int requestCode,Intent intent, int flags) |
| |
| |
| |
| public static PendingIntent getService(Context context, int requestCode,Intent intent, int flags) |
| |
| |
| |
| public static PendingIntent getBroadcast(Context context, int requestCode,Intent intent, int flags) |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步