<span style="font-size:18px;">
</span><span style="font-size:18px;">动态广播:</span><span style="font-size:18px;">
</span><span style="font-size:18px;">package com.example.boradcast;
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.View;
import android.widget.Button;
public class MainActivity extends Activity {
private MyBroadcastReceiver mBroadcastReceiver;
//频道号
private final String chanel="com.";//过滤器需要的字段(tag)
@Override
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) {
//发送广播(比如,某个广播电台在Chanel的频道发送节目)
Intent intent=new Intent();
intent.setAction(chanel);
activity.sendBroadcast(intent);
}
});
//初始化广播接收器(搞到一台收音机,开始准备接收音乐广播频道的音乐节目)
mBroadcastReceiver=new MyBroadcastReceiver();
//过滤器(选定音乐频道)
IntentFilter filter=new IntentFilter();
filter.addAction(chanel);
//注册(旋转收音机的选台按钮,选定频道。开始接收音乐)
registerReceiver(mBroadcastReceiver, filter);
}
@Override
public void onDestroy(){
super.onDestroy();
//注销广播接收(关闭收音机)
unregisterReceiver(mBroadcastReceiver);
}
//创建广播接收器(收音机待命中。。。)
private class MyBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Log.d("广播测试","收到广播!");
}
}
}
</span>
静态广播:
<span style="font-size:18px;">package com.hx.gb;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private final String action = "com.xx";//过滤器需要的字段(tag)
@Override
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 view) {
//发送广播
Intent intent = new Intent();
intent.setAction(action);
activity.sendBroadcast(intent);
}
});
}
}</span><span style="font-size:18px;">package com.hx.gb;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
//创建广播接收器
public class MyBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent ) {
Log.d("广播测试","收到广播!");
}
}
</span><span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hx.gb"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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>
<span style="color:#ff0000;"><receiver
android:name="com.hx.gb.MyBroadcastReceiver"
>
<intent-filter>
<action android:name="com.xx" >
</action>
</intent-filter>
</receiver></span>
</application>
</manifest>
</span>
本文深入探讨了Android系统中动态广播与静态广播的工作原理、实现方式及应用场景,包括发送广播、注册广播接收器、过滤器配置等关键步骤。
3385

被折叠的 条评论
为什么被折叠?



