我有一个WebActivity
为JavascriptInterface
设置WebView
,在接口上我创建了各种方法,使用Javascript调用不同的任务,其中一个是发送通过蓝牙的文件。
该文件发送得很好,但我想告知用户该文件何时已被接受并在另一端完全传输。
我正在尝试在我的主WebActivity上使用onActivityResult
,并尝试使用处理蓝牙传输的单独类,并且在JavascriptInterface上调用了一个方法,但是在文件发出后我没有得到任何回调结果成功发送:
//首次尝试
public class WebActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
//some code here
//Here I set the interface
myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == 0) {
//This message is shown after the file transfer has started but no
//finished
Toast.makeText(this, "File Sent", Toast.LENGTH_SHORT).show();
if(resultCode == Activity.RESULT_OK){
//It doesn't work here either I always get Activity.RESULT_CANCELED (0)
//flag afer the file transfer has started
}
}
}
}
//第二次尝试
public class BluetoothTasks {
//code here
private void startTransferIntent() {
//some more code here
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
File file_to_transfer = new File(Environment.getExternalStorageDirectory() +
"/txtTransfer.txt");
intent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(file_to_transfer));
//some more code here
Activity act = (Activity) mContext;
act.startActivityForResult(intent, TRANSFER_RESULT);
}
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == TRANSFER_RESULT) {
//Not working here either
//code that is not executed
}
}
}
你能告诉我是否有办法做到这一点,如果有的话,怎么办呢?谢谢。