03_隐式意图打开activity

想让第一个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);
        
    }

}

 

 

转载于:https://www.cnblogs.com/ZHONGZHENHUA/p/7124129.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android中,我们可以使用隐式意图(Intent)来打开一个新的Activity隐式意图是指没有明确指明目标组件的意图,而是通过指定一些匹配条件来启动匹配的组件。 下面是一个使用隐式方式打开一个新的Activity的示例代码: ```java Intent intent = new Intent(); intent.setAction("com.example.action.OPEN_NEW_ACTIVITY"); intent.addCategory("android.intent.category.DEFAULT"); startActivity(intent); ``` 上面的代码中,我们首先创建了一个Intent对象,然后通过调用setAction()方法来设置Action,这里设置的是"com.example.action.OPEN_NEW_ACTIVITY"。接着,我们调用addCategory()方法来添加Category,这里添加的是"android.intent.category.DEFAULT"。最后,我们调用startActivity()方法来启动Activity。 在这个示例中,我们没有明确指定要启动哪个Activity,而是通过设置Action和Category来启动匹配的Activity。具体来说,我们要求被启动的Activity必须满足以下条件: - Action为"com.example.action.OPEN_NEW_ACTIVITY"。 - Category包含"android.intent.category.DEFAULT"。 如果有多个Activity同时满足这些条件,系统会弹出一个选择对话框让用户选择要启动的Activity。 需要注意的是,如果要使用隐式方式启动Activity,需要在被启动的Activity的AndroidManifest.xml文件中设置正确的Intent过滤器。具体来说,需要在<activity>标签中添加一个<intent-filter>标签,并在其中设置Action和Category。例如: ```xml <activity android:name=".NewActivity"> <intent-filter> <action android:name="com.example.action.OPEN_NEW_ACTIVITY" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> ``` 上面的代码中,我们在<activity>标签中添加了一个<intent-filter>标签,并在其中设置了Action和Category,这样就可以响应隐式意图了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值