【问题标题】:Why does waze deeplink not work in asynctask?为什么 waze deeplink 在 asynctask 中不起作用?
【发布时间】:2020-07-25 15:34:52
【问题描述】:

背景

在任务对地址进行地理编码后,我试图从 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 中仅使用数据字符串启动意图? 或者是否有其他解决方案?

【问题讨论】:

    标签: android android-intent deep-linking waze


    【解决方案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");
    

    【讨论】:

    • 总结:本质上,@shakac 告诉我使用 ApplicationContext 而不是 MainActivity.this;这是一个有效的答案。谢谢
    • 欢迎我的朋友
    【解决方案2】:

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

    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);
    }
    

    【讨论】:

    • 这在 mainactivity 中运行良好,但我需要在 asyncTask onPostExecute 中使用它。我会更新问题。由于某种原因仍然无法正常工作。感谢您及时的回复。不幸的是,它带我去游戏商店,而不是启动 waze
    • 因为您没有安装 waze,所以它会将您发送到 play store 安装 waze。
    • 我已经安装了 waze,当我从 mainactivity 运行时,您的代码可以正常工作,但不能从 asynctask 运行。我不知道,为什么。更新了我的问题以更好地解释
    猜你喜欢
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-16
    相关资源
    最近更新 更多