Static intent android

1.manifest registered

Activity in registration, as follows:
Note: This is the must exist

      <intent-filter>
        <action android:name="action.action.action1"/> <action android:name="action.action.action2"/> <action android:name="action.action.action3"/> <action android:name="action.action.action4"/> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> 

2. Call

            Intent intent = new Intent(action); context.startActivity(intent); 

You can also use the command adb shell am start -a action.action.action1 test

3. Receive

3.1 Android get action

            Intent receiverIntent = getIntent(); String action =receiverIntent.getAction(); 

or

    @Override
    protected void onNewIntent(Intent intent) { super.onNewIntent(intent); intentResult=intent.getStringExtra(ReceiverTag); if(intentResult==null || intentResult == "") { intentResult=defaultIntentResult; } Log.d(Tag,"Intent result:"+intentResult); } 

3.2 Unity get action

3.2.1 Gets Activity

    public static AndroidJavaObject GetActivity() { AndroidJavaClass jc = new AndroidJavaClass(unityPlayerClass); if (jc == null) { Debug.LogErrorFormat("Failed to get Unity Player class, {0}", unityPlayerClass); return null; } AndroidJavaObject activity = jc.GetStatic<AndroidJavaObject>("currentActivity"); if (activity == null) { Debug.LogError("Failed to obtain Android Activity from Unity Player class."); return null; } return activity; } 

3.2.2 Gets intent

    private AndroidJavaObject GetIntent() { AndroidJavaObject activity = GetActivity(); if (activity != null) { return activity.Call<AndroidJavaObject>("getIntent"); } return null; } 

3.2.3 get action

    public string GetIntentMessage() { string result = ""; AndroidJavaObject intent = GetIntent(); if (intent == null) { Debug.Log("Get null intent"); return result; } try { result = intent.Call<string>("getAction"); Debug.Log("Get action result:" + result); } catch(Exception ex) { Debug.Log("Exception while getting action:" + ex.ToString()); } return result; }

Guess you like

Origin www.cnblogs.com/llstart-new0201/p/11916868.html