Android学习笔记之广播意图及广播接收者MyBroadcastReceiver、Broadcast

(1)第一种使用xml文件进行注册

布局文件,添加一个button点击的时候进行广播

<RelativeLayout 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=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="68dp"
        android:layout_marginTop="82dp"
        android:text="Button" />

</RelativeLayout>

MainActivity.java

package com.lc.broadcastreceiver_demo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.example.broadcastreceiver_demo.R;

/*
 * 使用xml文件注册的广播接受者,需要在清单文件中注册
 * <receiver android:name="com.lc.broadcastreceiver_demo.MyBroadcastReceiver" >
            <intent-filter>
                <action android:name="com.freedie.broadcast" />
            </intent-filter>
        </receiver>
 */
public class MainActivity extends Activity {

	private Button button;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button = (Button) this.findViewById(R.id.button1);
		/*
		 * 当按钮点击的时候
		 */

		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (R.id.button1 == v.getId()) {
					Intent intent = new Intent();
					intent.setAction("com.freedie.broadcast");
					sendBroadcast(intent); // 发送广播意图
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

MyBroadcastReceiver.java广播的接受者,要继承BroadcastReceiver类,并且重载onReceive方法:

package com.lc.broadcastreceiver_demo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/*
 * 广播接受者,当广播接受者受到广播意图的时候,onReceive回调方法就会执行
 */
public class MyBroadcastReceiver extends BroadcastReceiver {

	private static final String TAG = "MyBroadcastReceiver";

	@Override
	public void onReceive(Context context, Intent intent) {
		if ("com.freedie.broadcast".equals(intent.getAction())) {
			Log.d(TAG, "com.freedie.broadcast Intent was receiverd!");
		}
	}

}


在清单文件中要注册一个广播:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.broadcastreceiver_demo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.lc.broadcastreceiver_demo.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- 对MyBroadcastReceiver进行相关注册 -->
        <receiver android:name="com.lc.broadcastreceiver_demo.MyBroadcastReceiver" >
            <intent-filter>
                <action android:name="com.freedie.broadcast" />
            </intent-filter>
        </receiver>
    </application>

</manifest>


(2)第二种使用Java代码来注册

布局文件不变(含有一个button当点击的时候进行广播)、清单文件不许操作、MyBroadcastReceiver.java文件不变,只适用于测试是否接收成功!:

MainActivity.java

package com.example.broadcastreceiver_javacode;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

	private Button button;
	private MyBroadcastReceiver myBroadcastReceiver;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button = (Button) this.findViewById(R.id.button1);
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (R.id.button1 == v.getId()) {
					Intent intent = new Intent();
					intent.setAction("com.freedie.brodcast");
					sendBroadcast(intent); // 发送广播
				}
			}
		});
	}

	/*
	 * 在这里注册一个广播程序
	 */
	@Override
	protected void onResume() {
		super.onResume();
		myBroadcastReceiver = new MyBroadcastReceiver();
		// 意图过滤器
		IntentFilter filter = new IntentFilter();
		filter.addAction("com.freedie.brodcast");
		// 对广播接受者进行注册
		registerReceiver(myBroadcastReceiver, filter);
	}

	/*
	 * 对广播接受者进行注销
	 */
	@Override
	protected void onStop() {
		super.onStop();
		unregisterReceiver(myBroadcastReceiver);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}



你可能感兴趣的:(广播意图及广播接收者)