Intent 进阶

Intent 进阶
(一)概述
    Intent 负责组件之间消息传递和跳转,较好地实现了组件之间的解耦合。
    耦合是指两个应用程序之间存在的依赖关系程度。依赖程度高的称为强耦合,依赖程度低的称为弱耦合。
    软件开发推荐应用程序之间采用弱耦合的方式,即某个应用程序的改动,对其它的应用程序的影响越小越好。这种设计方式能最大程度地降低程序维护的成本。


预定义 mActionmCategories 属性值
    Action 和 category 的值可以自定义,Android 系统也提供了许多预定义的常量值,用于启动系统预定义的 Activity、Service。

Intent 类中与 Action 相关的常量列表

mAction 常量    对应的字符串            说明
ACTION_MIAN    android.intent.action.MAIN    应用程序入口
ACTION_VIEW    android.intent.action.VIEW    显示指定数据
ACTION_EDIT    android.intent.action.EDIT    编辑指定数据
ACTION_DIAL    android.intent.action.DIAL    显示拨号面板
ACTION_CALL    android.intent.action.CALL    向指定用户打电话
ACTION_SEND    android.intent.action.SEND    发送数据
ACTION_SENDTO    android.intent.action.SENDTO    发送消息
ACTION_ANSWER    android.intent.action.ANSWER    应答电话
ACTION_INSERT    android.intent.action.INSERT    插入数据
ACTION_DELETE    android.intent.action.DELETE    删除数据
ACTION_RUN    android.intent.action.RUN    运行数据
ACTION_SYNC    android.intent.action.SYNC    用户数据同步
ACTION_PICK_ACTIVITY                                    android.intent.action.PICK_ACTIVIT                                    选择Activity
ACTION_SEARCH    android.intent.action.SEARCH    执行搜索
ACTION_WEB_SEARCH   
        android.intent.action.WEB_SEARCH
                        执行Web搜索

 
Intent 类中与 Category 相关的常量值列表

mCategory 常量       对应的字符串                      说明
CATEGORY_DEFAULT     android.intent.category_DEFAULT   默认的Category   
CATEGORY_LAUNCHER
        android.intent.category_LAUNCHER
                Activity 显示在顶级程序列表中
CATEGORY_INFO
        android.intent.category_INFO
                用于提供包信息
CATEGORY_HOME
        android.intent.category_HOME
                设置该 Activity 随系统启动而运行
CATEGORY_PREFERENCE
        android.intent.category_PREFERENCE
                设置 Activity 是参数面板

 

(二)<intent-filter>标签
1、概述
    以上的启动还差一个环节,在项目清单中注册组件时,在<intent-filter>标签中设置<action>标签和<category>标签的值
    <intent-filter>是 Intent 的过滤器,在该过滤器中通过在<action>、<category>标签中设置条件,凡符合设置条件的 Activity 都会被启动。

2、通过 Action 和 Category 启动 Activity
    假设有一个名为 SecondActivity.java 的 Activity,项目入口为:MainActivity,按以下步骤启动 SecondActivity:

步骤1、在项目清单文件中注册 SecondActivity 类。
<activity android:name=".SecondActivity">
    <intent-filter>
        <action android:name="com.jxust.SECOND_ACTIVITY"/>
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

步骤2、在MainActivity 的 onCreate()方法中输入以下代码:
    Intent intent = new Intent();
    intent.setAction("com.jxust.SECOND_ACTIVITY");
    intent.addCategory("android.intent.category.DEFAULT");


*** 隐式意图启动系统预定义Activity ***

为什么要用隐式意图启动系统预定义 Activity?
因为显式意图只能用于启动同一个程序中的 Activity,如果想要启动不同程序中的 Activity 就需要使用隐式意图来启动。


实例:

 

MainActivity.java代码

 

package com.jxust.day04_06_intentdemo;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity implements OnClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		setListener();
	}

	private void setListener() {

		findViewById(R.id.btnBrowser).setOnClickListener(this);
		findViewById(R.id.btnCall).setOnClickListener(this);
		findViewById(R.id.btnDial).setOnClickListener(this);
		findViewById(R.id.btnInstall).setOnClickListener(this);
		findViewById(R.id.btnPlayMusic).setOnClickListener(this);
		findViewById(R.id.btnSendSms).setOnClickListener(this);
		findViewById(R.id.btnStartSecondActivity).setOnClickListener(this);
		findViewById(R.id.btnUninstall).setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		Intent intent = null;
		switch (v.getId()) {
		case R.id.btnBrowser:
			intent = new Intent(Intent.ACTION_VIEW);
			intent.setData(Uri.parse("http://www.baidu.com"));
			break;
		case R.id.btnCall:
			intent = new Intent(Intent.ACTION_CALL);
			intent.setData(Uri.parse("tel:11111111111")); // 这个是拨号的固定格式
			// 拨号要一个权限所以要在Manifest里设置好
			break;
		case R.id.btnDial:
			intent = new Intent(Intent.ACTION_DIAL);
			intent.setData(Uri.parse("tel:110"));
			break;
		case R.id.btnInstall: {
			// 自动找到 android 系统中的 SD 卡中的 Download 目录
			File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
			File file = new File(dir, "mobileqq_android.apk");
			intent = new Intent(Intent.ACTION_VIEW);
			// 特别需要注意的是application/vnd.android.package-archive 是固定写法
			intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
		}
			break;
		case R.id.btnPlayMusic: {
			intent = new Intent(Intent.ACTION_VIEW);
			File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
			File file = new File(dir, "Amy Diamond - Heartbeats.mp3");
			intent.setDataAndType(Uri.fromFile(file), "audio/mp3");
		}
			break;
		case R.id.btnSendSms:
			intent = new Intent(Intent.ACTION_SENDTO);
			intent.setData(Uri.parse("smsto:11111111111"));
			intent.putExtra("sms_body", "hello android!");
			break;
		case R.id.btnStartSecondActivity:
			intent = new Intent("com.jxust.day04_06.SecondActivity");
			break;
		case R.id.btnUninstall:
			intent = new Intent(Intent.ACTION_DELETE);
			intent.setData(Uri.parse("package:com.jxust.day04_02_startactivityforresut"));
			break;
		}
		startActivity(intent);
	}

}

 

activity_main.xml代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnStartSecondActivity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="start SecondActivity" />

    <Button
        android:id="@+id/btnBrowser"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="浏览网页" />

    <Button
        android:id="@+id/btnCall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="拨打电话" />

    <Button
        android:id="@+id/btnDial"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="启动拨号面板" />

    <Button
        android:id="@+id/btnUninstall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="卸载应用程序" />

    <Button
        android:id="@+id/btnInstall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="安装应用程序" />

    <Button
        android:id="@+id/btnSendSms"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="发送短信" />

    <Button
        android:id="@+id/btnPlayMusic"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="播放音乐" />
    

</LinearLayout>

 
    Manifast.xml代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.jxust.day04_06_intentdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />
    
    <uses-permission android:name="android.permission.CALL_PHONE"/>	<!-- 拨号所需要的权限 -->
    <uses-permission android:name="android.permission.INTERNET"/>		<!-- 浏览网页所需要的权限 -->
    

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SecondActivity"
            android:label="@string/title_activity_second" >
            <intent-filter >
                <action android:name="com.jxust.day04_06.SecondActivity"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

 
activity_second.xml代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.jxust.day04_06_intentdemo.SecondActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

 

SecondActivity.java代码

package com.jxust.day04_06_intentdemo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class SecondActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);
		Log.i("main", "SecondActivity.onCreate()");
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值