简体   繁体   English

如何为 Vcard 打开新的 Activity 以及如何打开 Google Maps 应用程序?

[英]How to open new Activity for Vcard and how to open Google Maps app?

I am making a qr code scanner app and I can't complete two things.我正在制作一个二维码扫描仪应用程序,但我无法完成两件事。 The first one is to pass information about Vcard to a new Activity.第一个是将有关Vcard的信息传递给新的 Activity。 Here is the code I wrote:这是我写的代码:

@Override
public void handleResult(Result rawResult) {
    processRawResult(rawResult.getText());
}

private void processRawResult(String text){
    if (text.startsWith("BEGIN:")) {
        String[] tokens = text.split("\n");
        QRVcardModel qrVcardModel = new QRVcardModel();
        for (int i = 0; i < tokens.length; i++) {
            if (tokens[i].startsWith("BEGIN:")) {
                qrVcardModel.setType(tokens[i].substring("BEGIN:".length()));

            } else if (tokens[i].startsWith("N:")) {
                qrVcardModel.setName(tokens[i].substring("N:".length()));

            } else if (tokens[i].startsWith("ORG:")) {
                qrVcardModel.setOrg(tokens[i].substring("ORG:".length()));

            } else if (tokens[i].startsWith("TEL:")) {
                qrVcardModel.setTel(tokens[i].substring("TEL:".length()));

            } else if (tokens[i].startsWith("URl:")) {
                qrVcardModel.setUrl(tokens[i].substring("URL:".length()));


            } else if (tokens[i].startsWith("EMAIL:")) {
                qrVcardModel.setEmail(tokens[i].substring("EMAIL:".length()));

            } else if (tokens[i].startsWith("ADR:")) {
                qrVcardModel.setAddress(tokens[i].substring("ADR:".length()));

            } else if (tokens[i].startsWith("NOTE:")) {
                qrVcardModel.setNote(tokens[i].substring("NOTE:".length()));

            } else if (tokens[i].startsWith("SUMMARY:")) {
                qrVcardModel.setSummary(tokens[i].substring("SUMMARY:".length()));

            } else if (tokens[i].startsWith("DTSTART:")) {
                qrVcardModel.setDtstart(tokens[i].substring("DTSTART:".length()));

            } else if (tokens[i].startsWith("DTEND:")) {
                qrVcardModel.setDtend(tokens[i].substring("DTEND:".length()));

            }
        }

        Intent intentVcard = new Intent(MainActivity.this, VcardActivity.class);
        startActivity(intentVcard);

    }

The second problem I face is that I can't open Google Maps app when I scan the Geoqrcode .我面临的第二个问题是我在扫描Geoqrcode时无法打开 Google Maps 应用程序。 Here is the code:这是代码:

else if (text.startsWith("geo:"))
{
    QRGeoModel qrGeoModel = new QRGeoModel();
    String delims = "[ , ?q= ]+";
    String tokens[] = text.split(delims);

    for (int i=0;i<tokens.length;i++)
    {
        if (tokens[i].startsWith(" geo:"))
        {
            qrGeoModel.setLat(tokens[i].substring("geo:".length()));
        }
    }
    qrGeoModel.setLat(tokens[0].substring("geo:".length()));
    qrGeoModel.setLng(tokens[1]);
    qrGeoModel.setGeo_place(tokens[2]);

    Uri gmmIntentUri = Uri.parse("qrGeoModel.getLat(),qrGeoModel.getLng()");
    Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
    mapIntent.setPackage("com.google.android.apps.maps");
    startActivity(mapIntent);

}

Do you know how can I do that two?你知道我该怎么做这两个吗?

You can pass values in Intent like this:您可以像这样在 Intent 中传递值:

intentVcard.putExtra("value_name" , value);

value can be String, int, boolean, long and more (you can check in your ide what you can pass). value 可以是 String、int、boolean、long 等等(您可以在 ide 中检查您可以通过的内容)。

On the recieving activity you can get the value like this:在接收活动中,您可以获得如下值:

getIntent.getStrinExtra("value_name");

Be sure to replace it by your value name, for String it is like this, however for boolean it will be:请务必用您的值名称替换它,对于 String 它是这样的,但是对于 boolean 它将是:

getIntent.getBooleanExtra("value_name" , defualt_value);

About the Google Maps problem, look at this line关于谷歌地图问题,看这一行

 Uri gmmIntentUri = Uri.parse("qrGeoModel.getLat(),qrGeoModel.getLng()");

you don't need " there, that way you will pass it all as a string and not the actual value of qrGeoModel.getLat(), insted write this line like that:你不需要 " 那里,这样你将把它作为一个字符串而不是 qrGeoModel.getLat() 的实际值传递,这样写这一行:

Uri gmmIntentUri = Uri.parse(qrGeoModel.getLat() + "," + qrGeoModel.getLng());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM