本文主要是介绍广播 收音机,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
<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)@Overrideprotected 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() {@Overridepublic 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);}@Overridepublic void onDestroy(){super.onDestroy();//注销广播接收(关闭收音机)unregisterReceiver(mBroadcastReceiver);}//创建广播接收器(收音机待命中。。。)private class MyBroadcastReceiver extends BroadcastReceiver{@Overridepublic 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)@Overrideprotected 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() {@Overridepublic 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{@Overridepublic 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-sdkandroid:minSdkVersion="15"android:targetSdkVersion="15" /><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid: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>
这篇关于广播 收音机的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!