Intent的功能有:

在mainActivity中为按钮1添加监听事件:

listener1 = new OnClickListener() {

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
       Intent intent1 = new Intent(mainActivity.this, Activity1.class);
        intent1.putExtra("mainActivity", "这是来自mainActivity的数据");
        startActivityForResult(intent1, REQUEST_CODE);
    }
};

在Activity1中接收来自mainActivity中Intent中的数据:

String data = null;
Bundle extras = getIntent().getExtras();
if (extras != null) {
    data = extras.getString("mainActivity");
}
setTitle("现在在Activity1里:" + data);

为Activity1中的按钮添加监听事件,返回一个Intent:

listener1 = new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Bundle bundle = new Bundle();
                bundle.putString("store", "数据来自Activity1");
                Intent mIntent = new Intent();
                mIntent.putExtras(bundle);
                setResult(RESULT_OK, mIntent);
                finish();
            }
        };

在mainActivity中覆写onActivityResult()方法,对返回的内容处理:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE) {
            if (resultCode == RESULT_CANCELED) {
                setTitle("取消");
            } else if (resultCode == RESULT_OK) {
                String temp = null;
                Bundle extras = data.getExtras();
                if (extras != null) {
                    temp = extras.getString("store");
                }
                setTitle("在mainActivity中:"+temp);
            }
        }
    }

为按钮2添加监听事件:

protected static final String ACTION1 = "com.sunny.action.BROADCASE";

listener2 = new OnClickListener() {

@Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent2 = new Intent(ACTION1);
                sendBroadcast(intent2);
            }
        };

添加一个Broadcast Receiver,其捕获action为com.sunny.action.BROADCASE的Intent,生成Notification:

public class broadcastReceive1 extends BroadcastReceiver {
    private static final int NOTIFICATION_ID = 0;
    Context context;
    
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        this.context=context;
       showNotification();
    }

private void showNotification() {
        // TODO Auto-generated method stub
        NotificationManager notificationManager=(NotificationManager) context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
        Notification notification=new Notification(R.drawable.icon, "在broadcastReceive1中",System.currentTimeMillis());
        PendingIntent contentIntent=PendingIntent.getActivity(context, 0, new Intent(context,mainActivity.class), 0);
        notification.setLatestEventInfo(context, "在broadcastReceive1中:", null, contentIntent);
        notificationManager.notify(NOTIFICATION_ID, notification);
    }

}

其在AndroidManifest.xml中注册:

<receiver android:name=".broadcastReceive1">
    <intent-filter>
        <action android:name="com.sunny.action.BROADCASE" />
    </intent-filter>
</receiver>

Intent的功能有:

在mainActivity中为按钮1添加监听事件:

listener1 = new OnClickListener() {

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
       Intent intent1 = new Intent(mainActivity.this, Activity1.class);
        intent1.putExtra("mainActivity", "这是来自mainActivity的数据");
        startActivityForResult(intent1, REQUEST_CODE);
    }
};

在Activity1中接收来自mainActivity中Intent中的数据:

String data = null;
Bundle extras = getIntent().getExtras();
if (extras != null) {
    data = extras.getString("mainActivity");
}
setTitle("现在在Activity1里:" + data);

为Activity1中的按钮添加监听事件,返回一个Intent:

listener1 = new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Bundle bundle = new Bundle();
                bundle.putString("store", "数据来自Activity1");
                Intent mIntent = new Intent();
                mIntent.putExtras(bundle);
                setResult(RESULT_OK, mIntent);
                finish();
            }
        };

在mainActivity中覆写onActivityResult()方法,对返回的内容处理:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE) {
            if (resultCode == RESULT_CANCELED) {
                setTitle("取消");
            } else if (resultCode == RESULT_OK) {
                String temp = null;
                Bundle extras = data.getExtras();
                if (extras != null) {
                    temp = extras.getString("store");
                }
                setTitle("在mainActivity中:"+temp);
            }
        }
    }

为按钮2添加监听事件:

protected static final String ACTION1 = "com.sunny.action.BROADCASE";

listener2 = new OnClickListener() {

@Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent2 = new Intent(ACTION1);
                sendBroadcast(intent2);
            }
        };

添加一个Broadcast Receiver,其捕获action为com.sunny.action.BROADCASE的Intent,生成Notification:

public class broadcastReceive1 extends BroadcastReceiver {
    private static final int NOTIFICATION_ID = 0;
    Context context;
    
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        this.context=context;
       showNotification();
    }

private void showNotification() {
        // TODO Auto-generated method stub
        NotificationManager notificationManager=(NotificationManager) context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
        Notification notification=new Notification(R.drawable.icon, "在broadcastReceive1中",System.currentTimeMillis());
        PendingIntent contentIntent=PendingIntent.getActivity(context, 0, new Intent(context,mainActivity.class), 0);
        notification.setLatestEventInfo(context, "在broadcastReceive1中:", null, contentIntent);
        notificationManager.notify(NOTIFICATION_ID, notification);
    }

}

