Android: Bluetooth file transfer and more pop and show progress

A Bluetooth File Transfer pop

  When the native Android Bluetooth file transfer, Bluetooth file will pop up box and a lack of people received notification is the default form displayed in the status bar when the user clicks will pop up a dialog. Then when the status bar is disabled, how to achieve full acceptance of documents without user clicks automatically receive it?

1. How to let a user click on the status bar direct bomb confirmation dialog?

 In BluetoothOppNotification.java of updateIncomingFileConfirmNotification () method will accept the incoming file certain treatment will also construct a Notification, to receive and display information denied, then the solution ideas here.

 Private  void updateIncomingFileConfirmNotification () { 

 // omitted several ... 

 the Intent Intent = new new the Intent (Constants.ACTION_INCOMING_FILE_CONFIRM); // this rather critical, a pass action to the BluetoothOppReceiver 

 intent.setClassName (Constants.THIS_PACKAGE_NAME, BluetoothOppReceiver. class .getName ()) ; 

 intent.setDataAndNormalize (contentURI);

 After constructing the intent here is not to send out a broadcast, but after construction notification below, click on the radio when sent out, so to solve the problem on the spot here. If the user does not need to directly display the status bar interface file reception confirmation and rejection may be directly here mContext.sendBroadcast (intent); broadcast sent

//省略若干…

{

//构造notification

 Notification n = new Notification();

 n.icon = R.drawable.bt_incomming_file_notification;

 n.flags |= Notification.FLAG_ONLY_ALERT_ONCE;

 n.flags |= Notification.FLAG_ONGOING_EVENT;

 n.defaults = Notification.DEFAULT_SOUND;

 n.tickerText = title;

 n.when = timeStamp;

 n.color = mContext.getResources().getColor(

 com.android.internal.R.color.system_notification_accent_color);

 n.setLatestEventInfo(mContext, title, caption, PendingIntent.getBroadcast(mContext, 0,

 intent, 0));

 intent = new Intent(Constants.ACTION_HIDE);

 intent.setClassName(Constants.THIS_PACKAGE_NAME, BluetoothOppReceiver.class.getName());

 intent.setDataAndNormalize(contentUri);

 n.deleteIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);//用户点击之后将广播发送出去

 mNotificationMgr.notify(id, n);

 }

 }

}

 

2. The user does not click a button to confirm the file to receive documents directly on how to receive?

 Continued above, when the broadcast is sent in BluetoothOppReceiver.java start BluetoothOppIncomingFileConfirmActivity directly. For further processing in this activity.

 It can be seen that in this activity is mainly the above mentioned configuration files received acknowledgment and rejection dialog.

 To achieve the desired effect, the code receive button events only need to confirm the relocation can be. It can be performed directly in the mobile oncreate, after the expiry of the dialog dismiss out. Mainly as a few codes

if (!mTimeout) {

 // Update database

 mUpdateValues = new ContentValues();

 mUpdateValues.put(BluetoothShare.USER_CONFIRMATION,

 BluetoothShare.USER_CONFIRMATION_CONFIRMED);

 this.getContentResolver().update(mUri, mUpdateValues, null, null);

 Toast.makeText(this, getString(R.string.bt_toast_1), Toast.LENGTH_SHORT).show();

 }

 

3. How to display a progress bar?

 When the above documents will need to begin to accept the pop-up progress bar displays the progress, so in the above code also need to add the code to start the progress bar interface:

 Intent in = new Intent(this, BluetoothOppTransferActivity.class);

 in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

 in.setDataAndNormalize(mUri);

 this.startActivity(in);

 So far, single-file transfer and has started to show a progress bar.

 

4. After the file transfer is complete, the progress bar interface how to disappear automatically after three seconds?

 Enter BluetoothOppTransferActivity this activity, first of all define a method to disappear:

private void dismissNowDialog(){

 new Handler().postDelayed(new Runnable() {

 @Override

 public void run() {

 dismiss();

 }

 }, 2000);

 }

 After re-setUpDialog () calls this method when you can == DIALOG_RECEIVE_COMPLETE_FAIL in mWhichDialog == DIALOG_RECEIVE_COMPLETE_SUCCESS and mWhichDialog.

 I write to you, the whole single-file file transfer does not require any user clicks you can automatically receive completed.

 But I do not know you have not thought about a problem, after accepting finished dismiss out the interface, then those files back if the progress bar will be displayed when multiple file transfer it? The answer is no.

 

How to display a progress bar all file transfers 5. multi-file?

  The idea is, when no amount of file transfer, a single file transfer is completed, the status bar displays progress information will be updated other files, consider here, continue to enter BluetoothOppNotification.java this class, you can see updateActiveNotification () method in multiple files transmission, which is carried out by Notification.Builder refresh the display, our demand is not the case, so these desirable. Read on to see a focus Intent intent = new Intent (Constants.ACTION_LIST); this can be understood as handle multiple files. Native code is not a good way to distinguish between multiple files or a single file, so you need to find ways to be processed here. When I do see this was very excited, I thought that it is not very simple, single-file and transfer exactly the same I just need to send it again broadcast manually. Under the results will make you collapse, here it simply, if ten files so that he would be broadcast in the transmission times it? The end result is the back of the overlap interface plus stop flashing. So here have to do is send this broadcast only once during file transfer, but there is no existing method or variable to indicate whether multiple file transfers.

 

6. How will broadcast according to their needs just send it out once in a loop?

  I thought here is the definition of a variable of any type, given an initial value, to find a way when a document is received will certainly calls, change the value of face in this method, after the expiry of plus when sending broadcast determine this variable, after the expiry of the value of a variable return the default value. The next, he certainly will not send out a broadcast to ensure that broadcast only sent once, you can achieve demand.

Int TEMP 0 = Private ; 

 Private  void updateActiveNotification () { 

...... 

 IF (TEMP ==. 1) { // only sent once this is determined by the guaranteed broadcast 

 mContext.sendBroadcast (Intent); 

 TEMP = TEMP +. 1 ; 

 } 

...... 

} 

rivate void updateIncomingFileConfirmNotification () { 

// this method add the following code 

  IF (TEMP ==. 1 ) { 

 TEMP = TEMP +. 1 ; 

 return ; 

 } 

...... 

TEMP =. 1 ; 

}

 At this point, the entire demand for processed. When asked if you want to show how much is completed after receiving the file transfer is complete, the number of file transfer fails, then you can pass in the code BluetoothOppTransferActivity.java to process information in dynamically change the display dialog, note that there is not in this class know how many files have been transferred and failures, requires from BluetoothOppNotification.java of updateCompletedNotification () method,

int outboundSuccNumber = 0;

 int outboundFailNumber = 0;

 int outboundNum;

 int inboundNum;

 int inboundSuccNumber = 0;

 int inboundFailNumber = 0;

 Optional parameters transmitted or stored, so that the above said interface is displayed.

 Finally come the end of a little knowledge. I wonder if there is no thought, Bluetooth file transfer How to determine when a file is being transferred or have been transferred it? After providing native Bluetooth code for pairing Bluetooth, connectivity status, and the state does not provide file transfer, you will need to do it yourself. The idea is Bluetooth file transfer is carried out by the stream, so I just need to know if it corresponds Liu close to know whether the file transfer is complete.

 In the framework \ base \ obex \ javax \ obex ServerSession below a category, which will pass judgment on this ObexTransport; InputStream OutputStream determines whether or closed, where their external interface to add, to determine whether Bluetooth file transfer is complete, easier.

 

Guess you like

Origin www.cnblogs.com/blogs-of-lxl/p/11769433.html