蓝牙发送通知
上一篇介绍了设置通知取消提示的流程,本篇来介绍蓝牙有文件需要接收时,如何发送通知的。
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.