您的位置:首页 > 移动开发 > Android开发

Android BroadcastReceiver实例Demo

2014-07-15 18:00 169 查看
该文主要讲解Android四大组件之一:BroadcastReceiver的初步使用,和其他三大组件一样,都需要在AndroidManifest.xml文件中注册(当然,广播注册的另一种方式是在代码中)。

实现效果图:



源代码:



布局文件:

<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/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="点击发送广播" />

</RelativeLayout>


MainActivity:

package com.android_broadcastreceiver;

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 Button button;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		button = (Button) findViewById(R.id.button);
		button.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				/**
				 * 三个广播在manifest.xml文件中的Action设置的都是myaction,所以发送广播的时候,这三个都可以收到广播
				 * 。
				 */
				intent.setAction("myaction");
				intent.putExtra("name", "梅西!!!");
				sendBroadcast(intent);
			}
		});
	}
}


MyBroadcastReceiver:

package com.android_broadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyBroadcastReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		String name = intent.getExtras().getString("name");
		Toast.makeText(context, "广播1已发送..."+name, Toast.LENGTH_SHORT).show();
	}

}


MyBroadcastReceiver2:

package com.android_broadcastreceiver;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
/**
 * 以通知的形式展示广播
 *
 */
public class MyBroadcastReceiver2 extends BroadcastReceiver {
	private NotificationManager manager;

	@Override
	public void onReceive(Context context, Intent intent) {
		manager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		String name = intent.getExtras().getString("name");

		NotificationCompat.Builder builder = new NotificationCompat.Builder(
				context);
		builder.setTicker("广播2来了!!!");
		builder.setSmallIcon(R.drawable.ic_launcher);
		builder.setContentTitle("电量不足!!!");
		builder.setContentText("电量还剩10%"+"快通知:"+name);
		manager.notify(1001, builder.build());
	}

}


MyBroadcastReceiver3:

package com.android_broadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyBroadcastReceiver3 extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		String name = intent.getExtras().getString("name");
		Toast.makeText(context, "广播3已发送..."+name, Toast.LENGTH_SHORT).show();
	}

}


AndroidManifest.xml:

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

    <uses-sdk
        android:minSdkVersion="11"
        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.android_broadcastreceiver.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="myaction" />
            </intent-filter>
        </receiver>
        <receiver android:name=".MyBroadcastReceiver2" >
            <intent-filter>
                <action android:name="myaction" />
            </intent-filter>
        </receiver>
        <receiver android:name=".MyBroadcastReceiver3" >
            <intent-filter>
                <action android:name="myaction" />
            </intent-filter>
        </receiver>
    </application>

</manifest>
比较简单,需要源码的读者可以到我的资源中下载。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: