From 2531ef0ab750acb71cc0f4af8a5d102753d29f89 Mon Sep 17 00:00:00 2001 From: HEYAHONG <2229388563@qq.com> Date: Tue, 7 Jan 2025 17:10:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AE=9A=E6=97=B6=E5=99=A8?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/src/main/cpp/native-lib.cpp | 14 +++++++++++++ .../hcppbox/helloworld/MainActivity.java | 21 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/test/Android/HelloWorld/app/src/main/cpp/native-lib.cpp b/test/Android/HelloWorld/app/src/main/cpp/native-lib.cpp index 06e9366..3bc272b 100644 --- a/test/Android/HelloWorld/app/src/main/cpp/native-lib.cpp +++ b/test/Android/HelloWorld/app/src/main/cpp/native-lib.cpp @@ -35,3 +35,17 @@ JNIEXPORT void JNICALL Java_cn_hyhsystem_hcppbox_helloworld_MainActivity_Init(JNIEnv *env, jobject thiz) { LOGI("Init"); } + +/* + * 注意:毫秒定时器由定时器(在主活动实现)调用 + */ +hdefaults_tick_t current_tick=0; +extern "C" +JNIEXPORT void JNICALL +Java_cn_hyhsystem_hcppbox_helloworld_MainActivity_MsTick(JNIEnv *env, jobject thiz) { + current_tick++; + if(current_tick%5000==0) + { + LOGI("Current Tick=%llu",(unsigned long long)current_tick); + } +} \ No newline at end of file diff --git a/test/Android/HelloWorld/app/src/main/java/cn/hyhsystem/hcppbox/helloworld/MainActivity.java b/test/Android/HelloWorld/app/src/main/java/cn/hyhsystem/hcppbox/helloworld/MainActivity.java index 8cab388..b349cd1 100644 --- a/test/Android/HelloWorld/app/src/main/java/cn/hyhsystem/hcppbox/helloworld/MainActivity.java +++ b/test/Android/HelloWorld/app/src/main/java/cn/hyhsystem/hcppbox/helloworld/MainActivity.java @@ -5,6 +5,9 @@ import android.os.Bundle; import android.widget.TextView; +import java.util.Timer; +import java.util.TimerTask; + import cn.hyhsystem.hcppbox.helloworld.databinding.ActivityMainBinding; public class MainActivity extends AppCompatActivity { @@ -16,6 +19,10 @@ public class MainActivity extends AppCompatActivity { private ActivityMainBinding binding; + //毫秒定时器 + private Timer MsTimer=null; + private TimerTask MsTimerTask=null; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -23,6 +30,15 @@ protected void onCreate(Bundle savedInstanceState) { binding = ActivityMainBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); + MsTimer=new Timer(); + MsTimerTask=new TimerTask() { + @Override + public void run() { + MsTick(); + } + }; + MsTimer.schedule(MsTimerTask,1,1); + // Example of a call to a native method TextView tv = binding.sampleText; tv.setText(stringFromJNI()); @@ -45,4 +61,9 @@ public MainActivity() Init(); } + /* + * 毫秒定时器,由定时器调用,一般用于更新UI + */ + public native void MsTick(); + } \ No newline at end of file