/// <summary> /// 发送无参数广播 /// </summary> /// <param name="actionName">action</param> /// <param name="packageName">包名</param> /// <param name="broadcastName">广播名</param> public static void SendBroadcast(string actionName, string packageName, string broadcastName) { AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent", actionName); AndroidJavaObject componentNameJO = new AndroidJavaObject("android.content.ComponentName", packageName, broadcastName); intent.Call <AndroidJavaObject>("setComponent", componentNameJO); UnityAppContext.Call("sendBroadcast", intent); }
/// <summary> /// 发送带参数广播 /// </summary> /// <param name="actionName">action</param> /// <param name="packageName">包名</param> /// <param name="broadcastName">广播名</param> public static void SendBroadcastWithArgs(string actionName, string packageName, string broadcastName) { AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent", actionName); //设置广播参数 //intent.Call<AndroidJavaObject>("putExtra", "enable".ToJavaString(), true); intent.Call <AndroidJavaObject>("putExtra", "args key1".ToJavaString(), "args value1".ToJavaString()); intent.Call <AndroidJavaObject>("putExtra", "args key2".ToJavaString(), "args value2".ToJavaString()); AndroidJavaObject componentNameJO = new AndroidJavaObject("android.content.ComponentName", packageName, broadcastName); intent.Call <AndroidJavaObject>("setComponent", componentNameJO); UnityAppContext.Call("sendBroadcast", intent); }
/// <summary> /// 通过包名打开其他应用 /// </summary> /// <param name="pkgName"></param> public static void OpenAppByPkg(string pkgName) { using (AndroidJavaObject joIntent = PackageManager.Call <AndroidJavaObject>("getLaunchIntentForPackage", pkgName)) { AndroidLogI(pkgName + " start"); if (null != joIntent) { UnityAppContext.Call("startActivity", joIntent); } else { AndroidLogI(pkgName + " not exist"); } } }
/*安卓方法 * public static boolean isServiceRunning(Context mContext, String className) { * boolean isRunning = false; * ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); * List<ActivityManager.RunningServiceInfo> serviceList = activityManager.getRunningServices(100); * if (!(serviceList.size() > 0)) { * return false; * } * for (int i = 0; i < serviceList.size(); i++) { * String name = serviceList.get(i).service.getClassName(); * if (name.equals(className) == true) { * isRunning = true; * break; * } * } * return isRunning; * }*/ /// <summary> /// 判断指定服务是否运行 测试ok /// </summary> /// <returns></returns> public static bool IsServiceRunning(string serviceName) { AndroidJavaClass Context = new AndroidJavaClass("android.content.Context"); AndroidJavaObject systemServices = UnityAppContext.Call <AndroidJavaObject>("getSystemService", Context.GetStatic <AndroidJavaObject>("ACTIVITY_SERVICE")); AndroidJavaObject runningServiceInfos = systemServices.Call <AndroidJavaObject>("getRunningServices", 100); AndroidJavaObject[] runningServices = runningServiceInfos.Call <AndroidJavaObject[]>("toArray"); //AndroidLog("Running service Length:" + runningServices.Length); for (int i = 0; i < runningServices.Length; i++) { AndroidJavaObject sName = runningServices[i].Get <AndroidJavaObject>("service").Call <AndroidJavaObject>("getClassName"); AndroidLog(sName.ToCString()); if (sName.ToCString() == serviceName) { return(true); } } return(false); }
//google可能为了安全考虑,在5.1.+后调用activitymanager.getRunningAppProcesses()方法只能返回你自己应用的进程 /*安卓方法 * public static boolean isRunning(Context context, String packageName) { * ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); * List<RunningAppProcessInfo> infos = am.getRunningAppProcesses(); * for (RunningAppProcessInfo rapi : infos) { * if (rapi.processName.equals(packageName)) * return true; * } * return false; * }*/ /*添加权限 * <uses-permission android:name="android.permission.GET_TASKS" /> * <uses-permission android:name="android.permission.REAL_GET_TASKS" /> */ /// <summary> /// 判断指定包名的进程是否运行 测试...待完整测试 /// </summary> /// <returns></returns> private static bool IsAppRunningLow5Dot1(string pkg) { AndroidJavaClass Context = new AndroidJavaClass("android.content.Context"); AndroidJavaObject systemServices = UnityAppContext.Call <AndroidJavaObject>("getSystemService", Context.GetStatic <AndroidJavaObject>("ACTIVITY_SERVICE")); AndroidJavaObject runningAppInfos = systemServices.Call <AndroidJavaObject>("getRunningAppProcesses"); AndroidJavaObject[] runningApps = runningAppInfos.Call <AndroidJavaObject[]>("toArray"); AndroidLog("running apps Length:" + runningApps.Length); for (int i = 0; i < runningApps.Length; i++) { string appNameStr = runningApps[i].Get <string>("processName"); AndroidLog(appNameStr); if (appNameStr == pkg) { return(true); } } return(false); }