Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android廣播接收機制詳細介紹(附短信接收實現)

Android廣播接收機制詳細介紹(附短信接收實現)

編輯:關於Android編程

Android中廣播(BroadcastReceiver)的詳細講解.

1. BroadcastReceiver的注冊過程:
(1).廣播消息發出來後,只有訂閱了該廣播的對象才會接收發出來的廣播消息並做出相應處理。
**(2).**Android廣播分為兩個方面:廣播發送者和廣播接收者。Android中的廣播使用了觀察者模式,基於消息的發布/訂閱事件模型。廣播接收者通過Binder機制向AMS進行注冊,AMS查找符合相應條件的Broadcastreceiver,將廣播發送到BroadcastReceiver(一般情況下是Activity)相應的消息循環隊列中。消息循環執行拿到此廣播,回調BroadcastReceiver中的onReceive()方法。
(3).廣播發送者和廣播接收者的執行順序是異步的,發出去的廣播不會關心有無接收者的接收,也不確定接收者是什麼時候能接收到。
2. BroadcastReceiver的步驟:
(1).注冊廣播:廣播接收者在AMS中注冊廣播。
(2).發送廣播:廣播發送者向AMS中發送廣播。
(3).接收廣播:廣播接收者拿到廣播後調用onReceive()方法執行。

以下是一個發送短信的例子,代碼如下:

BroadcastReceiverHelper 類:

package com.scd.broadcastreceiver.helper;

import com.scd.broadcastreceiver.activity.MainActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

/**
 * 廣播接收
 * 
 * @author scd
 * 
 */
public class BroadcastReceiverHelper extends BroadcastReceiver {
  /** 上下文 */
  private MainActivity mActivity = null;

  public BroadcastReceiverHelper(MainActivity mContext) {
    super();
    mActivity = mContext;
  }

  @Override
  public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(MainActivity.ACTION_SENDMESSAGE)) {
      toast("發送成功");
    } else if (intent.getAction()
        .equals(MainActivity.ACTION_DELIVERMESSAGE)) {
      toast("接收成功");
    }

  }

  public void toast(String text) {
    Toast.makeText(mActivity, text, Toast.LENGTH_SHORT).show();

  }

}


MainActivity類:

package com.scd.broadcastreceiver.activity;

import com.scd.broadcastreceiver.R;
import com.scd.broadcastreceiver.helper.BroadcastReceiverHelper;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
  private Button mButton = null;
  /** 發送短信 */
  public static String ACTION_SENDMESSAGE = "com.scd.broadcastreceiver.MainActivity.SendMessage";
  /** 接收短信 */
  public static String ACTION_DELIVERMESSAGE = "com.scd.broadcastreceiver.MainActivity.DeliverMessage";
  /** Reciver類 */
  private BroadcastReceiverHelper mHelperS = null;
  private BroadcastReceiverHelper mHelperD = null;
  /** 短信管理類 */
  private SmsManager mSmsManager = null;

  /** 電話號碼 */
  private String mTelephone = "13607567010";

  /** 短信內容 */
  private String mContext = "你好,有什麼需要幫助的嗎?";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mButton = (Button) findViewById(R.id.sendBroadcast);
    mButton.setOnClickListener(this);
    mSmsManager = SmsManager.getDefault();
    // 注冊廣播
    registerBroadcast();
  }

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
    case R.id.sendBroadcast: {
      //發送廣播
      sendBroadcastReceiver(mTelephone, mContext);
      break;
    }

    default:
      break;
    }

  }

  /**
   * 注冊廣播
   */
  public void registerBroadcast() {
    // 發送
    IntentFilter intentFilterS = new IntentFilter(ACTION_SENDMESSAGE);
    mHelperS = new BroadcastReceiverHelper(MainActivity.this);
    this.registerReceiver(mHelperS, intentFilterS);
    // 接收
    IntentFilter intentFilterD = new IntentFilter(ACTION_DELIVERMESSAGE);
    mHelperD = new BroadcastReceiverHelper(MainActivity.this);
    this.registerReceiver(mHelperD, intentFilterD);
  }

  /**
   * 發送廣播 短信發送的過程:發送的時候需要發送廣播一次,接收的時候需要發送廣播一次
   */
  public void sendBroadcastReceiver(String telephone, String content) {
    if (telephone != null) {
      Intent sIntent = new Intent(ACTION_SENDMESSAGE);
      // 短信成功發送後才發送該廣播
      PendingIntent sentIntent = PendingIntent.getBroadcast(
          MainActivity.this, 0, sIntent, 0);

      Intent dIntent = new Intent(ACTION_DELIVERMESSAGE);
      // 短信接收後才發送該廣播
      PendingIntent deliveryIntent = PendingIntent.getBroadcast(
          MainActivity.this, 0, dIntent, 0);
      mSmsManager.sendTextMessage(telephone, null, content, sentIntent,
          deliveryIntent);
      // this.sendBroadcast(intent);

    }

  }
}

注意:需要添加以下權限:

 

 <!-- 短信權限 -->
  <uses-permission android:name="android.permission.SEND_SMS" >
  </uses-permission>
  <uses-permission android:name="android.permission.READ_SMS" >
  </uses-permission>
  <uses-permission android:name="android.permission.RECEIVE_SMS" >
  </uses-permission>

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved