收到彩信二

接收彩信2.png

收到通知,返回通知確認

收到彩信通知

下載彩信

下載彩信

1.onReceive

packages\apps\Mms\src\com\android\mms\transaction\PushReceiver.java

public void onReceive(Context context, Intent intent) {
  if (intent.getAction().equals(WAP_PUSH_DELIVER_ACTION) && ContentType.MMS_MESSAGE.equals(intent.getType())) {
    // Hold a wake lock for 5 seconds, enough to give any
    // services we start time to take their own wake locks.
    PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
    //PARTIAL_WAKE_LOCK:Wake lock level: Ensures that the CPU is running; the screen and keyboard backlight will be allowed 
    //to go off.
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                                            "MMS PushReceiver");
    wl.acquire(5000);
    new ReceivePushTask(context).execute(intent);
  }
}

2.doInBackground

packages\apps\Mms\src\com\android\mms\transaction\PushReceiver.java

private class ReceivePushTask extends AsyncTask<Intent,Void,Void> {
  protected Void doInBackground(Intent... intents) {
    Intent intent = intents[0];
    // Get raw PDU push-data from the message and parse it
    byte[] pushData = intent.getByteArrayExtra("data"); //獲得數(shù)據(jù)
    PduParser parser = new PduParser(pushData, PduParserUtil.shouldParseContentDisposition());
    GenericPdu pdu = parser.parse(); //解析成pdu

    PduPersister p = PduPersister.getPduPersister(mContext);
    ContentResolver cr = mContext.getContentResolver();
    int type = pdu.getMessageType();
    long threadId = -1;

    switch (type) {
      case MESSAGE_TYPE_NOTIFICATION_IND: {
        NotificationInd nInd = (NotificationInd) pdu;
        if (!isDuplicateNotification(mContext, nInd)) { //不存在相同的通知
          // Save the pdu. If we can start downloading the real pdu immediately,
          // don't allow persist() to create a thread for the notificationInd
          // because it causes UI jank.
          //如果是自動下載彩信的颓帝,這里存入數(shù)據(jù)庫的時候不創(chuàng)建threadId
          Uri uri = p.persist(pdu, Inbox.CONTENT_URI, !NotificationTransaction.allowAutoDownload(),
                                    MessagingPreferenceActivity.getIsGroupMmsEnabled(mContext), null);

          // Start service to finish the notification transaction.
          Intent svc = new Intent(mContext, TransactionService.class);
          svc.putExtra(TransactionBundle.URI, uri.toString());
          svc.putExtra(TransactionBundle.TRANSACTION_TYPE, Transaction.NOTIFICATION_TRANSACTION);
          mContext.startService(svc);
        }
      }
    }
  }
}

3.onStartCommand

packages\apps\Mms\src\com\android\mms\transaction\TransactionService.java

public int onStartCommand(Intent intent, int flags, int startId) {
  if (intent != null) {
    Message msg = mServiceHandler.obtainMessage(EVENT_NEW_INTENT);
    msg.arg1 = startId;
    msg.obj = intent;
    mServiceHandler.sendMessage(msg);
  }
  return Service.START_NOT_STICKY;
}

4.handleMessage

packages\apps\Mms\src\com\android\mms\transaction\TransactionService.java

private final class ServiceHandler extends Handler {
  public void handleMessage(Message msg) {
    Transaction transaction = null;
    switch (msg.what) {
      case EVENT_NEW_INTENT:
        onNewIntent((Intent)msg.obj, msg.arg1);
        break;
    }
  }
}

5.onNewIntent

packages\apps\Mms\src\com\android\mms\transaction\TransactionService.java

public void onNewIntent(Intent intent, int serviceId) {
  String action = intent.getAction();
  if (ACTION_ONALARM.equals(action) || ACTION_ENABLE_AUTO_RETRIEVE.equals(action) ||
                (intent.getExtras() == null)) {

  } else{//彩信通知走這里
    // For launching NotificationTransaction and test purpose.
    TransactionBundle args = new TransactionBundle(intent.getExtras());
    launchTransaction(serviceId, args, noNetwork);
  }
}

