android的粘性广播,是指广播接收器一注册马上就能接收到广播的一种机制,当然首先系统要存在广播。而普通广播就是要先注册广播接收器,然后广播被发送到系统,广播接收器才能接收到广播。
所以他们的区别是:
粘性广播调用registerReceiver能马上接受广播,而普通广播不行。
对于粘性广播:
1.系统首先存在粘性广播
sendStickyBroadcast(Intent intent)
2.注册广播接收器
registerReceiver(BroadcastReceiver receiver, IntentFilter filter)
3.处理广播
public void onReceive(android.content.Context context, Intent intent) {
};
下面用一个例子展示下他们的区别
主Acitivity
package com.example.stickybroadcastdemo;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
Button registerStickbt;
Button registerNormalbt;
TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendStickyBroadcast(new Intent("com.example.stickybroadcastdemo.stickybrocast")); //发送粘性广播
sendBroadcast(new Intent("com.example.stickybroadcastdemo.normalbrocast")); //发送普通广播
registerStickbt=(Button)findViewById(R.id.registerstick);
registerNormalbt=(Button)findViewById(R.id.registernormal);
mTextView=(TextView)findViewById(R.id.result);
registerStickbt.setOnClickListener(this);
registerNormalbt.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.registerstick:
registerReceiver(stickreceiver, new IntentFilter("com.example.stickybroadcastdemo.stickybrocast")); //注册粘性广播接收器
break;
case R.id.registernormal:
registerReceiver(normalreceiver, new IntentFilter("com.example.stickybroadcastdemo.normalbrocast"));//注册普通广播接收器
break;
default:
break;
}
}
BroadcastReceiver stickreceiver=new BroadcastReceiver(){ //粘性广播接收器
public void onReceive(android.content.Context context, Intent intent) {
mTextView.setText("recevier stick broadcast!");
};
};
BroadcastReceiver normalreceiver=new BroadcastReceiver(){ //普通广播接收器
public void onReceive(android.content.Context context, Intent intent) {
mTextView.setText("recevier normal broadcast!");
};
};
}
布局
<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="com.example.stickybroadcastdemo.MainActivity" >
<Button
android:id="@+id/registerstick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="注册粘性广播" />
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="36dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/registernormal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/registerstick"
android:layout_below="@+id/registerstick"
android:layout_marginTop="17dp"
android:text="注册普通广播" />
</RelativeLayout>
布局有两个按钮,一个是注册粘性广播,一个是注册普通广播。点击注册粘性广播按钮会马上返回结果。而点击注册普通广播按钮则没有反应