gpt4 book ai didi

java - 执行一些 Action 并根据时间重复这些 Action

转载 作者:行者123 更新时间:2023-12-01 17:44:12 25 4
gpt4 key购买 nike

我正在 Android Studio 中编写我的项目应用程序。该应用程序连接到微 Controller ,并在按钮单击上发送逻辑 1,以通过 BT 从微 Controller 获取数据字符串。我有一个问题要问你,我怎样才能自动发送这个逻辑 1 并有一些延迟。我尝试了一些方法,但它们不能正常工作。应用程序停止

    //Send Button
sendButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
try
{
sendData();
}
catch (IOException ex) { }
}
});
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
sendButton.performClick();
}
},20000);

日志猫:

2020-03-27 18:44:10.913 11754-11754/com.example.bt_serial_test_1_0 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.bt_serial_test_1_0, PID: 11754
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:5645)
at android.view.View.performClick(View.java:6608)
at com.example.bt_serial_test_1_0.MainActivity$4.run(MainActivity.java:104)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:6878)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:876)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:5640)
at android.view.View.performClick(View.java:6608) 
at com.example.bt_serial_test_1_0.MainActivity$4.run(MainActivity.java:104) 
at android.os.Handler.handleCallback(Handler.java:873) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:207) 
at android.app.ActivityThread.main(ActivityThread.java:6878) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:876) 
Caused by: java.lang.IllegalArgumentException: Invalid message body
at android.telephony.SmsManager.sendTextMessageInternal(SmsManager.java:337)
at android.telephony.SmsManager.sendTextMessage(SmsManager.java:325)
at com.example.bt_serial_test_1_0.MainActivity.sendSms(MainActivity.java:239)
at java.lang.reflect.Method.invoke(Native Method) 
at android.view.View$DeclaredOnClickListener.onClick(View.java:5640) 
at android.view.View.performClick(View.java:6608) 
at com.example.bt_serial_test_1_0.MainActivity$4.run(MainActivity.java:104) 
at android.os.Handler.handleCallback(Handler.java:873) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:207) 
at android.app.ActivityThread.main(ActivityThread.java:6878) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:876) 

这个方法在onCreate中。有人可以帮我解决这个问题

最佳答案

这是重复任务的处理程序示例。

// Create the Handler object (on the main thread by default)
Handler handler = new Handler();
// Define the code block to be executed
private Runnable runnableCode = new Runnable() {
@Override
public void run() {
// Do something here on the main thread
Log.d("Handlers", "Called on main thread");
// Repeat this the same runnable code block again another 2 seconds
handler.postDelayed(runnableCode, 2000);
}
};
// Start the initial runnable task by posting through the handler
handler.post(runnableCode);

您可以根据您的情况进行修改

 sendButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
try
{
handler().postDelayed(r,1000);
}
catch (IOException ex) { }
}
});


final Runnable r = new Runnable()
{
public void run()
{
sendButton.performClick();
}
};

关于java - 执行一些 Action 并根据时间重复这些 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60891460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com