6.launchTransaction

packages\apps\Mms\src\com\android\mms\transaction\TransactionService.java

private void launchTransaction(int serviceId, TransactionBundle txnBundle, boolean noNetwork) {
  if (noNetwork) {
    Log.w(TAG, "launchTransaction: no network error!");
    onNetworkUnavailable(serviceId, txnBundle.getTransactionType());
    return;
  }
  Message msg = mServiceHandler.obtainMessage(EVENT_TRANSACTION_REQUEST);
  msg.arg1 = serviceId;
  msg.obj = txnBundle;

  if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) {
    Log.v(TAG, "launchTransaction: sending message " + msg);
  }
  mServiceHandler.sendMessage(msg);
}

7.handleMessage

packages\apps\Mms\src\com\android\mms\transaction\TransactionService.java

private final class ServiceHandler extends Handler {
  public void handleMessage(Message msg) {
    Transaction transaction = null;
    switch (msg.what) {
      case EVENT_TRANSACTION_REQUEST:
        int transactionType = args.getTransactionType();
        switch (transactionType) {
          case Transaction.NOTIFICATION_TRANSACTION:
            String uri = args.getUri();
            if (uri != null) {
              transaction = new NotificationTransaction(TransactionService.this, serviceId, transactionSettings, uri);
            }
        }
  
        if (!processTransaction(transaction)) {
          transaction = null;
          return;
        }
    }
  }
}

8.processTransaction

packages\apps\Mms\src\com\android\mms\transaction\TransactionService.java

private boolean processTransaction(Transaction transaction) throws IOException {
  transaction.attach(TransactionService.this);
  transaction.process();
  return true;
}

9.process

packages\apps\Mms\src\com\android\mms\transaction\NotificationTransaction.java

public void process() {
  new Thread(this, "NotificationTransaction").start();
}

10.run

packages\apps\Mms\src\com\android\mms\transaction\NotificationTransaction.java

