Webview不会从之前的活动中收到getStringExtra

时间:2013-03-20 22:36:58

标签: android eclipse webview barcode getstring

我正在尝试使用条形码扫描仪然后接受该输入并在另一个活动中使用以打开网址。我已经能够返回数据,而不是在另一个活动中,并且没有看到任何完全像这样的项目。我不确定它是否与意图或我如何调用字符串有关。第二个java中的webview工作但不接受字符串。谢谢你的帮助!

Scanner.java(工作正常)

package com.pangolin.rollin.ts;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Scanner extends Activity {
TextView tvStatus;
TextView tvResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scanner);
    Button websku = (Button) findViewById(R.id.btnsku);
    websku.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Intent myintent = new Intent(Scanner.this, Websku.class);
            startActivity(myintent);
        }
    });

    tvStatus = (TextView) findViewById(R.id.tvStatus);
    tvResult = (TextView) findViewById(R.id.tvResult);

    Button scanBtn = (Button) findViewById(R.id.btnScan);

    // in some trigger function e.g. button press within your code you
    // should add:
    scanBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // TODO Auto-generated method stub

            try {

                Intent intent = new Intent(
                        "com.google.zxing.client.android.SCAN");
                intent.putExtra("SCAN_MODE", "QR_CODE_MODE,PRODUCT_MODE");
                startActivityForResult(intent, 0);

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "ERROR:" + e, Toast.LENGTH_LONG)
                        .show();

            }

        }
    });

}

// In the same activity you’ll need the following to retrieve the results:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {

        if (resultCode == RESULT_OK) {
            tvStatus.setText(intent.getStringExtra("SCAN_RESULT_FORMAT"));
            tvResult.setText(intent.getStringExtra("SCAN_RESULT"));
        } else if (resultCode == RESULT_CANCELED) {
            tvStatus.setText("Press a button to start a scan.");
            tvResult.setText("Scan cancelled.");
        }
    }
}

}

websku.java(不起作用,应该从之前的活动中获取结果。

package com.pangolin.rollin.ts;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class Websku extends Activity {
final Activity activity = this;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    String sku = intent.getStringExtra("SCAN_RESULT");
    this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
    setContentView(R.layout.activity_websku);
    WebView webView = (WebView) findViewById(R.id.webview_sku);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
            activity.setTitle("Loading...");
            activity.setProgress(progress * 100);

            if (progress == 100)
                activity.setTitle(R.string.title_activity_websku);
        }
    });

    webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {
            // Handle the error
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });

    webView.loadUrl("http://m.radioshack.com/radioshack/catalog/searchList.do?categoryId=&keyword="+sku);

    };
}

2 个答案:

答案 0 :(得分:0)

您不需要为Websku意图设置任何额外内容:

websku.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        Intent myintent = new Intent(Scanner.this, Websku.class);
        startActivity(myintent);
    }
});

应该是:

websku.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        Intent myintent = new Intent(Scanner.this, Websku.class);
        myintent.putExtra("somename", somevalue);
        startActivity(myintent);
    }
});

答案 1 :(得分:0)

您没有为websku活动设置额外内容。保存从扫描仪返回的意图:

private Intent mWebskuIntent;
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        mWebskuIntent = intent;
        // more of your code

然后当你启动websku Activity时,制作一份保存的意图副本,该意图也会复制从扫描仪返回的额外内容:

Intent myintent = new Intent(mWebskuIntent);
myintent.setClass(Scanner.this, Websku.class);
startActivity(myintent);

您可能还希望检查mWebskuIntent是否为空。