1.如何创建一个自定义广播
package com.example.broadcastreceiver01;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity {
//1.定义一个Action常量
private static final String MY_ACTION = "MY_BROADCAST_ACTION";
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button)findViewById(R.id.button1);
mButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//2.实例化Intent对象
Intent intent = new Intent();
//3.设置Intent Action属性
intent.setAction(MY_ACTION);
//4.为Intent 添加附加信息
intent.putExtra("msg", "小喇叭开始广播啦~");
//5.发出广播
sendBroadcast(intent);
}});
}
}
布局文件中只有一个按钮这里就不贴出来了
2.如何接受广播
创建自定义类,然后继承BroadcastReceiver
package com.example.broadcastreceiver01;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
String m = arg1.getStringExtra("msg");
Toast.makeText(arg0, m, Toast.LENGTH_SHORT).show();
}
}
创建完接收器后,还需要在AndroidManifest.xml中声明广播接收器
<receiver android:name="MyReceiver">
<intent-filter>
<action android:name="MY_BROADCAST_ACTION" />
</intent-filter>
</receiver>
过滤器中action 的名字 就是前面定义的常量,用来声明MyReceiver自定义接收器用来接收哪个广播
*在AndroidManifest中注册的广播接收器,即使程序退出也会接受到广播。可以通过代码来注册和注销广播接收器,控制何时打开或关闭广播。
点击按钮发出广播,然后接收器接收后显示信息
下面来用代码注册和注销一个广播接收器,修改上面的代码,在布局文件中添加两个按钮,一个用来注册一个用来注销,TextView用来显示当前接收器的状态
因为是修改上面的代码,所以不能忘了把在AndroidManifest.xml中的receiver标签去掉
布局文件:
<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"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="183dp"
android:text="发出广播" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="43dp"
android:layout_toLeftOf="@+id/button1"
android:text="注册广播" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_toRightOf="@+id/button1"
android:text="注销广播" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
package com.example.broadcastreceiver01;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
public class MainActivity extends Activity {
// 1.定义一个Action常量
private static final String MY_ACTION = "MY_BROADCAST_ACTION";
private Button mButton, btnRegister, btnUnRegister;
private MyReceiver mReceiver;
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.textView1);
mTextView.setText("广播接收器已关闭");
// 注册接收器
btnRegister = (Button) findViewById(R.id.button2);
btnRegister.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
IntentFilter filter = new IntentFilter();
// 添加要接收广播的action
filter.addAction(MY_ACTION);
mReceiver = new MyReceiver();
registerReceiver(mReceiver, filter);
mTextView.setText("广播接收器已开启");
}
});
// 注销接收器
btnUnRegister = (Button) findViewById(R.id.button3);
btnUnRegister.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
unregisterReceiver(mReceiver);
mTextView.setText("广播接收器已关闭");
}
});
mButton = (Button) findViewById(R.id.button1);
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 2.实例化Intent对象
Intent intent = new Intent();
// 3.设置Intent Action属性
intent.setAction(MY_ACTION);
// 4.为Intent 添加附加信息
intent.putExtra("msg", "小喇叭开始广播啦~");
// 5.发出广播
sendBroadcast(intent);
}
});
}
}
当没有注册广播接收器是,发出广播是接收不到的
开启后,就能接收到了。