一、显式Intent
FirstActivity.java(SecondActivity.java与此几乎完全相同,只是变量名称不一样)
package com.example.intenttest;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class FirstActivity extends AppCompatActivity {
private Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout);
btn1=(Button)findViewById(R.id.button_1);
btn1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
//第一个参数Context要求提供启动活动的上下文,第二个参数Class指定想要启动的目标活动
Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
//专门用来启动意图的方法
startActivity(intent);
}
});
}
}
first_layout.xml(second_layout.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"
>
<Button
android:id="@+id/button_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button1"/>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.intenttest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".SecondActivity">
</activity>
<activity android:name=".FirstActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
效果图:
二、隐式Intent
-
定义
不明确的指出我们想要启动哪一个活动,而是指定一系列更为抽象的action和category等信息,然后交由系统去分析这个Intent,并帮我们找出合适的活动去启动
-
实例(改写上面的功能)
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.intenttest"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".SecondActivity"> <intent-filter> <!--指明SecondActivity界面可以接受com.example.activitytest.ACTION_START的Action--> <action android:name="com.example.activitytest.ACTION_START"/> <category android:name="android.intent.category.DEFAULT"/> <category android:name="CategoryTest"/> </intent-filter> </activity> <activity android:name=".FirstActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
MainActivity.java
package com.example.intenttest; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class FirstActivity extends AppCompatActivity { private Button btn1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first_layout); btn1=(Button)findViewById(R.id.button_1); btn1.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ //隐式Intent Intent intent=new Intent("com.example.activitytest.ACTION_START"); intent.addCategory("CategoryTest"); startActivity(intent); } }); } }
结果:(效果与上面完全相同)
-
注意:
在MainActivity.java中添加了category后
intent.addCategory("CategoryTest");
要在AndroidManifest.xml进行注册,否则会导致程序崩溃
<category android:name="CategoryTest"/>
结果:
三、更多隐式Intent的用法
1. 打开网页
只需要将主程序中的代码修改成:
btn1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent);
}
});
效果:调用系统浏览器打开百度
2. 调出拨号界面
btn1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent intent=new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:10086"));
startActivity(intent);
}
});
效果:
3. 设置活动可以响应的数据类型
在标签中配置一个标签
将该活动设置为响应所有http协议的intent
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<!--响应所有http协议的intent-->
<data android:scheme="http"/>
</intent-filter>
效果:
四、利用Intent传递数据
1. 向下一个活动传递数据
FirstActivity.java
btn1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
String data="Hello SecondActivity";//传递的数据
Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("extra_data",data);//通过键值对的形式传递
startActivity(intent);
}
});
SecondActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
Intent intent=getIntent();
String data=intent.getStringExtra("extra_data");
Log.d("SecondActivity",data);//通过键找到值然后打印
}
结果:在SecondActivity的logcat下面发现已经接收到传来的信息
-
返回数据给上一个活动(待补充…)
《第一行代码》P51
看了一下,比向下传递复杂的多,而且感觉没有什么用,所以暂时放在这里,待补充…