代码注册广播接收器
来源:互联网 发布:为什么还原网络设置 编辑:程序博客网 时间:2024/04/30 14:41
注册广播接收器的方式:
代码:
在AndroidMainfest.xml中增加修改后的代码
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.haige.broatcast"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".MyReceiver1" android:enabled="true" android:exported="true"> <intent-filter><!--增加的代码--> <action android:name="com.codingke.action.MY_BROLOCAST" /> </intent-filter> <!--增加的代码--> </receiver> <receiver android:name=".MyReceiver2" android:enabled="true" android:exported="true"></receiver> </application></manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="match_parent" android:weightSum="1"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送一个普通的广播" android:id="@+id/button_normal" android:onClick="sendNormalClick" android:layout_alignParentTop="true" android:layout_alignParentEnd="true" /></RelativeLayout>
MainActivity.java
package com.example.haige.broatcast;import android.app.Activity;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.view.View;public class MainActivity extends Activity { private MyReceiver2 receiver2=new MyReceiver2(); protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /* 在该方法中进行广播注册 */ @Override protected void onResume() { super.onResume(); IntentFilter filter=new IntentFilter(); filter.addAction("com.codingke.action.MY_BROLOCAST"); registerReceiver(receiver2,filter); } /* 在该方法中进行解除注册 */ @Override protected void onPause() { super.onPause(); unregisterReceiver(receiver2); } //发送一个普通的广播 public void sendNormalClick(View v) { Intent intent=new Intent("com.codingke.action.MY_BROLOCAST");//在AndroidMainfest中修改的代码 intent.putExtra("info","我们很有缘,终于对接了"); this.sendBroadcast(intent); }}MyReceiver1.java
package com.example.haige.broatcast;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;public class MyReceiver1 extends BroadcastReceiver { public MyReceiver1() { } /* 自定义的广播接收器 */ @Override public void onReceive(Context context, Intent intent) { String info=intent.getStringExtra("info"); Toast.makeText(context,info,Toast.LENGTH_SHORT).show(); }}
MyReceiver2.java
package com.example.haige.broatcast;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;public class MyReceiver2 extends BroadcastReceiver { public MyReceiver2() { } @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context,"动态注册的广播接收器",Toast.LENGTH_SHORT).show(); }}
总结:运行截图简陋不发了,当点击Button时会分别出现两条悬浮广播信息,一秒后又消失。
0 0
- 代码注册广播接收器
- 广播接收器动态注册
- 动态注册广播接收器
- 静态注册广播接收器
- 静态注册广播接收器
- Broadcast Receiver(二)用代码注册广播接收器
- android 注册静态广播接收器VS注册动态广播接收器
- 【Android】动态注册广播接收器
- 【Android】一步注册广播接收器
- 【Android】动态注册广播接收器
- 【Android】动态注册广播接收器
- android动态注册广播接收器
- Android动态注册广播接收器
- 广播接收器 发送与注册
- 广播接收器动静态注册
- android 单元测试 注册广播接收器
- 创建广播接收器并注册广播
- BroadcastReceiver静态注册广播与动态注册广播接收器
- shell按行读取文件的3种方法
- IOS runtime简单使用
- vim跳到文件头和文末结尾
- bzoj1612(dfs)
- BLE CC2541 DMA串口查询方式环形缓冲区的建立 与函数指针 回掉函数的使用
- 代码注册广播接收器
- 第二次CCF-4-无线网络(二维最短路-spfa)
- 注意Hibernate4在开发当中的一些改变
- 进程通讯(命名管道)
- C++中变量命名规则
- TabLayout+ViewPager
- Android Dialog 提示框基本用法
- 页面登录cookie<在程序里写关于在浏览器页面上显示访问错误3面后返回登录面>
- ScrollView不满全屏留白问题