Android中broadcastreceiver发送广播信息

一、结果效果:

输入要广播的信息,然后点击“发送广播”,可以在在Android的下滑页面看见广播形式的通知。

二、实现步骤:

1.首先同之前文档所述建立如下项目目录:HelloBroadcast.javaMainActivity.java,其中MainActivity.java控制main.xml,而HelloBroadcasrReceiver.java主要是取得之前的数据,并发送广播。

2.AndroidManifest.xml中<application>下声明BroadcastReceiver,具体方式如下(这是静态注册,动态注册在java文件中):

        <receiver android:name=".HelloBroadcastReceiver">
        	<intent-filter>
        		<action android:name="com.HoD.action.BroadcastReceiverTest" />
        	</intent-filter>
        </receiver>

3.打开main.xml布局文件。设置控件,如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    
    <EditText android:id="@+id/et_broadcastContent"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="请输入广播内容" /> 
            
    <Button android:id="@+id/btn_sendBroadcast"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:text="发送广播" />   
</LinearLayout> 

4.打开MainActivity.java文件,编写如下代码:

package com.HoD.broadcastreceiver;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	//因为步骤操作相对较多,故取得对象实例化信息与操作数据分开写,则需要存入变量
	private Context mContext;
	private Button btnSendBroadcast;
	private TextView etBroadcastContent;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		//
		mContext = this;
		
		//获取Button实例化对象,并设置监听事件
		btnSendBroadcast = (Button) findViewById(R.id.btn_sendBroadcast);
		btnSendBroadcast.setOnClickListener(new SendBroadcastClickListener());
		
		//获取TextView的实例化对象
		etBroadcastContent = (TextView) findViewById(R.id.et_broadcastContent);
		
	}
	
	//声明SendBroadcastClickListener类使用OnClickListener接口
	private class SendBroadcastClickListener implements OnClickListener{
		@Override
		public void onClick(View v){
			//获取文本框内内容
			String content =  etBroadcastContent.getText().toString().trim();
			if(content.length() < 1){
				//取出里面“Hint”字符串用Toast方法显示
				Toast.makeText(mContext, etBroadcastContent.getHint(), 1).show();
				return;
			}
			Intent intent = new Intent();
			//运行HelloBroadcatReceiver
			intent.setAction("com.HoD.action.BroadcastReceiverTest");
			intent.putExtra("content", content);
			sendBroadcast(intent);
			
		}
	}
	
}

5.打开HelloBroadcastReceiver,删掉Activity后,继承BroadcastReceiver类,具体代码如下:

package com.HoD.broadcastreceiver;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;


public class HelloBroadcastReceiver extends BroadcastReceiver{
	
	private Context context;
	
	@Override
	//使用onReceive()方法接收数据
	public void onReceive(Context context, Intent intent){
		this.context = context;
		showNotification(intent);
	}
	
	private void showNotification(Intent intent){
		//通过getSystemService()方法获取NotificationManager服务
		NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		
		//创建一个Notification对象,并为其设置各种属性
		//获取传递的图标、数据和当时的系统时间
		//这种方式已经过时,不过先以体会思想为主
		Notification notification = new Notification(R.drawable.ic_launcher, intent.getExtras().getString("content"), System.currentTimeMillis());
		
		//该语句的作用是定义了一个不是当即显示的activity,只有当用户拉下notify显示列表,并且单击对应的项的时候,才会触发系统跳转到该activity
		PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
		
		//notification.setLatestEventInfo(this, title, content, contentIntent);在此处设置在nority列表里的该norifycation得显示情况。
		notification.setLatestEventInfo(context, intent.getExtras().getString("content"), null, pendingIntent);
		
		//通过NotificationManager类的notify()方法将通知发送到状态栏
		notificationManager.notify(R.layout.main, notification);
	}
	
}

6.遇到的问题就是包需要再加上一些,使用之前的方法,Clean一下就好了。




猜你喜欢

转载自blog.csdn.net/HoD_DoH/article/details/53633511