使用“额外”参数启动某个应用程序?

时间:2016-06-16 16:33:57

标签: android android-intent unity3d

我有一个屏幕截图,我希望我的用户能够在他们的社交媒体帐户上分享我指定的一些文字。

我的函数采用要查找的社交媒体应用程序的名称,一旦找到,我会填充意图参数并使用“选择”运行选择器对象。

问题是它没有立即启动应用程序。它仍会显示“共享”屏幕以从中选择应用,因此我必须明确点按应用才能启动它。

这是我的代码:

    static bool ShareScreenshot_Android_ToSpecific(string ScreenshotPath, string SocialNetwork)
    {
        bool FoundApp = false;

        AndroidJavaClass Unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject CurrentActivity = Unity.GetStatic<AndroidJavaObject>("currentActivity");

        AndroidJavaClass IntentClass = new AndroidJavaClass("android.content.Intent");
        AndroidJavaClass PackageManagerClass = new AndroidJavaClass("android.content.Intent");

        AndroidJavaObject IntentObject = new AndroidJavaObject("android.content.Intent");

        IntentObject.Call<AndroidJavaObject>("setAction", IntentClass.GetStatic<string>("ACTION_SEND"));
        IntentObject.Call<AndroidJavaObject>("setType", "image/jpeg");

        AndroidJavaObject PackageManager = CurrentActivity.Call<AndroidJavaObject>("getPackageManager");

        AndroidJavaObject Apps = PackageManager.Call<AndroidJavaObject>(
            "queryIntentActivities", IntentObject, 0);

        int Count = Apps.Call<int>("size");

        //Debug.Log("[Social Share] Activity count: " + Count);

        for (int i = 0; i < Count; i++)
        {
            var App = Apps.Call<AndroidJavaObject>("get", i);

            var ActivityInfo = App.Get<AndroidJavaObject>("activityInfo");
            var PackageName = ActivityInfo.Get<string>("packageName");
            var ActivityName = ActivityInfo.Get<string>("name");

            if (ActivityName.ToLower().Contains(SocialNetwork) || 
                PackageName.ToLower().Contains(SocialNetwork))
            {
                Debug.Log("[Social Share] Found app: " + PackageName);
                Debug.Log("[Social Share] Found app: " + ActivityName);

                AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");
                AndroidJavaObject UriObject = uriClass.CallStatic<AndroidJavaObject>("parse", 
                    "file://" + ScreenshotPath);

                IntentObject.Call<AndroidJavaObject>("putExtra",
                    IntentClass.GetStatic<string>("EXTRA_TEXT"), ScreenshotText);

                IntentObject.Call<AndroidJavaObject>("putExtra",
                    IntentClass.GetStatic<string>("EXTRA_STREAM"), UriObject);

                IntentObject.Call<AndroidJavaObject>("setPackage", PackageName);

                FoundApp = true;
                break;
            }
        }

        if (FoundApp)
        {
            AndroidJavaObject chooser = IntentClass.CallStatic<AndroidJavaObject>("createChooser", IntentObject, "Select");
            CurrentActivity.Call("startActivity", chooser);
        }

        return (FoundApp);
    }

以下是我的称呼方式:

ShareScreenshot_Android_ToSpecific(ScreenshotPath, "twi"); // or "face", etc

我希望在找到这些额外的文本和流值后启动应用程序。

我试过了:

IntentObject.Call<AndroidJavaObject>("addCategory",
         IntentClass.GetStatic<string>("CATEGORY_LAUNCHER"));

IntentObject.Call<AndroidJavaObject>("addFlags",
         IntentClass.GetStatic<int>("FLAG_ACTIVITY_NEW_TASK"));

但它没有做任何事情。我需要另一个意图吗?或者这实际上如何运作?

PS:我第一次与Android API进行互动。

0 个答案:

没有答案