在其他Android应用中启动应用

时间:2018-01-29 03:21:17

标签: android firebase android-intent firebase-dynamic-links

我的问题是如何在应用B中启动应用A,应用A通过深层链接显示在应用B内?

在下面的第一张图片中,Debug应用程序显示为Slack的单独应用程序(新代码,Firebase深层链接)。在第二张图片中,Debug应用程序似乎位于Slack应用程序中(旧代码,Android深层链接)。我想使用Firebase深层链接并在其他应用程序(Slack,Gmail等)中显示Debug应用程序。

enter image description here enter image description here

任何人都可以通过下面的代码告诉我如何实现这一目标吗?

旧代码,Android深层链接:

AndroidManifest

<activity
    android:name=".activity.SplashScreenActivity"
    android:screenOrientation="portrait"
    android:theme="@style/SplashTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <!-- Accepts URIs that begin with "abc://flights” -->
        <data
            android:host="sales"
            android:scheme="abc" />
        <data
            android:host="deals"
            android:scheme="abc" />
    </intent-filter>
</activity>

活动:

Intent intent = new Intent(SplashScreenActivity.this,BottomNavBarActivity.class);

        //Deep Linking Content
        Uri deepLinkData = getIntent().getData();
        if (deepLinkData != null) {
            intent.putExtra(EXTRA_DEEP_LINK, deepLinkData.getHost());
        }

        startActivity(intent);
        overridePendingTransition(R.anim.splash_fade_in, R.anim.splash_fade_out);
        finish();

新代码,Firebase深层链接:

AndroidManifest:

<activity
        android:name=".activity.SplashScreenActivity"
        android:screenOrientation="portrait"
        android:theme="@style/SplashTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <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:host="abc.app.goo.gl"
                android:scheme="http"/>
            <data
                android:host="abc.app.goo.gl"
                android:scheme="https"/>
        </intent-filter>
    </activity>

活动:

FirebaseDynamicLinks.getInstance()
            .getDynamicLink(getIntent())
            .addOnSuccessListener(this, new OnSuccessListener<PendingDynamicLinkData>() {
                @Override
                public void onSuccess(PendingDynamicLinkData pendingDynamicLinkData) {
                    // Get deep link from result (may be null if no link is found)
                    Uri deepLink = null;
                    if (pendingDynamicLinkData != null) {

                    // Start the activity through intent, same as before.

                    }
                }
            })
            .addOnFailureListener(this, new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.v(TAG, "Firebase deep link failure");
                }
            });

2 个答案:

答案 0 :(得分:0)

它依赖于发送意图的应用程序,例如它们是否通过FLAG_ACTIVITY_NEW_TASK。我怀疑这里的区别在于Slack是如何处理链接的 - 它们可能不同于其他格式的URL(旧链接具有非标准方案)。

答案 1 :(得分:0)

创建一个方法openApp(),并根据您的需要调用它。

public void openAnApp()
    {
      Boolean flag=false;
      try{
          Intent intent = new Intent(Intent.ACTION_SEND);
           intent.setType("text/plain");  
           final PackageManager packageManager = getActivity().getPackageManager();
           Intent intent1 = new Intent(Intent.ACTION_MAIN, null);
           intent1.addCategory(Intent.CATEGORY_LAUNCHER);
           List<ResolveInfo> resInfos = packageManager.queryIntentActivities(intent1, 0);
           ActivityInfo activity = null;
           //getting package names and adding them to the hashset
           for(ResolveInfo resolveInfo : resInfos) {
            System.out.println("apap="+resolveInfo.activityInfo.packageName);
            if(resolveInfo.activityInfo.packageName.equals("your.app.packagename"))
            {  
                flag = true;
                activity = resolveInfo.activityInfo; 
                break;
            }
         }
           if (flag) { 
           //  final ActivityInfo activity = app.activityInfo; 
               final ComponentName name = new ComponentName(activity.applicationInfo.packageName,activity.name);
               intent = new Intent(Intent.ACTION_MAIN);
               intent.addCategory(Intent.CATEGORY_LAUNCHER);
               intent.setComponent(name);
               getActivity().startActivity(intent);
               //startActivity(Intent.createChooser(intent , "Send image using.."));
              } else {
                  Uri uri=Uri.parse("market://details?id=your.app.packagename");
                Intent goToMarket=new Intent(Intent.ACTION_VIEW,uri);
                try{
                    startActivity(goToMarket);
                }catch(ActivityNotFoundException e){                    
                    Toast.makeText(getActivity(),"Couldn't launch the market",Toast.LENGTH_SHORT).show();
                }
              }
             } catch (Exception e) {
              Toast.makeText(getActivity(), "Something went wrong", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }