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 ...
随机推荐
- nodejs Async 使用方法(解决多层回调嵌套)
由于nodejs是异步处理的,有时我们想同步从mysql里取出数据,最后在处理逻辑 就需要用到此扩展: 此扩展可以避免多层回调: 安装方法: npm install async 使用方法: 1.par ...
- Windows7获取、更换桌面背景,C#
使用的API原型是 BOOL SystemParametersinfo(UINT uiAction,UINT uiParam,PVOID pvParam,UINT fWinlni); 在C#中定义如下 ...
- Java之final、static关键字及匿名对象
个人通俗理解: 1.final:首先被final修饰的变量就自动变成的不能被修改的常量了.被修饰的类会自动变成太监类,只能有父类,不能有子类:被修饰的方法也不能被子类重写了:被修饰的引用变量值也不能更 ...
- let和const注意点
let 一.块级作用域 下面的代码如果使用var,最后输出的是10. var a = []; for (var i = 0; i < 10; i++) { a[i] = function () ...
- iOS 自定义读写文件
LSCacheFile.h // // LSCacheFile.h // iPhone // // Created by xujinzhong on 14-6-5. // Copyright (c) ...
- 更改IDEA默认使用JDK1.5编译项目
在使用IDEA编译项目时,总是提示我编译失败,有些功能是在1.5之后的版本才支持.废了好大的功夫才编译成功.下面呢,就是更改编译的过程了! 一.初步更改 使用快捷键Ctrl+Alt+Shift+S打开 ...
- red5 重新分配 ip
root@hett-OptiPlex-7040:~# ll /usr/local/src/red5/conf/total 144drwxr-xr-x 2 root root 4096 1月 9 ...
- fluent_python1
Magic Method python中有些跟对象本身有关的方法, 以两个下划线开始,两个下划线结束, 一般称为魔法方法(magic method). 比如 obj[key] 的背后就是 __geti ...
- Tensorflow_入门学习_1
1.0 TensorFlow graphs Tensorflow是基于graph based computation: 如: a=(b+c)∗(c+2) 可分解为 d=b+c e=c+2 a=d∗e ...
- JAVA 数据库编程中的性能优化
1. 禁止自动提交:在默认情况下,程序执行的任何sql 语句都是自动提交的向一个表中插入2000条记录,自动提交所用的时间 11666毫秒禁止自动提交(显示提交) 3450毫秒 2. 批处理:多用批 ...