广播Broadcast,收发步骤
来源:互联网 发布:怎么在淘宝上买彩票了 编辑:程序博客网 时间:2024/05/21 09:27
转载请注明出处:http://blog.csdn.net/mr_leixiansheng/article/details/53583161
步骤:
1、新建自己的广播类继承BroadcastReceiver并重写onReceive(一旦广播接收到设定接收的信息就会启动onReceive)
2、在Manifest中对广播注册声明(静态注册,此处就可以设置广播接收的信息)
3、主程序中发送要接受的信息,触发广播机制
1、自定义广播
package com.example.leixiansheng.test3;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.widget.Toast;/** * Created by Leixiansheng on 2017/3/15. */public class MyBroadcast extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String str = intent.getExtras().get("CONTENT").toString(); Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); }}2、注册
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.leixiansheng.test3"> <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=".MyBroadcast"> <intent-filter> <action android:name="com.example.leixiansheng.test3.MyBroadcast"/> </intent-filter> </receiver> </application></manifest>
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.leixiansheng.test3.MainActivity"> <Button android:id="@+id/btn" android:text="发送广播" android:layout_width="match_parent" android:layout_height="wrap_content" /></RelativeLayout>
package com.example.leixiansheng.test3;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity { private Button send; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); send = (Button) findViewById(R.id.btn); send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent("com.example.leixiansheng.test3.MyBroadcast"); intent.putExtra("CONTENT","这是广播内容"); sendBroadcast(intent); } }); }}
0 0
- 广播Broadcast,收发步骤
- broadcast广播
- Broadcast广播
- broadcast 广播
- Broadcast广播
- broadcast广播
- Broadcast广播
- 广播 (Broadcast)
- 广播Broadcast
- Broadcast广播
- Broadcast广播
- broadcast广播
- Broadcast广播
- Broadcast 广播
- 2016年9月2号-心情很差--收发广播Broadcast,两个广播同时发出的接收与本项目发出广播,另一个项目接收广播
- Android-Broadcast广播事件(1)-简介及普通广播调用步骤
- Broadcast广播的使用
- android--broadcast(广播)
- Linux权限管理
- SQL SERVER复习提纲
- codeforces contest 349
- 判断一个字符是否为数字
- UML系列图——用例图(Use Case)
- 广播Broadcast,收发步骤
- 轮询程序中使用的各种方法:spring调用;http调用;存储过程调用;
- 初识PHP(5)
- 四大金融资产管理公司的起起伏伏
- 编译mrcp的教训
- 淘宝网商品SKU系统设计经验分享
- Spark-1.3.1与Hive整合实现查询分析
- OAF 系列教程 二 HelloWorld
- Elasticsearch 权威指南(中文版)