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 ...
随机推荐
- python_16(bootstrap)
素材网址: 1.1 官网 https://jquery.com/ 1.2 开源库链接: https://www.bootcdn.cn/ 1.3 网页模板 www.jq22.com 1.1 bootst ...
- 创建Maven项目出错 pom出错
错误为 org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project.MavenProject, org.a ...
- 数据库 join
数据库 join 最近有个项目用到了数据库,写 sql 时有联表查询,不知道怎么写,怎过滤,查了一些资料,补充了一些知识点. 以下是链接: 维基百科 JOIN关联表中ON,WHERE后面跟条件的区别 ...
- 分布式系统ID生成办法
前言 一般单机或者单数据库的项目可能规模比较小,适应的场景也比较有限,平台的访问量和业务量都较小,业务ID的生成方式比较原始但是够用,它并没有给这样的系统带来问题和瓶颈,所以这种情况下我们并没有对此给 ...
- [问题记录]Ubuntu下chmsee安装失败的解决
日期:2016年2月26日 一直在找Ubuntu下查看chm的工具但是普遍不理想,发现在deepin中的chmsee相对比较好,但是直接执行网上的sudo apt-get install chmsee ...
- 浅析 var that = this;
在阅读别人的代码时,发现别人写的代码中有这么一句:var that = this;,这代表什么意思呢?经过一番查阅,才明白是这么回事. 在JavaScript中,this代表的是当前对象. var t ...
- sql创建作业--自动执行存储过程
创建自动执行存储过程: 1.创建参数 2.删除已有同名的作业 3. 创建作业 4.创建作业步骤 5.连接服务器 6.创建作业调度 7.启动作业 ALTER PROCEDURE dbo.sx_pro_A ...
- 申请Bing Search API
地址:https://datamarket.azure.com/home 没有帐号先注册一个,然后登录. 1:在数据中订阅Bing Search API,如果找不到就使用这个地址: https://d ...
- Android学习总结(八)———— 广播的最佳实践(实现强制下线功能)
一.基本概念 强制下线功能功能应该算是比较常见的了,很多应用程序都具备这个功能,比如你的QQ号或者微信号在别处登录了,就会将你强制挤下线.只需要在界面上弹出一个对话框,让用户无法进行任何其他的操作,必 ...
- COFF文件格式
链接器 目录 一 COFF-Common Object File Format-通用对象文件格式... 3 COFF的文件格式与结构体... 4 文件头... 5 numberOfSections(区 ...