zoukankan      html  css  js  c++  java
  • Unity 模拟点击Home键和启动其他app

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class NewBehaviourScript : MonoBehaviour {
    
    
        private AndroidJavaObject currentActivity{
    
            get{ 
                return new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
            }
    
        }
        //启动应用
        public void StartApp(string packageName,bool isExitThisApp){
            if(Application.platform != RuntimePlatform.Android)
                return;
            AndroidJavaObject launch = currentActivity.Call<AndroidJavaObject>("getPackageManager").Call<AndroidJavaObject>("getLaunchIntentForPackage",packageName);
            currentActivity.Call("startActivity",launch);
            if(isExitThisApp){
                Application.Quit();
            }
        }
        //模拟点击Home键
        void GoHome1()
        {
            string ACTION_MAIN = "android.intent.action.MAIN";
            int FLAG_ACTIVITY_NEW_TASK = 268435456;
            string CATEGORY_HOME = "android.intent.category.HOME";
    
            AndroidJavaObject intent=new AndroidJavaObject("android.content.Intent",ACTION_MAIN);
            intent.Call<AndroidJavaObject> ("setFlags",FLAG_ACTIVITY_NEW_TASK);
            intent.Call<AndroidJavaObject> ("addCategory", CATEGORY_HOME);
            currentActivity.Call("startActivity",intent);
        }
        //模拟点击Home键
        void GoHome2()
        {
            AndroidJavaClass Intent = new AndroidJavaClass ("android.content.Intent");
            AndroidJavaObject intent=new AndroidJavaObject("android.content.Intent",Intent.GetStatic<AndroidJavaObject>("ACTION_MAIN"));
    
            //        intent.Call<AndroidJavaObject> ("setFlags",Intent.GetStatic<AndroidJavaObject> ("FLAG_ACTIVITY_NEW_TASK"));//不知道什么原因,这样写居然报错,
            int FLAG_ACTIVITY_NEW_TASK = Intent.GetStatic<int> ("FLAG_ACTIVITY_NEW_TASK");//非得需要用个变量接收一下,而且用AndroidJavaObject接收也报错,坑
            intent.Call<AndroidJavaObject> ("setFlags",FLAG_ACTIVITY_NEW_TASK);
    
            intent.Call<AndroidJavaObject> ("addCategory",Intent.GetStatic<AndroidJavaObject> ("CATEGORY_HOME"));
            currentActivity.Call("startActivity",intent);
        }
    
        void Update()
        {
            if (Input.GetMouseButtonDown(0)) {
    //            GoHome1 ();
                GoHome2 ();
            }
        }
    
    
    }

    已上代码不需要引用任何 jar 包,直接可以用

    是通过以下java代码变形而来的

    Intent intent = new Intent(Intent.ACTION_MAIN);  

    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    intent.addCategory(Intent.CATEGORY_HOME);

    startActivity(intent);  

  • 相关阅读:
    dnn5.5.1的配置
    The Auto option has been disabled as the DotNetNuke Application cannot connect to a valid SQL Server database
    DNN常用的几种页面跳转(EditUrl和Globals.NavigateURL)
    动态生成ASP.NET按钮时要注意的一个问题
    Visual C#实现Windows信使服务
    浏览器滚动条的参数总结
    AJAX实现无刷新三联动下拉框
    c#.net常用的小函数和方法集
    利用OWC生成统计图表(代码+注释)
    ASP.NET之精通弹出窗口
  • 原文地址:https://www.cnblogs.com/Jason-c/p/8359603.html
Copyright © 2011-2022 走看看