public void run() {
  DownloadManager downloadManager = DownloadManager.getInstance();
  boolean autoDownload = allowAutoDownload(); //是否自動下載
  try {
    // By default, we set status to STATUS_DEFERRED because we
    // should response MMSC with STATUS_DEFERRED when we cannot
    // download a MM immediately.
    int status = STATUS_DEFERRED;
    // Don't try to download when data is suspended, as it will fail, so defer download
    if (!autoDownload) { //如果不是自動下載
      downloadManager.markState(mUri, DownloadManager.STATE_UNSTARTED);
      sendNotifyRespInd(status); //發(fā)送  M-NotifyResp.ind
      return;
    }
    downloadManager.markState(mUri, DownloadManager.STATE_DOWNLOADING);
    byte[] retrieveConfData = null;
    // We should catch exceptions here to response MMSC
    // with STATUS_DEFERRED.
    try {
      retrieveConfData = getPdu(mContentLocation); //下載彩信
    } catch (IOException e) {
      mTransactionState.setState(FAILED);
    }

    if (retrieveConfData != null) {
      //解析
      GenericPdu pdu = new PduParser(retrieveConfData, PduParserUtil.shouldParseContentDisposition()).parse();
      if ((pdu == null) || (pdu.getMessageType() != MESSAGE_TYPE_RETRIEVE_CONF)) {//如果下載的類型不對
        mTransactionState.setState(FAILED);
        status = STATUS_UNRECOGNIZED;
      } else{ //下載到正確的M-Retrieve.conf pdu
        // Save the received PDU (must be a M-RETRIEVE.CONF).
        PduPersister p = PduPersister.getPduPersister(mContext);
        //存儲彩信
        Uri uri = p.persist(pdu, Inbox.CONTENT_URI, true, MessagingPreferenceActivity.getIsGroupMmsEnabled(mContext), null);
        // We have successfully downloaded the new MM. Delete the
        // M-NotifyResp.ind from Inbox.
        //刪除通知   M-Notification.ind
       SqliteWrapper.delete(mContext, mContext.getContentResolver(), mUri, null, null);

        mUri = uri;
        status = STATUS_RETRIEVED;
      }
    }
    sendNotifyRespInd(status); //發(fā)送  M-NotifyResp.ind
  } finally {
    mTransactionState.setContentUri(mUri);
    if (!autoDownload) {
      // Always mark the transaction successful for deferred
      // download since any error here doesn't make sense.
      mTransactionState.setState(SUCCESS);
    }
    if (mTransactionState.getState() != SUCCESS) {
      mTransactionState.setState(FAILED);
      Log.e(TAG, "NotificationTransaction failed.");
    }
    notifyObservers(); //執(zhí)行TransactionService的update
  }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末褐筛,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌卧土,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,188評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件像樊,死亡現(xiàn)場離奇詭異尤莺,居然都是意外死亡,警方通過查閱死者的電腦和手機凶硅,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評論 3 395
  • 文/潘曉璐 我一進店門缝裁,熙熙樓的掌柜王于貴愁眉苦臉地迎上來扫皱,“玉大人足绅,你說我怎么就攤上這事『裕” “怎么了氢妈?”我有些...
    開封第一講書人閱讀 165,562評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長段多。 經(jīng)常有香客問我首量,道長,這世上最難降的妖魔是什么进苍? 我笑而不...
    開封第一講書人閱讀 58,893評論 1 295
  • 正文 為了忘掉前任加缘,我火速辦了婚禮,結(jié)果婚禮上觉啊,老公的妹妹穿的比我還像新娘拣宏。我一直安慰自己,他們只是感情好杠人,可當我...
    茶點故事閱讀 67,917評論 6 392
  • 文/花漫 我一把揭開白布勋乾。 她就那樣靜靜地躺著宋下,像睡著了一般。 火紅的嫁衣襯著肌膚如雪辑莫。 梳的紋絲不亂的頭發(fā)上学歧,一...
    開封第一講書人閱讀 51,708評論 1 305
  • 那天,我揣著相機與錄音各吨,去河邊找鬼枝笨。 笑死,一個胖子當著我的面吹牛揭蜒,可吹牛的內(nèi)容都是我干的伺帘。 我是一名探鬼主播,決...
    沈念sama閱讀 40,430評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼忌锯,長吁一口氣:“原來是場噩夢啊……” “哼伪嫁!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起偶垮,我...
    開封第一講書人閱讀 39,342評論 0 276
  • 序言:老撾萬榮一對情侶失蹤张咳,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后似舵,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體脚猾,經(jīng)...
    沈念sama閱讀 45,801評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,976評論 3 337
  • 正文 我和宋清朗相戀三年砚哗,在試婚紗的時候發(fā)現(xiàn)自己被綠了龙助。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,115評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡蛛芥,死狀恐怖提鸟,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情仅淑,我是刑警寧澤称勋,帶...
    沈念sama閱讀 35,804評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站涯竟,受9級特大地震影響赡鲜,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜庐船,卻給世界環(huán)境...
    茶點故事閱讀 41,458評論 3 331
  • 文/蒙蒙 一银酬、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧筐钟,春花似錦揩瞪、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽北发。三九已至,卻和暖如春喷屋,著一層夾襖步出監(jiān)牢的瞬間琳拨,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評論 1 272
  • 我被黑心中介騙來泰國打工屯曹, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留狱庇,地道東北人扭勉。 一個月前我還...
    沈念sama閱讀 48,365評論 3 373
  • 正文 我出身青樓记某,卻偏偏與公主長得像,于是被迫代替她去往敵國和親愕够。 傳聞我的和親對象是個殘疾皇子偷俭,可洞房花燭夜當晚...
    茶點故事閱讀 45,055評論 2 355