Android 的 BroadcastReceiver

jia635發表於2014-03-09

http://developer.android.com/reference/android/content/BroadcastReceiver.html


BroadcastReceiver  分為一般廣播和有序廣播

 有序廣播中設定  priority ,值 越大,級別越高,  假如  設定的數為3 ,2,1,那麼 3 接收不到或不接收 ,2和1也就接收不到


有序廣播

  <receiver android:name=".one">
            <intent-filter android:priority="1">
                <action android:name="abc"/>
            </intent-filter>
        </receiver>
       
        <receiver android:name=".three">
            <intent-filter android:priority="3">
                <action android:name="abc"/>
            </intent-filter>
        </receiver>
       
        <receiver android:name=".two">
            <intent-filter android:priority="2">
                <action android:name="abc"/>
            </intent-filter>
        </receiver>

無序廣播的接收者不能確定誰先接收到

無須廣播的部分程式碼:

public void onReceive(Context context, Intent intent) {
  // TODO Auto-generated method stub
  String name = intent.getStringExtra("name");
  // Toast.makeText(context, "Hello !--->++"+name, 1).show();
  manger = (NotificationManager) context
    .getSystemService(Context.NOTIFICATION_SERVICE);
  
  NotificationCompat.Builder builder =new NotificationCompat.Builder(context);
  builder .setContentTitle("流量馬上就 Over 了");
  builder.setTicker("廣播來啦");
  builder.setContentText("你要掉網啦   "+name);
  builder.setSmallIcon(R.drawable.ic_launcher);
  manger.notify(1001,builder.build());
  
  

 }

 public broadcastReceiver() {
  // TODO Auto-generated constructor stub

 }




相關文章