码迷,mamicode.com
首页 > 移动开发 > 详细

android静态intent

时间:2019-11-23 12:44:40      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:vacl   ror   rev   eric   string   exce   section   rri   log   

1.manifest中注册

在Activity中注册,如下:
注:此为必须存在的

      <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.调用

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

也可以使用命令adb shell am start -a action.action.action1测试

3.接收

3.1 Android获取action

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

或者

    @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获取action

3.2.1 获取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 获取intent

    private AndroidJavaObject GetIntent()
    {
        AndroidJavaObject activity = GetActivity();

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

        return null;
    }

3.2.3 获取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;
    }

android静态intent

标签:vacl   ror   rev   eric   string   exce   section   rri   log   

原文地址:https://www.cnblogs.com/llstart-new0201/p/11916868.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!