首页 文章

Codename One覆盖Android onNewIntent活动方法

提问于
浏览
2

我发现很多Android示例都使用Codename One覆盖主要的Activity生命周期方法,如 onResumeonPause 等 . 我通过接口类访问Android本机代码 .

现在我想重写方法 onNewIntent . 我怎么能做到这一点?它甚至可能吗?我是否需要通过像 public void start()public void stop() 这样的C1方法?

我已经在手机上激活了NFC,当我的NFC卡固定在手机上时,我想 grab NFC意图 . 此意图通过 onNewIntent 发送到应用程序 .

4 回答

  • 0

    您可以创建一个与Codename One完全无关的完全独立的活动,并将其放在native / android层次结构中 . 您可以使用类似 android.xactivity build hint的内容在清单XML中注册它 .

    从这一点开始,它就变成了一个本地活动,可以与Codename One进行通信 .

    请注意,如果您希望Codename One代码响应onResume,您可以在重新启动应用程序时调用的 start() 方法中编写它 .

  • 1

    除非您修改Codename One框架(或特别是其Android实现),否则您无法接收 onNewIntent() 回调 . LifecycleListener的当前实现不支持 onNewIntent() 生命周期方法 . 当 onNewIntent() 回调触发时,Codename One唯一能做的就是将新意图存储为当前活动意图(参见CodenameOneActivity.java:367):

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }
    

    因此,如果您需要在 onNewIntent() 触发时收到通知,唯一的方法是修改Codename One框架 .

    但是,您可以执行以下操作:使用 onResume() 生命周期方法 . 保证在 onNewIntent() 之后调用此方法,并且由于使用 setIntent(...) 更新了当前活动意图,因此一旦您的应用程序收到有关 onResume() 的通知,您就可以获取该意图 .

    • 您首先要创建一个lifecylce侦听器对象并使用 AndroidNativeUtil 类注册它:
    LifecycleListener listener = new LifecycleListener() {
        public void onCreate(Bundle savedInstanceState) {}
        public void onPause() {}
        public void onDestroy() {}
        public void onSaveInstanceState(Bundle b) {}
        public void onLowMemory() {}
    
        public void onResume() {
            // this is where you get notified about onResume()
        }
    };
    
    AndroidNativeUtil.addLifecycleListener(listener);
    
    • 然后,您将在 onResume 侦听器中检索intent:
    public void onResume() {
        Activity myActivity = AndroidNativeUtil.getActivity();
        Intent potentialNfcIntent = myActivity.getIntent();
    
        // do something with the intent if its an NFC intent
    }
    
    • 由于 onNewIntent() 的调用不是调用 onResume() 的唯一原因(实际上还有许多其他原因可能发生),您可能希望实现一些只处理该特定意图的检查 .
  • 1

    好的,我让它工作了 . 我使用了 LifecycleListener() 解决方案 . 这就是我做的方法:在我的接口类中,我创建了一个静态方法来创建我的LifecycleListener并执行 AndroidNativeUtil.addLifecycleListener(myListener); . 我使用 android.onCreate=com.mycompany.myapp.MyNativeImpl.start(); 从应用程序的内置提示中调用此静态方法 . 当我启动我的应用程序时,会弹出 onResume() 方法中的警报 . 完善 .

    还有另一种方法,使用C1的init / start / stop,但我不知道为什么,因为我检查适配器和每个参数是否都没有't null before using the method. Maybe it' s PendingIntent 问题?这是我写它的方式:

    final Intent intent = new Intent(AndroidNativeUtil.getActivity().getApplicationContext(), AndroidNativeUtil.getActivity().getClass());
                    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    pendingIntent = PendingIntent.getActivity(AndroidNativeUtil.getActivity().getApplicationContext(), 0, intent, 0);
    
  • 2

    如果我理解正确的问题,你想要一个共同的地方来处理不同活动的newIntent方法 .

    如果是,则定义自定义活动并使其他活动扩展它 .

    public class CustomActivity extends AppCompatActivity {
        @Override
        protected void onNewIntent(Intent intent) {
            super.onNewIntent(intent);
            // put your code here
        }
    }
    
    public class SomeActivity extends CustomActivity {
    
    }
    

相关问题