想让第一个activity把第二个activity打开的话,在清单文件里面声明一下并且

右键Debug As Android Application居然没有报错


mimeType  讲HTML的时候就讲过的对应的一个响应头。setContentType("text/html").这个实际上就是一个mime。

通过mime就可以说明当前这个activity可以处理的数据。我当前这个activity接收什么样类型的数据?所以mime就是用来说明我当前应用可以处理的媒体类型都有哪些?可以打开的具体内容是怎么样的?

当然这个东西咱们也可以自定义。自定义的格式是:类型/子类型.

        <activity
android:name="com.itheima.callnewactivity.SecondActivity">
<intent-filter >
<action android:name="com.itheima.second"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="itheima"/>
</intent-filter>
<intent-filter >
<action android:name="com.itheima.second2"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="itheima"
android:mimeType="itcast/itheima"
/>
</intent-filter> </activity>

给SecondActivity搞了两个意图过滤器,实际上只需要匹配其中的一个。只要有一个完整的intent-filter被匹配上,我就可以把对应的activity给它打开。

Intent android.content.Intent.setType(String type)

Set an explicit MIME data type. 

This is used to create intents that only specify a type and not data, for example to indicate the type of data to return. 

This method automatically clears any data that was previously set (for example by setData). 

Note: MIME type matching in the Android framework is case-sensitive, unlike formal RFC MIME types. As a result, you should always write your MIME types with lower case letters, or use normalizeMimeType or setTypeAndNormalize to ensure that it is converted to lower case.
Parameters:type The MIME type of the data being handled by this intent.Returns:Returns the same Intent object, for chaining multiple calls into a single statement.See Also:getTypesetTypeAndNormalizesetDataAndTypenormalizeMimeType

首先找FATAL EXCEPTION:main,从这儿开始的。

问题出在哪里?

Intent android.content.Intent.setType(String type)

Set an explicit MIME data type. 

This is used to create intents that only specify a type and not data, for example to indicate the type of data to return. 

This method automatically clears any data that was previously set (for example by setData).

Note: MIME type matching in the Android framework is case-sensitive, unlike formal RFC MIME types. As a result, you should always write your MIME types with lower case letters, or use normalizeMimeType or setTypeAndNormalize to ensure that it is converted to lower case.
Parameters:type The MIME type of the data being handled by this intent.Returns:Returns the same Intent object, for chaining multiple calls into a single statement.See Also:getTypesetTypeAndNormalizesetDataAndTypenormalizeMimeType
This method automatically clears any data that was previously set (for example by setData). 
调用setType()方法会自动清除任何的data数据(通过setData()来设置对应的内容).
所以setType()会自动清除setData()所设置的内容.
办法是先调setType()再调setData().
Intent android.content.Intent.setData(Uri data)

Set the data this intent is operating on. This method automatically clears any type that was previously set by setType or setTypeAndNormalize. 

Note: scheme matching in the Android framework is case-sensitive, unlike the formal RFC. As a result, you should always write your Uri with a lower case scheme, or use Uri.normalizeScheme or setDataAndNormalize to ensure that the scheme is converted to lower case.
Parameters:data The Uri of the data this intent is now targeting.Returns:Returns the same Intent object, for chaining multiple calls into a single statement.See Also:getDatasetDataAndNormalizeandroid.net.Intent.normalize
setData()会清除setType()的数据.这是一个死结。setData()和setType()是互斥的.
所以可以用setDataAndType()把Data和Type一并设置。
程序运行成功了:

通过匹配第二个intent-filter可以把对应的activity给它打开。实际上咱们通过intent-filter方式打开一个activity这就叫一个隐式意图。隐式意图就是说我通过匹配某一个activity的意图过滤器,我把这个意图过滤器里面所有该匹配的内容都匹配上了,那我就可以把对应的activity打开。同样如果你的一个activity希望别人用隐式意图的方式打开,那你就必须设置一个intent-filter。这就是隐式意图。


