1

背景

在任务对地址进行地理编码后,我试图从 asynctask onPostExecute 的以下意图启动 waze,但它不工作。该链接工作正常,正如我所解释的那样,它与从 onPostExecute 启动一个意图有关,说无法解析 logcat 中的意图。

类声明:

public static class WazeAsyncLaunch extends AsyncTask<String, Void, Intent> {
    Context mContext = null;
    public WazeAsyncLaunch(Context context){
        mContext = context;
    }

这是 doInBackground

protected Intent doInBackground(String... addresses) {

    //does geocoding here, i give short version
    Uri intentURI = Uri.parse("https://waze.com/ul?ll=48.8566969,2.3514616&navigate=yes");
    Intent mapIntent = new Intent(Intent.ACTION_VIEW);
    mapIntent.setData(intentURI);
    mapIntent.addCategory(Intent.CATEGORY_DEFAULT);
    mapIntent.addCategory(Intent.CATEGORY_BROWSABLE);
    return mapIntent;
}

这是onPostExecute,发生错误的地方

protected void onPostExecute(Intent result) {
    mContext.startActivity(result);
}

我像这样从 mainactivity 中的按钮 onclick 事件处理程序调用它

new WazeAsyncLaunch(MainActivity.this).execute(address);

基于社区指针,我也在我的 onPostExecute 中尝试过,但无济于事

String url = "https://www.waze.com/ul?ll=40.75889500%2C-73.98513100&navigate=yes&zoom=17";
Intent intent = new Intent( Intent.ACTION_VIEW, Uri.parse( url ) );
startActivity( intent );

有趣的是,当我这样做时也不起作用:

adb shell am start -a android.intent.action.VIEW -c android.intent.category.DEFAULT -c android.intent.category.BROWSABLE -d https://waze.com/ul?ll=48.8566969,2.3514616&navigate=yes

但是当我这样做时它确实有效:

adb shell am start -d https://waze.com/ul?ll=48.8566969,2.3514616&navigate=yes

当我在手机上单击此链接时,它也可以使用 https://waze.com/ul?ll=48.8566969,2.3514616&navigate=yes

指针:

设备上没有安装浏览器。(仅位智)。

--在 waze 的清单中是以下意图过滤器:

<intent-filter>
   <action android:name="android.intent.action.VIEW"/>
   <category android:name="android.intent.category.DEFAULT"/>
   <category android:name="android.intent.category.BROWSABLE"/>
   <data android:scheme="http"/>
   <data android:scheme="https"/>
   <data android:host="www.waze.com"/>
   <data android:host="waze.com"/>
   <data android:pathPattern="/ul.*"/>
</intent-filter>

所以不知道为什么我的代码不工作,但是考虑到 adb 和链接点击的事实,当只有数据传递而没有类别和操作时,我看到意图有效。但我不知道如何在只有数据而没有其他属性的 Java 中启动意图。

问题

如何仅使用数据字符串从 java 启动意图?或者是否有其他解决方案?

4

2 回答 2

1

这是完整的工作代码。1 首先制作一个如下所示的类

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;

public class WazeClass{

    public static Context mContext;
    public static String address;
    public WazeClass(Context context,String waddress) {
        super();
        mContext = context;
        address = waddress;
    }



    public static class WazeShow extends AsyncTask<String,Void,Intent>
    {

        public WazeShow(Context applicationContext) {
            mContext = applicationContext;
        }

        @Override
        protected Intent doInBackground(String... strings) {
            String url = "https://www.waze.com/ul?ll=40.75889500%2C-73.98513100&navigate=yes&zoom=17";
            Intent intent = new Intent( Intent.ACTION_VIEW, Uri.parse( url ) );
            return intent;
        }

        @Override
        protected void onPostExecute(Intent intent) {
            super.onPostExecute(intent);
            try
            {
                mContext.startActivity(intent);
            }
            catch ( ActivityNotFoundException ex  )
            {
                // If Waze is not installed, open it in Google Play:
                Intent playintent = new Intent( Intent.ACTION_VIEW, Uri.parse( "market://details?id=com.waze" ) );
                mContext.startActivity(intent);
            }

        }
    }
}

然后从任何片段或活动调用。如果从片段调用使用 getContext();

new WazeClass.WazeShow(getApplicationContext()).execute("address");
于 2020-04-13T08:01:56.543 回答
0

像下面的代码一样改变你的意图

try
{

  String url = "https://www.waze.com/ul?ll=40.75889500%2C-73.98513100&navigate=yes&zoom=17";
  Intent intent = new Intent( Intent.ACTION_VIEW, Uri.parse( url ) );
  startActivity( intent );
}
catch ( ActivityNotFoundException ex  )
{
  // If Waze is not installed, open it in Google Play:
  Intent intent = new Intent( Intent.ACTION_VIEW, Uri.parse( "market://details?id=com.waze" ) );
  startActivity(intent);
}
于 2020-04-13T05:34:32.770 回答