gpt4 book ai didi

java - 如何在 Android 中每 60 秒运行一次代码

转载 作者:行者123 更新时间:2023-12-02 02:09:10 24 4
gpt4 key购买 nike

在我的应用程序中,我希望每 60 秒运行一次代码,这是我从服务器获取数据的代码。
为此,我编写了以下代码。
但是60秒后运行代码时,显示ForceClose错误并在logCat中显示以下消息。

显示我的 fragment View 的错误!

我的代码:

public class MainReminderFragment extends Fragment {

@BindView(R.id.mainExplore_noExploreTxt)
TextView mainExplore_noExploreTxt;
@BindView(R.id.newsPageLoadLay)
RelativeLayout newsPageLoadLay;
@BindView(R.id.toolbarTitleTxt)
TextView toolbarTitleTxt;
private Context context;
public List<Datum> model = new ArrayList<>();
public ReminderAdapter reminderAdapter;
private SharedPrefrencesHandler prefrencesHandler;
private String token = "";
private LinearLayoutManager layoutManager;
private MainActivity mainActivity;
private InterfaceApi api;
private boolean isRunning = false;
public ProgressBar mainExplore_progressBar;
public RecyclerView mainExplore_recyclerView;

public MainReminderFragment() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_main_reminder, container, false);
//Initialize
ButterKnife.bind(this, view);
context = getActivity();
prefrencesHandler = new SharedPrefrencesHandler(context);
reminderAdapter = new ReminderAdapter(context, model);
layoutManager = new LinearLayoutManager(context);
mainExplore_progressBar = view.findViewById(R.id.mainExplore_progressBar);
mainExplore_recyclerView = view.findViewById(R.id.mainExplore_recyclerView);
isRunning = true;
mainActivity = (MainActivity) getActivity();
api = ApiClient.getClient().create(InterfaceApi.class);
//Toolbar name
toolbarTitleTxt.setText(context.getResources().getString(R.string.reminder));
//RecyclerView
mainExplore_recyclerView.setLayoutManager(layoutManager);
mainExplore_recyclerView.setHasFixedSize(true);
//Get token
token = prefrencesHandler.getFromShared(SharedPrefrencesKeys.TOKEN.name());

//Get data
getData();
if (getActivity() != null) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(60000);
} catch (Exception e) {
}
if (isRunning) {
getData();
}
}
}
}).start();
}
});
}
return view;
}

//SendData method
private SerialReminderListSendData sendData(int page) {
SerialReminderListSendData listSendData = new SerialReminderListSendData();
listSendData.setPageIndex(page);
listSendData.setPageSize(200);

return listSendData;
}

//Get data
private void getData() {
Call<SerialReminderListResponse> call = api.getListSerialReminder(token, "2", sendData(1));

mainExplore_progressBar.setVisibility(View.VISIBLE);

call.enqueue(new Callback<SerialReminderListResponse>() {
@Override
public void onResponse(Call<SerialReminderListResponse> call, Response<SerialReminderListResponse> response) {
if (response.body().getData() != null && response.body().getStatusCode() != 401
&& response.body().getStatusCode() != 402) {
if (response.body().getData().size() > 0) {
model.clear();
model.addAll(response.body().getData());
reminderAdapter.notifyDataSetChanged();
mainExplore_recyclerView.setAdapter(reminderAdapter);
//Gone no explore
mainExplore_noExploreTxt.setVisibility(View.GONE);
} else {
mainExplore_noExploreTxt.setVisibility(View.VISIBLE);
mainExplore_recyclerView.setVisibility(View.GONE);
}
}

mainExplore_progressBar.setVisibility(View.GONE);
}

@Override
public void onFailure(Call<SerialReminderListResponse> call, Throwable t) {
mainExplore_progressBar.setVisibility(View.GONE);
}
});
}
}

logCat 中的错误消息:

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7130)
at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1115)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:5284)
at android.view.View.invalidateInternal(View.java:13615)
at android.view.View.invalidate(View.java:13579)
at android.view.View.setFlags(View.java:11422)
at android.view.View.setVisibility(View.java:8014)
at com.app.test.Fragments.MainPageFrags.MainReminderFragment.getData(MainReminderFragment.java:182)
at com.app.test.Fragments.MainPageFrags.MainReminderFragment.access$100(MainReminderFragment.java:47)
at com.app.test.Fragments.MainPageFrags.MainReminderFragment$1$1.run(MainReminderFragment.java:142)
at java.lang.Thread.run(Thread.java:761)

我该如何解决这个问题?请帮助我

最佳答案

每次尝试更改 View 的属性时,您只能从最初创建它的 Thread 中执行此操作。这就是主线程/UI 线程。例如,如果您调用

mainExplore_noExploreTxt.setVisibility(View.GONE);

你必须从主线程调用它。有一个实用方法是 runOnUiThread

runOnUiThread(new Runnable() {
@Override
public void run() {
mainExplore_noExploreTxt.setVisibility(View.GONE);
}
});

此外,您不想使用 Thread.sleep 或类似的东西。有很多针对 Android 的解决方案。在这种情况下,您可能希望将 HandlerpostDelayed 一起使用。

关于java - 如何在 Android 中每 60 秒运行一次代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50288861/

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