(四)组间通信与广播

一、实验内容1、使用“IntentDemo”程序来完成如何使用Intent启动新的Activity?2、使用“ActivityCommunication”说明了如何以Sub-Activity方式启动子Activity,以及使用Intent进行组件间通信?3、使用“IntentResolutionDemo”说明了如何在AndroidManifest.xml文件中注册Intent过滤器,以及如何...
摘要由CSDN通过智能技术生成

一、实验内容

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字样。

图5.1.1启动界面图              5.1.2点击启动ACTIVITY按钮后的界面在这里插入图片描述
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
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值