你必须搞一个意图过滤器,有了意图过滤器别人才可以用隐式意图的方式打开。还有一点要注意:

 <category android:name="android.intent.category.DEFAULT"/>

需要注意一点的是:category这个DEFAULT虽说不用匹配,但是这个东西必须得有。这个category DEFAULT虽说不需要通过代码的方式去匹配它,但是你想用隐式意图把这个打开,默认的这个category一定要在清单文件里面给它配置上。


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.callnewactivity"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itheima.callnewactivity.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="com.itheima.callnewactivity.SecondActivity">
<intent-filter >
<action android:name="com.itheima.second"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="itheima"/>
</intent-filter>
<intent-filter >
<action android:name="com.itheima.second2"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="itheima"
android:mimeType="itcast/itheima"
/>
</intent-filter> </activity>
</application> </manifest>
<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=".MainActivity" > <Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="call"
android:text="打电话" />
<TextView
android:id="@+id/tv"
android:layout_below="@id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:textSize="20sp"
android:text="第一个activity"/>
<Button
android:layout_below="@id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="openSecond"
android:text="打开第二个activity"/>
<Button
android:layout_toRightOf="@id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="openSecond2"
android:text="打开第二个activity"/>
</RelativeLayout>
<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=".MainActivity" > <Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="call"
android:text="打电话" />
<TextView
android:layout_below="@id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#00ff00"
android:textSize="20sp"
android:text="第二个activity"
/>
</RelativeLayout>
package com.itheima.callnewactivity;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} public void call(View v){
Intent intent = new Intent();
//给意图设置一个动作
intent.setAction(Intent.ACTION_CALL);
//给意图设置数据
intent.setData(Uri.parse("tel:"+110));
//打开打电话的activity
startActivity(intent); }
public void openSecond(View v){
Intent intent = new Intent();
intent.setAction("com.itheima.second");
intent.setData(Uri.parse("itheima:"+1223));
startActivity(intent);
}
public void openSecond2(View v){
Intent intent = new Intent();
intent.setAction("com.itheima.second2");
//intent.setData(Uri.parse("itheima:"+1233));
//intent.setType("itcast/itheima");//
//intent.setData(Uri.parse("itheima:"+1233));
intent.setDataAndType(Uri.parse("itheima:"+1223), "itcast/itheima");
startActivity(intent);
}
}
package com.itheima.callnewactivity;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View; public class SecondActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
setContentView(R.layout.activity_second);
} public void call(View v){
Intent intent = new Intent();
//给意图设置一个动作
intent.setAction(Intent.ACTION_CALL);
//给意图设置数据
intent.setData(Uri.parse("tel:"+110));
//打开打电话的activity
startActivity(intent); } }
 

