4

I'm new in Unity and really don't understand how to start unity application from android activity. I need a widget that sometimes changes (for example, when sms received) and on click start unity application.There is no problem with widget and unity project separately. But I don't know how to start unity from android project.

Lex Li
  • 60,503
  • 9
  • 116
  • 147
user966562
  • 41
  • 1
  • 4
  • If you want to start another app from your app then you can start another app thru its package name. – Vineet Shukla Oct 07 '11 at 12:39
  • Thank you, I used getLaunchIntentForPackage("package_name"). To know Unity application package name I look BuildSettings/PlayerSettings then in inspector OtherSettings/BundleIdentifier. And it works :) – user966562 Oct 07 '11 at 14:10

2 Answers2

0

You can extend the main activity UnityPlayerActivity (controll the main loop for the Unity Player on Android)

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();
    }


}

Here is the document

Maadiah
  • 431
  • 6
  • 20
0

I call start Application from widget button, and it helps for me. I call UpdateWidgetButtonsAction() from onUpdate widget method and set PendingIntent to 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);
    }

then create BroadcastReciever, which receive ACTION_LOAD_APP action, and startActivity

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);
    }
}

}