Android学习笔记之Broadcast Receiver
可程序间通信
注册通信,注销通信,发送消息
package com.jiahemeikang.helloandroid; import com.jiahemikang.service.EchoService;
import com.jiahemikang.service.EchoService.EchoServiceBingder;
import com.jikexuyua.broadcastreceiver.MyBC; import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener,ServiceConnection{ private TextView tvResult ;
private Intent serviceIntent;
private Button btnStarAty1;
private Button btnStarService;
private Button btnStopService; private Button btnBingService;
private Button btnUnBingService;
private Button btnGetNum;
private Button btnbroadcast;
private Button regbroadcastButton;
private Button btnunregbroadcastButton;
public EchoService echoService = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
serviceIntent = new Intent(this,EchoService.class);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvResult = (TextView)findViewById(R.id.tvResult); btnStarAty1= (Button)findViewById(R.id.btnStartAty1);
btnStarService= (Button)findViewById(R.id.btnStartService);
btnStopService= (Button)findViewById(R.id.btnStopService); btnStarService.setOnClickListener(this);
btnStopService.setOnClickListener(this); btnBingService= (Button)findViewById(R.id.bingdingservice);
btnUnBingService= (Button)findViewById(R.id.unbingdingservice);
btnBingService.setOnClickListener(this);
btnUnBingService.setOnClickListener(this); btnGetNum= (Button)findViewById(R.id.btnGetNum);
btnGetNum.setOnClickListener(this); btnbroadcast= (Button)findViewById(R.id.btnbroadcast);
btnbroadcast.setOnClickListener(this); regbroadcastButton= (Button)findViewById(R.id.btnregbroadcast);
regbroadcastButton.setOnClickListener(this);
btnunregbroadcastButton= (Button)findViewById(R.id.btnunregbroadcast);
btnunregbroadcastButton.setOnClickListener(this); btnStarAty1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){ Intent i = new Intent(MainActivity.this,Aty1.class);
i.putExtra("txt","Hello aty1");
startActivity(i);
startActivityForResult(i,);
}
}); } @Override
protected void onActivityResult(int a,int b ,Intent i){ if (i!=null) {
String result = i.getStringExtra("result"); tvResult.setText(result);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} private final MyBC myBC = new MyBC();
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnStartService:
startService(serviceIntent);
break;
case R.id.btnStopService:
stopService(serviceIntent);
break;
case R.id.bingdingservice:
bindService(serviceIntent,this,Context.BIND_AUTO_CREATE);
break;
case R.id.unbingdingservice:
unbindService(this);
echoService =null;
break;
case R.id.btnGetNum:
if (echoService!=null) {
System.out.print("当前服务的 数字为"+echoService.getCountNum());
}
break;
case R.id.btnbroadcast:
Intent i = new Intent(MyBC.ACTION);
i.putExtra("txt", "消息");
sendBroadcast(i);
break;
case R.id.btnregbroadcast: registerReceiver(myBC, new IntentFilter(MyBC.ACTION));
break;
case R.id.btnunregbroadcast:
unregisterReceiver(myBC);
break;
default:
break;
}
} @Override
public void onServiceConnected(ComponentName arg0, IBinder binder) {
System.out.println("onServiceConnected");
echoService = ((EchoService.EchoServiceBingder)binder).getService(); } @Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub }
//onStart }
broadcast继承BroadcastReceiver
package com.jikexuyua.broadcastreceiver; import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent; public class MyBC extends BroadcastReceiver { public static final String ACTION = "com.jikexuyua.broadcastreceiver.intent.action.MyBC";
public MyBC() {
// TODO Auto-generated constructor stub
} @Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
System.out.println("OnReceiver data"+arg1.getStringExtra("txt"));
} }
Android学习笔记之Broadcast Receiver的更多相关文章
- Android学习笔记--广播(Broadcast)
1.Android广播分类 android的广播类型分为两类:标准广播和有序广播. 标准广播:异步广播,广播发出后,所有注册了的广播接收器都会同时接收到该广播.打个比方:做地铁过程中的语音播报,当列车 ...
- 【转】 Pro Android学习笔记(七五):HTTP服务(9):DownloadManager
目录(?)[-] 小例子 保存在哪里下载文件信息设置和读取 查看下载状态和取消下载 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件,转载须注明出处:http://blog.csd ...
- 【转】Pro Android学习笔记(十二):了解Intent(下)
解析Intent,寻找匹配Activity 如果给出component名字(包名.类名)是explicit intent,否则是implicit intent.对于explicit intent,关键 ...
- 【转】Pro Android学习笔记(十):了解Intent(上)
目录(?)[-] Intent基本含义 系统的Intent Android引入了Intent的概念来唤起components,component包括:1.Activity(UI元件) 2.Servic ...
- 【转】Pro Android学习笔记(九八):BroadcastReceiver(2):接收器触发通知
文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.sina.com.cn/flowingflying或作者@恺风Wei-傻瓜与非傻瓜 广播接 ...
- Pro Android学习笔记(一三七):Home Screen Widgets(3):配置Activity
文章转载仅仅能用于非商业性质,且不能带有虚拟货币.积分.注冊等附加条件.转载须注明出处http://blog.csdn.net/flowingflying/以及作者@恺风Wei. 通过widget定义 ...
- Android学习笔记:Home Screen Widgets(2):关于Widget
通过widget定义,我们在widget列表中看到了我们的TestWidget.当我们拖拽widget到主页时,假设在appwidet-provider中定义了android:configure的ja ...
- Android 学习笔记之Volley(七)实现Json数据加载和解析...
学习内容: 1.使用Volley实现异步加载Json数据... Volley的第二大请求就是通过发送请求异步实现Json数据信息的加载,加载Json数据有两种方式,一种是通过获取Json对象,然后 ...
- Android学习笔记进阶之在图片上涂鸦(能清屏)
Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...
随机推荐
- 【读书笔记】C Primer Plus ch.15位运算 示例程序15.1 整数转换成二进制字符串
正文: https://www.zybuluo.com/RayChen/note/595213
- JS调用OC方法
- (void)myMethod:(CDVInvokedUrlCommand*)command { NSString* echo = [command.arguments objectAtIndex: ...
- cordova sqlite
jar包在这里下载 https://github.com/litehelpers/Cordova-sqlite-storage 把SQLitePlugin 复制到自己工程目录 org.pgsqlite ...
- ibatis resultMap 结果集映射
1.结果集映射 就是将返回的记录,逐个字段映射到java对象上:如果数据库字段与java对象的成员变量名对应的话,则使用resultClas即可 2.实现 结合 ibatis初探这篇文章中提到的pro ...
- IIS express 7.5 设置默认文档
在C:\Users\[电脑用户名]\Documents\IISExpress\config 下面有个applicationhost.config文件,打开文件找到<system.webServe ...
- Webkit之资源加载
一.webkit资源分类 webkit中有多种资源,大致分为以下几种: HTML文本 CSS样式文本 - CachedCSSStyleSheet 字体 - CachedFont 图片 - Cached ...
- C: define many functions using predefine..
/* Defines COUNTER. There must be exactly one such definition at file scope * within a program. */ # ...
- C语言版的16进制与字符串互转函数
http://www.cnblogs.com/nio-nio/p/3309367.html /* // C prototype : void StrToHex(BYTE *pbDest, BYTE * ...
- sonar tomacat配置
最近在学习Sonar,配置了好几天,才搭建起来环境,为自己的学习能力感到汗颜,赶紧在此记录一下,所谓好记性不如烂笔头. 1.Sonar介绍 Sonar是一个用于代码质量管理的开源平台,用于管理Java ...
- ubuntu 12.04 安装snort acidbase相关注意事项
一.安装Snort 1.安装libpcap 1 apt-get install libpcap-dev 2.安装snort 1 2 apt-get install snort apt-get inst ...