Android入门练习——Intent的隐式调用(有bug)

Intent显式调用看这里:Android入门练习——Intent的显式调用

隐式Intent

不明确指定启动哪个组件,而是让系统来筛选出合适的Activity。
使用隐式Intent的时候,系统通过将Intent对象中的IntentFilter与组件在AndroidManifest.xml或者代码中动态声明的IntentFilter进行比较,从而找到要启动的相应组件。如果组件的IntentFilter与Intent中的IntentFilter正好匹配,系统就会启动该组件,并把Intent传递给它。如果有多个组件同时匹配到了,则系统会弹出一个选择框,让用户选择使用哪个应用去处理这个Intent。

<intent-filter>……</intent-filter>里定义的所有内容都是用来定义该activity可以被哪些intent激活的,如果匹配,就会被激活。

<intent-filter>里有以下几个属性可以让intent来匹配:Action、Category、Data;

  • Action:该activity可以执行的动作
    如果跟这里<intent-filter>所列出的任意一个匹配的话,就说明这个activity是可以完成这个intent的意图的,可以将它激活。可以用setAction函数为intent对象指定一个动作,也可以用getAction读取Intent对象中的动作信息。
    注意:一条<intent-filter>元素至少应该包含一个<action>,否则任何Intent请求都不能和该<intent-filter>匹配。一个Intent只能指定一个action,但是一个Activity可以设置(监听、匹配)多个action(即intent-filter中可以设置多个action属性),这是两个不同的概念。

  • Category:用于指定当前动作(Action)执行的环境
    即这个activity在哪个环境中才能被激活。不属于这个环境的,不能被激活。
    如果该activity想要通过隐式intent方式激活,那么不能没有任何category设置,
    至少包含一个android.intent.category.DEFAULT
    一个Intent只能有一个Action,但是可以有多个Category.同一个Intent中的多个Category项彼此间是“与”的关系。也就是说一个组件需要支持全部的category项才能处理该请求。

  • Data:执行时要操作的数据,即执行动作的URI,不同的动作有不同的数据规格。
    如果定义了Data,但intent却没有传进来指定类型的Data时,也不能激活该activity。

    < data android:scheme="http" android:host=" www.baidu.com" /> 

常用的数据规格有:

实例:
效果:


(最后一个???有bug? 目前还不知道问题出在哪 ,找出来以后来更新)
结构:

  • activity_main.xml
<LinearLayout 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:orientation="vertical" >

    <Button
        android:id="@+id/btn_01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="隐式调用Intent" />

    <Button
        android:id="@+id/btn_02"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="打开百度" />

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

    <Button
        android:id="@+id/btn_04"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="跳转时选择应用程序" />

</LinearLayout>
  • activity03.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="隐式调用的activity"
        />

</LinearLayout>
  • activity04.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是activity04" />

</LinearLayout>
  • MainActivity.java
public class MainActivity extends Activity {
	private Button btn1;
	private Button btn2;
	private Button btn3;
	private Button btn4;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		/*
		 * 隐式Intent没有明确的目标组件名称,所以必须由Android系统帮助应用程序寻找与Intent请求意图最匹配的组件。
		 * 某个activity能不能被某个intent激活,要看这个activity是不是符合这个intent要求的定义,
		 * 而这个定义就在AndroidManifest.xml中。。。。
		 *  
		 */
		btn1=(Button) findViewById(R.id.btn_01);
		btn2=(Button) findViewById(R.id.btn_02);
		btn3=(Button) findViewById(R.id.btn_03);
		btn4=(Button) findViewById(R.id.btn_04);
		
		btn1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent=new Intent();
				intent.setAction("com.example.intentdemo3.MYACTION");
				//intent.setAction("com.example.intentdemo3.MYACTION1");
				intent.addCategory("com.example.intentdemo3.MYINTENT");
				//intent.addCategory("com.example.intentdemo3.MYINTENT1");
				startActivity(intent);
			}
		});
		
		btn2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();  
                intent.setAction(Intent.ACTION_VIEW);
                //ACTION_VIEW:系统根据不同的Data类型,通过已注册的对应Application显示数据。  
                intent.setData(Uri.parse("http://www.baidu.com"));
                //通用资源标志符(Universal Resource Identifier, 简称"URI")。 Uri代表要操作的数据
                startActivity(intent);  //调用浏览器打开指定网页
				
			}
		});
		
		btn3.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent=new Intent();
				intent.setAction(Intent.ACTION_DIAL);
				intent.setData(Uri.parse("tel:10086"));
				startActivity(intent); //调用拨号程序
			}
		});
		
		btn4.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent=new Intent();
				intent.setAction("shareaction");//必须是已安装的app
				startActivity(intent); //当需要跳转Activity的intent-filter相同时,会给出选择。
				
			}
		});
		
	}
}

其中btn4想要打开分享到其他软件的操作时,必须时已安装的软件,例如

且两个软件中的AndroidManifest.xml中必须加上intent-filter过滤器:

<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="shareaction" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>
  • OtherActivity.java
public class OtherActivity extends Activity{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity04);
		
	}
	

}

  • SecondActivity.java
public class SecondActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity03);
	}
}
  • AndroidManifest.xml
 <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>
       
        <!--系统在使用隐式Intent的时候,会自动帮我们添加上“android.intent.category.default”,所以 
                               所有需要被隐式Intent启动的activity,
                               都要加上<category android:name="android.intent.category.DEFAULT" />这一段声明,
                               否则就会启动不了并提示无法匹配该Intent的错误 -->
        <activity
            android:name=".SecondActivity">
            <intent-filter>
                <action android:name="com.example.intentdemo3.MYACTION"/>
                <action android:name="com.example.intentdemo3.MYACTION1"/>
                <category android:name="android.intent.category.DEFAULT"/> 
            </intent-filter>
        </activity>
        
	<activity 
	    android:name=".OtherActivity">
	    <intent-filter >
	         <action android:name="com.example.intentdemo3.MYACTION"/>
             <category android:name="android.intent.category.DEFAULT"/>
             <category android:name="com.example.intentdemo3.MYINTENT"/>
             <category android:name="com.example.intentdemo3.MYINTENT1"/> 
	    </intent-filter>
	</activity>       
    </application>

猜你喜欢

转载自blog.csdn.net/qq_43145926/article/details/90051162