Android 取消蓝牙消息通知流程分析(二)

蓝牙发送通知

上一篇介绍了设置通知取消提示的流程,本篇来介绍蓝牙有文件需要接收时,如何发送通知的。
Android 中蓝牙的应用是在packages\apps\Bluetooth中(我本来以为在frameworks中),当通过蓝牙配对后,接收到文件传输的广播时,packages/apps/Bluetooth/src/com/android/bluetooth/opp/BluetoothOppHandoverReceiver.java对广播进行处理:

BluetoothOppManager.getInstance(context).saveSendingFileInfo(finalType,
                            finalUris, true);
BluetoothOppManager.getInstance(context).startTransfer(device);

接下来依次调用 startTransfer -> InsertShareInfoThread.run -> insertSingleShare /insertMultipleShare -> BluetoothOppProvider.insert 。
在insert中,把文件uri保存到数据库,并唤起BluetoothOppService。依次调用BluetoothOppService.onStartCommand -> updateFromProvider -> UpdateThread.run -> BluetoothOppNotification.updateNotification -> NotificationUpdateThread.run:

@Override
        public void run() {
   
            Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
            synchronized (BluetoothOppNotification.this) {
   
                if (mUpdateNotificationThread != this) {
   
                    throw new IllegalStateException(
                            "multiple UpdateThreads in BluetoothOppNotification");
                }
                mPendingUpdate = 0;
            }
            updateActiveNotification();//1
            updateCompletedNotification();//2
            updateIncomingFileConfirmNotification();//3
            synchronized (BluetoothOppNotification.this) {
   
                mUpdateNotificationThread = null;
            }
        }

标记1、2、3依次更新三种通知,这个会根据数据库中记录的文件状态来决定是否执行,如果不是对应的状态就直接返回。比如有文件发送过来,标记1、2处的函数都是直接返回的,真正执行的是标记3 updateIncomingFileConfirmNotification:


    private void updateIncomingFileConfirmNotification() {
   
        Cursor cursor = mContext.getContentResolver().query(BluetoothShare.CONTENT_URI, null,
                WHERE_CONFIRM_PENDING, null, BluetoothShare._ID);//1

        if (cursor == null) {
   
            return;
        }

        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
   
          BluetoothOppTransferInfo info = new BluetoothOppTransferInfo();
          BluetoothOppUtility.fillRecord(mContext, cursor, info);
          Uri contentUri = Uri.parse(BluetoothShare.CONTENT_URI + "/" + info.mID);
          Intent baseIntent = new Intent().setDataAndNormalize(contentUri)
              .setClassName(Constants.THIS_PACKAGE_NAME, BluetoothOppReceiver.class.getName());

          Notification n = new Notification.Builder(mContext)
              .setOnlyAlertOnce(true)
              .setOngoing(true)
              .setVibrate(new long[] {
    200 })
              .setWhen(info.mTimeStamp)
              .setDefaults(Notification.DEFAULT_SOUND)//2
              .setPriority(Notification.PRIORITY_HIGH)//3
              .addAction(R.drawable.ic_decline,
                  mContext.getText(R.string.incoming_file_confirm_cancel),
                      PendingIntent.getBroadcast(mContext, 0,
                          new Intent(baseIntent).setAction(Constants.ACTION_DECLINE), 0))
              .addAction(R.drawable.ic_accept,
                  mContext.getText(R.string.incoming_file_confirm_ok),
                      PendingIntent.getBroadcast(mContext, 0,
                          new Intent(baseIntent).setAction(Constants.ACTION_ACCEPT), 0))
              .setContentIntent(PendingIntent.getBroadcast(mContext, 0,
                  new Intent(baseIntent).setAction(Constants.ACTION_INCOMING_FILE_CONFIRM), 0))//4
              .setDeleteIntent(PendingIntent.getBroadcast(mContext, 0,
                  new Intent(baseIntent).setAction(Constants.ACTION_HIDE), 0))
              .setColor(mContext.getResources().getColor(
                  com.android.internal.R.color.system_notification_accent_color))
              .setContentTitle(mContext.getText(R.string.incoming_file_confirm_Notification_title))
              .setContentText(info.mFileName)
              .setStyle(new Notification.BigTextStyle().bigText(mContext.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值