我正在尝试将URL传递给我从QR码获得的WebView,但我不知道如何做到这一点,我在意图中尝试了putExtras但是当我扫描QR码并按下时访问按钮应用程序返回main_activity。
如何传递网址,我从QR码转到WebView
这里的代码,我附加了URL ... QR码包含/图像文字...... 当我附加URL时,它变为http://google.com/images...i想要将此URL传递给WebView
我还想在网址加载时显示进度条我该怎么做?
Scan.java
public class Scan extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private static final int REQUEST_CAMERA = 1;
private ZXingScannerView scannerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
scannerView = new ZXingScannerView(this);
setContentView(scannerView);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if(checkPermission()){
Toast.makeText(Scan.this,"Permission is granted",Toast.LENGTH_SHORT).show();
}else {
requestPermission();
}
}
}
private boolean checkPermission(){
return (ContextCompat.checkSelfPermission(Scan.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED);
}
private void requestPermission(){
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA},REQUEST_CAMERA);
}
@Override
protected void onDestroy() {
super.onDestroy();
scannerView.stopCamera();
}
@Override
protected void onResume() {
super.onResume();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if(checkPermission()){
if(scannerView == null){
scannerView = new ZXingScannerView(this);
setContentView(scannerView);
}
scannerView.setResultHandler(this);
scannerView.startCamera();
}else {
requestPermission();
}
}
}
@Override
public void handleResult(Result result) {
final String scanResult = "http://google.com" + result.getText();
Web web = new Web(scanResult);
//final String appendResult = "http://google.com" + scanResult;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Scan Result");
/* builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// scannerView.resumeCameraPreview(Scan.this);
setContentView(R.layout.activity_main);
finish();
}
});*/
builder.setPositiveButton("Visit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(Scan.this, WebResult.class);
intent.putExtra("title",scanResult);
startActivity(intent);
// Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(scanResult));
// startActivity(intent);
}
});
builder.setMessage(scanResult);
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
web_result.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="example.android.com.qrscannertest.WebResult">
<WebView
android:id="@+id/web"
android:layout_width="match_parent"
android:layout_height="match_parent"></WebView>
</RelativeLayout>
WebResult.java
public class WebResult extends AppCompatActivity {
Web web;
@Override
protected void onCreate(Bundle savedInstanceState) {
final WebView webView = (WebView)findViewById(R.id.web);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_result);
Bundle extras = getIntent().getExtras();
String url = null;
if(extras != null){
url = extras.getString("url");
}
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished( WebView view, String url ) {
super.onPageFinished(webView, url );
//Make Enable or Disable buttons
// backbtn.setEnabled(view.canGoBack());
//fwdbtn.setEnabled(view.canGoForward());
}
@Override
public void onReceivedError( WebView view, int errorCode, String description, String failingUrl ) {
super.onReceivedError( webView, errorCode, description, failingUrl );
Toast.makeText( WebResult.this, description, Toast.LENGTH_LONG );
}
});
webView.loadUrl(url);
}
}
答案 0 :(得分:1)
先执行 setContentView()
,然后执行 findViewById
使用此
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_result);
webView = (WebView)findViewById(R.id.web);
而不是这个
@Override
protected void onCreate(Bundle savedInstanceState) {
final WebView webView = (WebView)findViewById(R.id.web);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_result);
修改强>
在使用像
这样的意图传递数据时也使用相同的密钥使用此
url = extras.getString("title");
而不是
url = extras.getString("url");
要使用webview显示 ProgressBar
,请检查此How to add a progress/loading bar in WebView
答案 1 :(得分:0)
你放置了密钥title
并获得密钥url
,无论如何,这将如何运作。
String url = null;
if(extras != null){
url = extras.getString("title");
}
首先调用setContentView()
然后找到视图。