Android——接收广播(静态注册)
来源:互联网 发布:淘宝卖家退货骗局 编辑:程序博客网 时间:2024/05/01 21:55
静态注册广播是针对整个手机,只要你安装了此app,它都可以接收到发来的广播,并
不是仅仅局限于一个Activity,一个应用内部。
静态注册需要将发送方和接收方各自创建一个Android项目:
发送方:
只需要发送广播sendBroadcast(Intent),通过参数intent设置广播的action标识就好
啦。
接收方:
1.一个类继承broadcastreceiver
2.mainifest中注册
MyReceiver类继承broadcastreceiver:
public class MyReceiver extends BroadcastReceiver{ /* * 1.在receiver app中创建继承broadcastreceiver类 * 2.在mainifest文件中注册register标签注册该类,设置intentfilter的action与另一个send app的setaction相对应 * * 3.创建send类 发送广播 * */ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub //context 整个app 不局限于Activity }}
Manifest中注册:
<receiver android:name="com.example.receivertest.MyReceiver"> <intent-filter > <action android:name="lpl"/> </intent-filter> </receiver>
这里action就是“lpl”。在这里就不用像动态注册那样解注册了。
简单介绍之后,来试着做这样一个项目:
发广播的app发送一个广播,接广播的app收到此广播以后改变自己的背景颜色。
大致流程就是send广播的app——>收广播的app的receiver类(MyReceiver的
onreceive方法)——>该app的mainactivity。
这里需要注意一个问题:
需要先运行接收的app,再运行发送广播的app,虽然说静态注册的广播,是针对整个手
机的,接收的app是肯定能够接收到的,但是改变它Activity的背景颜色,必须在它的
Activity创建出来以后才可以设置有效,不然颜色肯定是改不了的。
效果图看一下:
未发送广播时的接收广播的app:
收到广播后的此app:
附代码:
SendBoradcast的app:
import android.app.Activity;import android.content.Intent;import android.graphics.Color;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void myclick(View v) { Intent intent = new Intent("lpl"); intent.putExtra("color", Color.RED); sendBroadcast(intent); }}
通过按钮来发送广播。
接收方app:
MyReceive类:
import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.graphics.Color;import android.widget.Toast;public class MyReceiver extends BroadcastReceiver{ /* * 1.在receiver app中创建继承broadcastreceiver类 * 2.在mainifest文件中注册register标签注册该类,设置intentfilter的action和另一个send * app的setaction相对应 * 3.创建send类 发送广播 * */ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub //context 整个app 不局限于Activity int color=intent.getIntExtra("color", Color.RED); Intent intent1=new Intent("ll"); intent1.putExtra("color", color); context.sendBroadcast(intent1); Toast.makeText(context, "收到广播", 0).show(); }}
在Manifest中设置注册:
<receiver android:name="com.example.receivertest.MyReceiver"> <intent-filter > <action android:name="lpl"/> </intent-filter> </receiver>
在application标签内,activity标签外设置。
mainactivity:
import android.app.Activity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.graphics.Color;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.RelativeLayout;public class MainActivity extends Activity { private RelativeLayout layout; private BroadcastReceiver receiver=new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub layout.setBackgroundColor(intent.getIntExtra("color", Color.RED)); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); layout=(RelativeLayout) findViewById(R.id.layout); IntentFilter filter=new IntentFilter(); filter.addAction("ll"); registerReceiver(receiver, filter); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); unregisterReceiver(receiver); }}
将布局设置一个id,通过布局对象来设置颜色。
- Android——接收广播(静态注册)
- Android——接收广播(动态注册)
- 【Android成长之路】全局大喇叭——广播机制的浅谈(接收系统广播与动、静态注册接收器)
- Android之广播机制—静态注册
- android 静态注册广播
- Android中的静态系统广播与动态系统广播及其相应的注册与接收
- Android广播之注册广播(包括静态广播和动态广播的注册)源码分析
- Android 接收系统发送的广播,动态注册和静态注册
- android 静态注册广播注意事项
- #android# 广播静态注册 动态注册
- android静态注册的广播在某些手机上无法正常接收
- 静态注册的广播每次接收广播都会重新生成一个接收广播的对象
- android 代码方式注册广播接收着
- Android 动态注册接收发送广播
- Android中广播注册与接收流程
- Android静态广播接收不到(app之间数据传递)
- android 注册静态广播接收器VS注册动态广播接收器
- Android静态注册广播和动态注册广播的区别
- 【LeetCode题解】203.Remove Linked List Elements
- 数据让生意更简单,网聚宝创业团队利用数加快速打造核心业务竞争力,在激烈的市场竞争中弯道超车。
- oracle 导入导出dmp文件
- laravel 5.2 注册登录(自带) php artisan make:auth
- linux守护进程形式运行java程序
- Android——接收广播(静态注册)
- java获得当前文件路径
- Swift 3:新的访问控制fileprivate和open
- 阿里云数加大数据计算服务MaxCompute学习路线图 (持续更新中)
- yii2高级版安装分享
- HTML5 拖放图片
- Druid、BoneCP、DBCP、C3P0等主流数据库对比
- html代开QQ和微信的href
- FTP服务器原理