Android in broadcastreceiver send broadcast information

First, the effect of the results:

Enter the information to be broadcast, and then click "Send broadcast", it can be in the Android notification of the page to see decline in broadcast form.

Second, the implementation steps:

1. First, with the establishment of the following items before the document directory: HelloBroadcast.java and MainActivity.java , which MainActivity.java control main.xml , and HelloBroadcasrReceiver.java mainly before data acquisition, and send broadcast.

2. In AndroidManifest.xml declared <application> under BroadcastReceiver , the following way (this is a static registration and dynamic registration in the java file):

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

3. Open main.xml layout file. Set the controls as follows:

<?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. OpenMainActivity.javafile, write the following code:

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. Open HelloBroadcastReceiver , deleted Activity after the inheritance BroadcastReceiver class specific code as follows:

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. The problem encountered is the package needs plus some methods, prior to use, Clean just fine.




Guess you like

Origin blog.csdn.net/HoD_DoH/article/details/53633511