android广播BroadcastReceiver基础代码教程

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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.gb.MainActivity" >

    <Button 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:text="send"/>
</LinearLayout>

package com.example.gb;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;


public class MainActivity extends Activity {

  private MyBroadcastReceiver mBroadcastReceiver;//频道号
  	private final String action="com.example";//过滤器需要的字段(tag)
  	protected void onCreate(Bundle savedInstanceState){
  		super.onCreate(savedInstanceState);
  	setContentView(R.layout.activity_main);
  	final Activity activity=this;
  	Button button=(Button) findViewById(R.id.button);
  	
  	button.setOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent=new Intent();
			intent.setAction(action);
			//发送广播(如,某个广播电台在 action的频道发送节目)
			activity.sendBroadcast(intent);
		
			
		}
	});
	mBroadcastReceiver =new MyBroadcastReceiver();//初始化广播接收器(搞到一台收音机,开始准备接受音乐广播频道的音乐节目)
  	IntentFilter filter=new IntentFilter();//过滤器(选定音乐频道)
  	filter.addAction(action);
	this.registerReceiver(mBroadcastReceiver, filter);	//注册(旋转收音机的选台按钮,选定频道。开始接收音乐)
	}
	public void onDestroy(){
		super.onDestroy();//注销广播接收(关闭收音机)
		this.unregisterReceiver(mBroadcastReceiver);
  			//创建广播接收器(收音机待命中。。。)
		}
	private class MyBroadcastReceiver extends BroadcastReceiver{
				public void onReceive(Context context,Intent intent){
					Log.d("广播测试","收到广播");
				
			  }
        	}
}
版权声明:转载请注明出处。谢谢合作。此文最终解释权归博主所有。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio 中注册广播接收器(BroadcastReceiver)需要以下步骤: 1. 创建一个继承自 BroadcastReceiver 的类,这个类将处理接收到的广播消息。例如,你可以创建一个名为 MyBroadcastReceiver 的类。 ```java public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // 在这里处理接收到的广播消息 } } ``` 2. 在 AndroidManifest.xml 文件中声明广播接收器。在 `<application>` 标签内部添加如下代码: ```xml <receiver android:name=".MyBroadcastReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.intent.action.ACTION_NAME" /> <!-- 添加其他需要匹配的 action --> </intent-filter> </receiver> ``` 其中,`android:name` 属性指定了广播接收器的类名,`.MyBroadcastReceiver` 表示当前包名下的 MyBroadcastReceiver 类。`android:enabled` 和 `android:exported` 属性分别用于启用和导出广播接收器。 3. 在你需要发送广播的地方,创建一个 Intent 对象,并使用 `sendBroadcast()` 方法发送广播。 ```java Intent intent = new Intent(); intent.setAction("android.intent.action.ACTION_NAME"); // 添加其他需要传递的数据 sendBroadcast(intent); ``` 其中,`"android.intent.action.ACTION_NAME"` 是你自定义的广播 action,用于匹配注册的广播接收器。 这样,当发送的广播 action 与注册的广播接收器的 action 匹配时,MyBroadcastReceiver 类中的 `onReceive()` 方法将被调用,你可以在该方法中处理接收到的广播消息。记得在不需要接收广播时,及时取消注册广播接收器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值