一、实验内容
1、使用“IntentDemo”程序来完成如何使用Intent启动新的Activity?
2、使用“ActivityCommunication”说明了如何以Sub-Activity方式启动子Activity,以及使用Intent进行组件间通信?
3、使用“IntentResolutionDemo”说明了如何在AndroidManifest.xml文件中注册Intent过滤器,以及如何设置节点属性来捕获指定的Intent?
4、使用“BroadcastReceiverDemo”说明了如何在应用程序中注册BroadcastReceiver,并接收指定类型的广播消息?
二、实验仪器、设备
硬件:PC 微型计算机、1G以上内存,40G以上硬盘
软件:Windows XP,Eclipse , JDK , Android SDK
三、实验步骤
1.使用“IntentDemo”程序来完成如何使用Intent启动新的Activity;
1)创建IntentDemo工程
打开Android Studio,点击Start a new Android Studio prohect;
在Application name中输入IntentDemo,点击next;
勾选Phone and Tablet,点击next;
勾选Empty Activity,点击next;
在Activity Name中填写MainActivity,在Layout Name中添加main,点击finish。
2)程序代码
IntentDemo.java:
package com.example.intentdemo;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class IntentDemo extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button=(Button)findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(IntentDemo.this,ActivityToStart.class);
startActivity(intent);
}
});
}
}
ActivityToStart.java:
package com.example.intentdemo;
import android.app.Activity;
import android.os.Bundle;
public class ActivityToStart extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activitytostart);
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.intentdemo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".IntentDemo">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ActivityToStart">
</activity>
</application>
</manifest>
Main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动Activity"/>
</LinearLayout>
Activitytostart.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="match_parent"
android:layout_height="wrap_content"
android:text="ActivityToStart"/>
</LinearLayout>
String.xml:
<resources>
<string name="app_name">IntentDemo</string>
<string name="hello">Hello World,IntentDemo</string>
</resources>
3)运行程序
点击Run按钮运行程序,选择合适的安卓设备或虚拟机调试程序。
程序启动时进入IntentDemo活动,点击启动ACTIVITY按钮通过显式调用Intent进入活动ActivityToStart,并显示ActivityToStart字样。
2.使用“ActivityCommunication”说明了如何以Sub-Activity方式启动子Activity,以及使用Intent进行组件间通信
1)创建ActivityCommunication工程
打开Android Studio,点击Start a new Android Studio prohect;
在Application name中输入ActivityCommunication,点击next;
勾选Phone and Tablet,点击next;
勾选Empty Activity,点击next;
在Activity Name中填写ActivityCommunication,在Layout Name中添加main,点击finish。
2)程序代码
ActivityCommunication.java:
package com.example.activitycommunication;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ActivityCommunication extends Activity {
private static final int SUBACTIVITY1=1;
private static final int SUBACTIVITY2=2;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//定义控件
textView=(TextView)findViewById(R.id.textShow);
final Button button1=(Button)findViewById(R.id.btn1);
final Button button2=(Button)findViewById(R.id.btn2);
//设置监听事件
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//以Sub-Activity的方式启动Activity1
Intent intent=new Intent(ActivityCommunication.this,SubActivity1.class);
startActivityForResult(intent,SUBACTIVITY1);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//以Sub-Activity的方式启动Activity2
Intent intent=new Intent(ActivityCommunication.this,SubActivity2