其在AndroidManifest.xml中注册:

<receiver android:name=".broadcastReceive1">
    <intent-filter>
        <action android:name="com.sunny.action.BROADCASE" />
    </intent-filter>
</receiver>

转载于:https://www.cnblogs.com/yechanglv/p/6922954.html

android 之 Intent、broadcast相关推荐

  1. Android组件之BroadCast简单实践

    作为Android的四大组件之一,没有理由不介绍一下BroadCast,BroadCast中文简单翻译就是广播,前阵子浙江某大学的啦啦操,广场舞的大妈,其中大妈和学生从喇叭和音响上听到的声音就是事件源 ...

  2. 【Android】 Intent应用详解

    转载:http://blog.csdn.net/liuhe688/article/details/7162988 看似尋常最奇崛,成如容易卻艱辛.北宋.王安石 看似普通的事情其实最不同寻常,并不是简简 ...

  3. Android核心 intent基础

    Intent(一简介) 一个安卓程序由多个组件组成,各个组件之间可以使用Intent进行交流.Intent对象包含了组件名称.动作.数据.种类.额外.标记等内容. 组件名称: 组件名称的类是指:Com ...

  4. Android广播(Broadcast)

    Android广播(Broadcast) 一.Broadcast简介 Broadcast是android中的四大组件之一,是在组件之间传播数据(Intent)的一种机制.广播的发送者和接收者事先是不需 ...

  5. android 四大组件Broadcast Receiver

    本文介绍Broadcast Receiver,包括几部分内容:Broadcast Receiver概述及实例.自定义Broadcast Receiver.Broadcast Receiver的实现细节 ...

  6. android入门之broadcast

    1. 前言 广播Broadcast是android四大组件之一.是用来互相通信(传递信息)的一种机制. 通信包括: a) 组件间(应用内)通信 b) 进程间通信 2. 广播Brocast的基本使用方式 ...

  7. Android 意图(Intent)

    写在前面 Android中,意图(Intent)是一个消息传递对象,活动.服务和广播接收器之间调用和消息传递都是通过意图实现的.意图的三个基本的用途为:启动Activity.启动Service和传递广 ...

  8. Android 30. 广播-Broadcast(一)

    为了便于进行系统级别的消息通知,Android 引入了Broadcast 机制. Android 中每个应用程序都可以对自己感兴趣的Broadcast 进行注册,其消息可能来自系统,也可能来自其他应用 ...

  9. android 使用intent传递对象,Android--Intent传递对象

    Intent 传递对象通常有两种实现方式,Serializable 和 Parcelable: 一.Serializable:序列化,表示将一个对象转换成可存储或可传输的状态,序列化后的对象可以在网络 ...

  10. android intent email,Android Email Intent

    问题 I've set up two buttons. One opens the compose sms intent and the other opens the compose email i ...

最新文章

  1. java中ajax概念_Java之AJAX概念和实现方式
  2. DataGrid 的 全选/取消全选 控制(CheckBox)
  3. linux 打包压缩工具
  4. 009_JSONFunction对象
  5. Sed教程(五):管理模式、正则表达式、使用功能
  6. .Net4.0并行库介绍——Cancellation Framework
  7. 【实用】表维护视图SM30增加自定义按钮的实现
  8. git 一口气带你走完git之旅
  9. PS教程第一课:PS简介
  10. java分库校验商户流水号是否重复,asp中用数据库生成不重复的流水号
  11. Web前端笔记(6)
  12. 序列每天从0开始_006 Python基础:通用序列操作
  13. Java基础复习笔记系列 七 IO操作
  14. 压缩解压缩文件zlib
  15. 数据分析报告怎么写(二)
  16. CentOS 使用二进制部署 Kubernetes 1.13集群
  17. Image.save()
  18. 使用Goods类创建十个商品 第四章 面向对象(上)课堂作业2
  19. 一对一直播软件如何盈利?
  20. linux root删除垃圾箱,Linux 用 root 用户都无法删除的文件如何删除

热门文章

  1. VS Code 安装插件、自定义模板、自定义配置参数、自定义主题、配置参数说明、常用的扩展插件
  2. 前端Vue学习之路(二)-Vue-router路由
  3. VTA:深度学习加速器堆栈
  4. Docker Buildx插件
  5. 嵌入式Linux的OTA更新,基础知识和实现
  6. 外部NORFlash是第一个以硬件为基础的信任
  7. 从单一图像中提取文档图像:ICCV2019论文解读
  8. Python机器学习:训练Tesseract
  9. java程序语句是_Java-语言编程
  10. Git 头指针分离与 FETCH_HEAD