03_隐式意图打开activity的更多相关文章

  1. 无废话Android之smartimageview使用、android多线程下载、显式意图激活另外一个activity,检查网络是否可用定位到网络的位置、隐式意图激活另外一个activity、隐式意图的配置,自定义隐式意图、在不同activity之间数据传递(5)

    1.smartimageview使用 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...

  2. android 21 隐式意图启动系统预定义activity

    Intent intent=new Intent(LoginActivity.this, MainActivity.class);//显示意图启动,显示从一个activity到另一个activity, ...

  3. [android] 隐式意图激活另外一个activity

    随着api的升级,系统的很多应用包名和类名都改掉了,所以很多时候,打开系统应用的时候会报错,隐式意图就是解决组件之间松耦合,描述动作行为 获取Intent对象,通过new出来 调用Intent对象的s ...

  4. 隐式意图启动一个Activity

    隐式意图是通过指定一组动作或者属性实现,主要用于跨应用使用. 1.创建一个意图对象 Intent intent = new Intent(); 2.设置意图过滤器 intent.setAction(& ...

  5. android intent 隐式意图和显示意图(activity跳转)

    android中的意图有显示意图和隐式意图两种, 显示意图要求必须知道被激活组件的包和class 隐式意图只需要知道跳转activity的动作和数据,就可以激活对应的组件 A 主activity  B ...

  6. Activity组件:(一)通过显式意图和隐式意图来实现Activity间的跳转

    一.通过显式意图来实现Activity间的跳转 显式意图是指在创建Intent对象时就指定接受者组件 /** * 下面是通过显式意图进行跳转,即明确写出要跳转到SecondActivity.class ...

  7. Activity组件(二):通过显式意图和隐式意图来跳转至第三方应用

    一.显式意图来跳转到第三方应用 /** * 这个方法会在点击按钮的时候执行 * @param view */ public void skip2Browser(View view){ Log.d(TA ...

  8. [android] 隐式意图和显式意图的使用场景

    激活系统的某些应用,并且往应用里面填一些数据,比如说短信应用 打开短信应用,查看logcat,找到ActivityManager, 看到Display.com.android.mms/.ui.Comp ...

  9. 04_显示意图打开activity

    实际上用显式意图打开一个activity就很简单了.只需要指定你要打开的这个activity的class就可以了. 需要注意一点的是创建了一个ThirdActivity必须要在清单文件里面声明.如果没 ...

随机推荐

  1. CodeIgniter与Zend Acl结合实现轻量级权限控制

    CodeIgniter与Zend Acl结合实现轻量级权限控制 Tag :CodeIgniter Zend Acl 权限控制 1. Zend_Acl简介 Zend_Acl 为权限管理提供轻量并灵活的访 ...

  2. poj——3118 Arbiter

      Arbiter 题目描述:      “仲裁者”是<星际争霸>科幻系列中的一种太空船.仲裁者级太空船是神族的战船,专门提供精神力支援.不像其他战船的人员主要是战士阶级,仲裁者所承载的都 ...

  3. hdu6212 祖玛(区间DP)

    题意 有一个长度为n的01串,我们可以在某个地方插入一个0或者1,那么如果有连续颜色相同的>=3个,那么这段就会消去,两边的合拢.问将所有01串消去,最少需要插入多少个.(n<=200) ...

  4. C/C++实现bmp文件读写

    之前知道点bmp图的格式,然后对8位操作过,然后今天弄了一下24位真彩色的. C++读取.旋转和保存bmp图像文件编程实现 主要是理解bmp文件的格式8/24位的区别 8位图有调色板,24位在文件头和 ...

  5. Angular结构简单介绍

    在当前项目目录下,使用Angular-CLI生成一个组件:heroes ng generate component heroes 主界面(也就是一个主模块+多个单模块,例如我们创建的heroes(单模 ...

  6. 设计模式 之代理(Proxy)模式

    为什么这里要定义代理呢?所谓代理代理,当然就是你不想做的事.找别人去做,这就是代理.所以,当你写代码的时候.你想保持类的简单性.重用性.你就能够把事件尽量都交给其他类去做.自己仅仅管做好自己的事.也就 ...

  7. VS自己定义project向导开发(Custom Wizard)

     在vs2010中定制VC++project向导有例如以下两种方式: 改动现有向导.以满足自己的须要: 使用"自己定义向导"来产生一个向导骨架,并设计自己的向导. 本文档仅仅讨 ...

  8. 编程题:1. var person = '{name:"Lily",sex:"famale",age:24,country:"US"}';将person转换成JSON对象并便利每个属性值。

    /// <summary> /// Json工具类 /// </summary> public class JsonUtility { private static JsonU ...

  9. rhel6 中安装使用finger命令

    rhel6中默认没有finger 命令, 到rpm 包网上没有找到合适的, 然后在终端中输入rpm -qa|grep finger 查到了其相关的一个rpm包, 然 yum install finge ...

  10. 包管理 import debug 模块管理 module

    import sys, os this_file_abspath = os.path.dirname(os.path.abspath(__file__)) ProjectUtil_path = '{} ...