android 之 Intent、broadcast
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>

android 之 Intent、broadcast的更多相关文章
- Android中Intent具体解释(二)之使用Intent广播事件及Broadcast Receiver简单介绍
通过第一篇的解说,我们已经看到了怎样使用Intent来启动新的应用程序组件,可是实际上他们也能够使用sendBroadcast方法来在组件间匿名的广播消息. 作为一个系统级别的消息传递机制,Inten ...
- Android随笔之——Android广播机制Broadcast详解
在Android中,有一些操作完成以后,会发送广播,比如说发出一条短信,或打出一个电话,如果某个程序接收了这个广播,就会做相应的处理.这个广播跟我们传统意义中的电台广播有些相似之处.之所以叫做广播,就 ...
- android 四大组件Broadcast Receiver
本文介绍Broadcast Receiver,包括几部分内容:Broadcast Receiver概述及实例.自定义Broadcast Receiver.Broadcast Receiver的实现细节 ...
- Android学习笔记--Broadcast, BroadcastReceiver(广播)
参考资料:http://www.cnblogs.com/playing/archive/2011/03/23/1992030.html 在 Android 中使用 Activity, Service, ...
- Android四大组件--Broadcast Receiver具体解释
本文主要讲述了: 一.BroadcastReceiver概述: 二.BroadcastReceiver事件分类 三.BroadcastReceiver事件的编程流程 四.两类BroadcastRece ...
- Android组件之BroadCast简单实践
作为Android的四大组件之一,没有理由不介绍一下BroadCast,BroadCast中文简单翻译就是广播,前阵子浙江某大学的啦啦操,广场舞的大妈,其中大妈和学生从喇叭和音响上听到的声音就是事件源 ...
- 【转】【Java/Android】Intent的简介以及属性的详解
一.Intent的介绍 Intent的中文意思是“意图,意向”,在Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述 ...
- Android之Intent和Activity
Intent能够说是Android的灵魂,程序跳转和传递数据的时候基本上就是靠Intent了.Intent在Android应用中是相当重要的,理解Intent相应用编程非常有帮助.在Android的官 ...
- Android学习之Broadcast初体验
•何为 Broadcast ? Broadcast 直译广播,接下来举个形象的例子来理解下 Broadcast: 上学的时候,每个班级都会有一个挂在墙上的大喇叭,用来广播一些通知,比如,开学要去搬书, ...
- Android 笔记 Intent and Bundle day7
学习了Intent与Bundle的使用,进行应用中的交互 package com.example.intent; import android.app.Activity; import android ...
随机推荐
- java中的线程安全是什么?什么叫线程安全?什么叫不安全?
java中的线程安全是什么: 就是线程同步的意思,就是当一个程序对一个线程安全的方法或者语句进行访问的时候,其他的不能再对他进行操作了,必须等到这次访问结束以后才能对这个线程安全的方法进行访问 什么叫 ...
- Json的详细用法
参考博客:https://www.cnblogs.com/haiyan123/p/7829080.html 1.json(Javascript Obiect Notation,JS对象标记)是一种 ...
- ES-什么是elasticsearch
前言 观今宜鉴古,无古不成今.在学习elasticsearch之前,我们要知道,elasticsearch是什么?为什么要学习elasticsearch?以及用它能干什么? 关于elasticsear ...
- org.apache.axis2.AxisFault: Service class XXXXX must have public as access Modifier解决方案
使用Axis2工具生成客户端调用辅助类后,编写客户端调用代码运行时报错,完整错误信息如下: log4j:WARN No appenders could be found for logger (org ...
- vue分环境打包配置方法一
直接上代码配置: 首先是config下面的文件修改 dev.env.js 'use strict' const merge = require('webpack-merge') const prod ...
- Slacklining 2017/2/6
原文 Get ready for a different kind of challenge What happens when mountain climbers take a day off?Ap ...
- ObjectiveC中的赋值,对象拷贝,浅拷贝与深拷贝
在开发过程中我们经常会遇到对象拷贝的问题,下面我们分别讨论赋值操作.对象拷贝.以及浅拷贝(Shallow copy)与深拷贝(Deep copy)的区别与各自的实现方式. 一.不同对象的赋值操作 Ob ...
- Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.Reason: Failed to determine a suitable driver class
解决方案: @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) 作用://取消数据库配置 但是 在用到数据库的时候记 ...
- ace editor 使用教程
<!DOCTYPE html><html> <head> <title>Demo of ACE Editor</title> <!-- ...
- 按Esc键实现关闭窗体
实现效果: 知识运用: KeyEventArgs类的KeyData属性 //获取KeyDown或KeyUp事件的键数据 public Keys KeyData {get;} 实现代码: private ...