Skip to content

Commit

Permalink
添加定时器接口
Browse files Browse the repository at this point in the history
  • Loading branch information
HEYAHONG committed Jan 7, 2025
1 parent d2a051e commit 2531ef0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
14 changes: 14 additions & 0 deletions test/Android/HelloWorld/app/src/main/cpp/native-lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -16,13 +19,26 @@ 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);

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());
Expand All @@ -45,4 +61,9 @@ public MainActivity()
Init();
}

/*
* 毫秒定时器,由定时器调用,一般用于更新UI
*/
public native void MsTick();

}

0 comments on commit 2531ef0

Please sign in to comment.