实训7 BroadcastReceiver广播接收器类
一、【实训目的】
(1) BroadcastReceiver广播接收器类的实现步骤
(2) 有序广播的实际应用
(3)无序广播的应用
-
【实训内容和步骤】
1、创建一个无序广播,广播发送器发送一个信息:“我是FM广播台”,广播接收器接收到信息,并用弹出对话框显示接收到的信息。
实训步骤:
<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:orientation="vertical"
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/sendBroadcast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sendBroadcast" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">haha</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="sendBroadcast">发送广播</string>
</resources>
MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button sendBroadcast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Intent intent=new Intent();
intent.setAction("com.example.haha.MyBroadcastReceiver");
sendBroadcast=(Button)findViewById(R.id.sendBroadcast);
sendBroadcast.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
sendBroadcast(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
MyBroadcastReceiver.java
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class MyBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "我是FM广播台",
Toast.LENGTH_LONG).show();
}
}
AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.haha"
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.example.haha.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>
<receiver android:name=".MyBroadcastReceiver" >
<intent-filter>
<action android:name="com.example.haha.MyBroadcastReceiver" />
</intent-filter>
</receiver>
</application>
</manifest>
运行结果截图:
2、创建一个有序广播,有1,2,3,4,5广播接收器,广播发送器发送一个信息:“我是有序广播”,按照顺序每个广播接收器接收到信息,并用弹出对话框显示接收到的信息。
实训步骤:
MainActivity.java
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 sendBroadcast,sortedBroadcast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Intent intent=new Intent();
intent.setAction("com.example.ww.MyBroadcastReceiver");
MyBroadcastReceiver myBroadcastReceiver=new MyBroadcastReceiver();
IntentFilter intentFilter=new IntentFilter("com.example.ww.MyBroadcastReceiver");
registerReceiver(myBroadcastReceiver, intentFilter);
sendBroadcast=(Button)findViewById(R.id.sendBroadcast);
sortedBroadcast=(Button)findViewById(R.id.sortedBroadcast);
sendBroadcast.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
sendBroadcast(intent);
}
});
sortedBroadcast.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
sendOrderedBroadcast(intent, null);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ww"
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.example.ww.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>
<activity
android:name="com.example.ww.MyBroadcastReceiver"
android:label="@string/app_name">
</activity>
<receiver android:name=".ABroadcastReceiver" >
<intent-filter>
<action android:name="com.example.ww.MyBroadcastReceiver" />
</intent-filter>
</receiver>
<receiver android:name=".BBroadcastReceiver" >
<intent-filter>
<action android:name="com.example.ww.MyBroadcastReceiver" />
</intent-filter>
</receiver>
<receiver android:name=".CBroadcastReceiver" >
<intent-filter>
<action android:name="com.example.ww.MyBroadcastReceiver" />
</intent-filter>
</receiver>
<receiver android:name=".DBroadcastReceiver" >
<intent-filter>
<action android:name="com.example.ww.MyBroadcastReceiver" />
</intent-filter>
</receiver>
<receiver android:name=".EBroadcastReceiver" >
<intent-filter>
<action android:name="com.example.ww.MyBroadcastReceiver" />
</intent-filter>
</receiver>
</application>
</manifest>
运行结果截图:
附注:该专栏是博主上学时的实训项目,可供访客练习与参考。代码质量不是很好,但能实现,仅供参考!