Intent
Intent intent = new Intent(this,xxxActivity.class);
startActivity(intent);
- 认识Intent
- Intent的对象属性
- Intent的种类
- Intent过滤器
认识
概念
Bundle 打包好数据
Intent 携带Bundle 在 各个组件之间传递数据
基本应用
- 通过一个Intent来开启一个新的Activity
- 开启一个后台Service
- 用来传递广播
对象属性
一组打包好的属性信息
- Component name 组件名称属性
使用 setComponent() 参数为: ComponentName 对象
创建 ComponentName 对象的时候需要两个参数 A:应用的包名 B:启动组件的类名
Intent intent = new Intent();
ComponentName componentName = new ComponentName("com.liyanfeng.shiyongintent", "com.liyanfeng.shiyongintent.DetailActivity");
intent.setComponent(componentName);
startActivity(intent);
Action和Data属性一块使用
常用的Action
- ACTION_CALL activity 启动一个电话.
- ACTION_EDIT activity 显示用户编辑的数据.
- ACTION_MAIN activity 作为Task中第一个Activity启动
- ACTION_SYNC activity 同步手机与数据服务器上的数据.
- ACTION_BATTERY_LOW broadcast receiver 电池电量过低警告.
- ACTION_HEADSET_PLUG broadcast receiver 插拔耳机警告
- ACTION_SCREEN_ON broadcast receiver 屏幕变亮警告.
- ACTION_TIMEZONE_CHANGED broadcast receiver 改变时区警告.
Action 指定将要进行的动作
Data 指定用到的数据
Action 使用 intent的Action常量来设置https://developer.android.google.cn/reference/android/content/Intent
Data 是个uri对象
例如使用Intent的Action来实现拨打电话和发送短信功能
package com.liyanfeng.shiyongintent;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button send_sms;
private Button callSomeBdy;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
send_sms = (Button) findViewById(R.id.send_sms);
send_sms.setOnClickListener(this);
callSomeBdy = (Button) findViewById(R.id.callSomeBdy);
callSomeBdy.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = new Intent();
switch (v.getId()) {
case R.id.send_sms:
intent.setAction(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:10086"));
intent.putExtra("sms_body", "哈哈哈哈");
startActivity(intent);
break;
case R.id.callSomeBdy:
intent.setAction(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:10086"));
startActivity(intent);
break;
}
}
}
在配置文件中添加发送短信和拨打电话的权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.liyanfeng.shiyongintent">
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".DetailActivity"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Action属性个Category属性一块使用
常用的Category
-
CATEGORY_DEFAULT:Android系统中默认的执行方式,按照普通Activity的执行方式执行。表示所有intent都可以激活它
-
CATEGORY_HOME:设置该组件为Home Activity。
-
CATEGORY_PREFERENCE:设置该组件为Preference。
-
CATEGORY_LAUNCHER:设置该组件为在当前应用程序启动器中优先级最高的Activity,通常为入口ACTION_MAIN配合使用。
-
CATEGORY_BROWSABLE:设置该组件可以使用浏览器启动。表示该activity只能用来浏览网页。
-
CATEGORY_GADGET:设置该组件可以内嵌到另外的Activity中。
使用Category 关闭并返回Home桌面
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
Extras 属性 往intent中添加附加信息
putExtras() getExtras() 主要用于Activity之间尽心数据交换
Flags属性 指定安卓程序启动哪个Activity,程序启动后要做如何处理
常用Flags常量
- FLAG_ACTIVITY_BROUGHT_TO_FRONT
- FLAG_ACTIVITY_CLEAR_TOP
- FLAG_ACTIVITY_NEW_TASK
- FLAG_ACTIVITY_NO_ANIMATION
- FLAG_ACTIVITY_NO_HISTORY
- FLAG_ACTIVITY_REORDER_TO_FRONT
- FLAG_ACTIVITY_SINGLE_TOP
ComponentName componentName = new ComponentName("com.liyanfeng.shiyongintent", "com.liyanfeng.shiyongintent.DetailActivity");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); // 指定当前Activity不再历史栈中保留,用户离开及关闭
intent.setComponent(componentName);
startActivity(intent);
Intent种类
- 显式
- 隐式
显式:创建Intent对象后指定组件名称,启动intent指定的Activity/Service。
Intent intent = new Intent(Context packageContext,Class<?>cls)
来构造
参数A:上下文对象 B:Activity.class 类
隐式:创建对象后,不指定组件名称,只指定action,category/data 然后让安卓去启动某些组件
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent);
Intent 过滤器
应用在使用隐式Intent中
设置方法:在配置 文件中要使用的组件中田间<intent-filter>
标记
<intent-filtet>
<action .../>
<category .../>
<data .../>
</intent-filter>
打开大图功能
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/little_img"
android:layout_width="200dp"
android:layout_height="200dp" />
<Button
android:id="@+id/showBig"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查看大图" />
</LinearLayout>
showActivity的xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".ShowActivity">
<ImageView
android:id="@+id/big_img"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
package com.liyanfeng.intentfilter;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
int img = R.drawable.img1;
private ImageView little_img;
private Button showBig;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
little_img = (ImageView) findViewById(R.id.little_img);
little_img.setImageResource(img);
showBig = (Button) findViewById(R.id.showBig);
showBig.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.showBig:
// 创建一个Intent,指定ACTION_VIEW方式打开
Bundle bundle = new Bundle();
bundle.putInt("img",img);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.putExtras(bundle);
startActivity(intent);
break;
}
}
}
showActivity 的 java
package com.liyanfeng.intentfilter;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
public class ShowActivity extends Activity {
private ImageView big_img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
initView();
makeBigImage();
}
private void initView() {
big_img = (ImageView) findViewById(R.id.big_img);
}
private void makeBigImage() {
Bundle bundle = getIntent().getExtras();
int img = bundle.getInt("img");
big_img.setImageResource(img);
}
}
配置文件中配置过滤器
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.liyanfeng.intentfilter">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ShowActivity">
<!-- 配种filter showActivity具有ACTION_VIEW功能-->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<!-- 默认启动showActivity 将其放在第一个位置 -->
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>