【问题标题】:How to start Unity application from android activity?如何从 android 活动启动 Unity 应用程序?
【发布时间】:2023-03-17 16:58:01
【问题描述】:

我是 Unity 的新手,真的不明白如何从 android 活动启动统一应用程序。 我需要一个有时会发生变化的小部件(例如,当收到短信时)并单击启动统一应用程序。小部件和统一项目分开没有问题。但我不知道如何从 android 项目开始统一。

【问题讨论】:

  • 如果你想从你的应用启动另一个应用,那么你可以通过它的包名启动另一个应用。
  • 谢谢,我使用了 getLaunchIntentForPackage("package_name")。要知道 Unity 应用程序包名称,我查看 BuildSettings/PlayerSettings,然后在检查器 OtherSettings/BundleIdentifier 中查看。它有效:)

标签: android unity3d


【解决方案1】:

您可以扩展主活动UnityPlayerActivity(控制Android上Unity Player的主循环)

public class MainActivity extends UnityPlayerActivity implements MyFunction {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


}

这里是document

【讨论】:

    【解决方案2】:

    我从小部件按钮调用启动应用程序,这对我有帮助。 我从 onUpdate 小部件方法调用 UpdateWidgetButtonsAction() 并将 PendingIntent 设置为 onClick。

     private void UpdateWidgetButtonsAction(Context context, AppWidgetManager appWidgetManager, int widgetID)
        {
            RemoteViews widgetView = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
            widgetView.setOnClickPendingIntent(R.id.MyButtonImage, getPendingSelfIntent(context, "ACTION_LOAD_APP"));
            appWidgetManager.updateAppWidget(widgetID, widgetView);
        }
    

    然后创建BroadcastReciever,接收ACTION_LOAD_APP动作,并启动Activity

    public class WidgetBroadcastReceiver extends BroadcastReceiver
    {
    
    @Override
    public void onReceive(Context context, Intent intent) {
    
        LogCollector.Log("WidgetBroadcastReceiver " + intent.getAction());
        if(ACTION_LOAD_APP.equals(intent.getAction()))
        {
            Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage("UNITY_PACKAGE_NAME");
            context.startActivity(launchIntent);
        }
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2012-04-21
      • 1970-01-01
      • 2016-12-09
      • 2012-06-22
      • 1970-01-01
      • 2019-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多