BroadcastReceiver广播接收者(四)——本地广播LocalBroadcastManager以及在onReceive()中弹出Dialog

MainActivity如下:
package cc.cv;

import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
/**
 * Demo描述:
 * 1 本地广播LocalBroadcast使用完整示例
 * 2 在广播的onReceive()方法中弹出Dialog
 * 
 * 本地广播的特点:
 * 1 本地广播只能在本App中传播,并且广播接收器也只能接收来自本APP发出的广播.
 *   所以在该情况下可防止其他应用获取广播中的敏感数据从而确保数据安全.
 *   
 * 2 本地广播只能使用在代码中动态注册的方式.
 *   因为静态注册主要是为了让程序在未启动的情况下也能收到广播.在发送本地广播
 *   时,我们的App肯定是已经启动了,因此也完全不需要使用静态注册.否则,这个本地
 *   广播也没什么特点了.
 * 
 * 
 * 注意事项:
 * 在广播的onReceive()方法中弹出Dialog
 * 1 给Dialog设置Type
 *   dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
 * 2 注意权限
 *   <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
 *  
 *  
 * 参考资料:
 * http://blog.csdn.net/guolin_blog
 * Thank you very much
 */
public class MainActivity extends Activity {
	private Button mButton;
	private Intent mIntent;
	private Context mContext;
	private IntentFilter mIntentFilter;
    private LocalBroadcastManager mLocalBroadcastManager;
	private LocalBroadcastReceiver mLocalBroadcastReceiver;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}
	
	private void init(){
		mContext=this;
		mButton=(Button) findViewById(R.id.button);
		mButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				registerLocalBroadcastReceiver();
				sendLocalBroadcast();
			}
		});
	}
	
	
	/**
	 * 注册本地广播接收者
	 */
	private void registerLocalBroadcastReceiver(){
		mIntentFilter=new IntentFilter("test_action");
		mLocalBroadcastReceiver=new LocalBroadcastReceiver();
		mLocalBroadcastManager=LocalBroadcastManager.getInstance(mContext);
		mLocalBroadcastManager.registerReceiver(mLocalBroadcastReceiver, mIntentFilter);
	}
	
	/**
	 * 发送本地广播
	 */
	private void sendLocalBroadcast(){
		mIntent=new Intent("test_action");
		mIntent.putExtra("number", "9527");
		mLocalBroadcastManager.sendBroadcast(mIntent);
		//发送广播后取消本地广播的注册
		unRegisterLocalBroadcastReceiver();
	}
	
	/**
	 * 取消本地广播的注册
	 */
	private void unRegisterLocalBroadcastReceiver(){
		if (mLocalBroadcastManager!=null) {
			if (mLocalBroadcastReceiver!=null) {
				mLocalBroadcastManager.unregisterReceiver(mLocalBroadcastReceiver);
			}
		}
	}
	
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		unRegisterLocalBroadcastReceiver();
	}

}


LocalBroadcastReceiver如下:

package cc.cv;

import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.WindowManager;

public class LocalBroadcastReceiver extends BroadcastReceiver{
	@Override
	public void onReceive(Context context, Intent intent) {
		String message=intent.getStringExtra("number");
		showDialogInBroadcastReceiver(context, message);
	}
	
	
	/**
	 * 在广播的onReceive()方法中弹出Dialog
	 * 1 给Dialog设置Type
	 *  dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
	 * 2 注意权限
	 *  <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
	 */
	private void showDialogInBroadcastReceiver(Context context,String message){
		Builder builder=new Builder(context);  
        builder.setIcon(R.drawable.ic_launcher);  
        builder.setTitle("Title");  
        builder.setMessage(message);  
        builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {  
            @Override  
            public void onClick(DialogInterface dialog, int arg1) {
            	dialog.dismiss();
            }  
        });  
        Dialog dialog=builder.create();
        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        dialog.show();  
	}

}

main.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"
   >

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="发送本地广播" />

</RelativeLayout>


你可能感兴趣的:(BroadcastReceiver广播接收者(四)——本地广播LocalBroadcastManager以及在onReceive()中弹出Dialog)