有没有一种方法可以在特定时间只执行一次无返回值的操作?

huangapple 未分类评论45阅读模式
英文:

Is there a way to execute a void only once at a certain time?

问题

以下是翻译好的内容:

我有一个带有RecyclerView的活动,我希望RecyclerView每次显示不同的列表项。这样做是可行的,但我希望这个过程只运行一次(在特定的时间,例如00:00),而不是每次我进入活动时都运行,这个过程会随机决定在RecyclerView上显示哪个项目。

以下是代码:

public class Comida2 extends AppCompatActivity implements Adaptador2.OnRecipeListener {
    // ... (其他代码略)

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // ... (其他代码略)

        // 在这里添加一个定时任务来在指定时间运行pickEntidad() 方法
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0); // 设置小时为00
        calendar.set(Calendar.MINUTE, 0);      // 设置分钟为00

        long currentTime = System.currentTimeMillis();
        long scheduledTime = calendar.getTimeInMillis();

        if (currentTime < scheduledTime) {
            // 如果当前时间小于指定时间,等待直到指定时间并运行pickEntidad()
            long delayMillis = scheduledTime - currentTime;
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    pickEntidad();
                }
            }, delayMillis);
        }
    }

    // ... (其他代码略)

    // 这是你之前的 pickEntidad() 方法
    public void pickEntidad(){
        // ... (其他代码略)
    }
}

另外,你提到想在另一个不同的活动中添加一个按钮,可以触发当前活动中的 pickEntidad() 方法,从而改变在RecyclerView上随机显示的项目。在另一个活动中,你可以通过点击按钮来触发当前活动的 pickEntidad() 方法,以下是在不同的活动中触发的示例代码:

public class AnotherActivity extends AppCompatActivity {
    // ... (其他代码略)

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // ... (其他代码略)

        Button triggerButton = findViewById(R.id.trigger_button);
        triggerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 触发Comida2活动中的pickEntidad()方法
                Comida2.this.pickEntidad();
            }
        });
    }
    
    // ... (其他代码略)
}

请注意,上述代码只是示例,可能需要根据你的实际情况进行调整。希望这些代码对你有所帮助。

英文:

I've got an activity with a recyclerview, and I want that recyclerview to show different listItems every time. That works, but I want this to run only once(at a certain hour eg. 00:00h) and not to run again every time that I get into the activity all the void that randomly decides which item to show on the recyclerview.

Here's the code:

public class Comida2 extends AppCompatActivity implements Adaptador2.OnRecipeListener {
    private RecyclerView recyclerView1;
    List&lt;Entidad2&gt; listItems;
    Adaptador2 adaptor;
    private Entidad2 entidad1,entidad2,entidad3;
    Button cambiarmenu;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_comida);

        cambiarmenu = (Button) findViewById(R.id.boton_cambiarmenu);

        recyclerView1 = findViewById(R.id.lv_1);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView1.setLayoutManager(layoutManager);

        listItems = new ArrayList&lt;&gt;();
        entidad1 = new Entidad2(R.drawable.calabacines_3, &quot;Solomillo a la plancha&quot;, &quot; 10 min.&quot;, 4, 20);
        entidad2 = new Entidad2(R.drawable.patatas_deluxe_especiadas_70523_300_150, &quot;Entrecot&quot;, &quot; 15 min.&quot;, 2, 50);
        entidad3 = new Entidad2(R.drawable.tomate, &quot;Hamburguesa&quot;, &quot; 2 min.&quot;, 5, 100);


        listItems.add(entidad1);
        listItems.add(entidad2);
        listItems.add(entidad3);

        adaptor = new Adaptador2(listItems, this);
        recyclerView1.setAdapter(adaptor);
        adaptor.notifyDataSetChanged();
    }


    @Override
    public void OnRecipe(int priority) {

        if (priority == 20) {
            Intent in = new Intent(this, Solomillo.class);
            startActivity(in);
        }
        if (priority == 50) {
            Intent in = new Intent(this, Entrecot.class);
            startActivity(in);
        }
        if (priority == 100) {
            Intent in = new Intent(this, Hamburguesa.class);
            startActivity(in);
        }
    }

    public void pickEntidad(){
        final int random = new Random().nextInt(101);

        int priority1 = entidad1.getPriority();
        int priority2 = entidad2.getPriority();
        int priority3 = entidad3.getPriority();


        listItems.clear();
        if(random &lt; priority1){

            listItems.add(entidad1);

        }else if(random &lt; priority2){

            listItems.add(entidad2);

        }else if (random &lt;= priority3){

            listItems.add(entidad3);

        }
        adaptor.notifyDataSetChanged();
    }
}

I've looked at some other posts but the problem is that I want all that process to run one time when I first get into the app and then I'd shut down the app. After that, get in the app and still not run the void. But it should run at a certain hour, 00:00h would be ok as an example.

In addition, I'd like to add a button on a different activity which can run the void in this activity and therefore change what is randomly shown on the recyclerview.

答案1

得分: 0

使用 WorkManager 并安排一个任务,在每天晚上 0:00 运行。让它挑选要显示的项目并将它们写入文件。然后当您的活动运行时,让它读取该文件并显示这些项目。

对于活动中的按钮,让它删除该文件。如果一开始没有文件,编写 RecyclerView 活动来自动生成文件(在开头如果需要的话显示加载屏幕)。

英文:

Use WorkManager and schedule a job to run at 0:00h every night. Have it pick the items to show and write them to a file. Then when your activity runs, have it read that file and display those items.

For the button in the activity, have it delete the file. Code the RecyclerView activity to generate the file itself if there is none to begin with (showing a loading screen if necessary at the beginning).

huangapple
  • 本文由 发表于 2020年4月6日 20:13:24
  • 转载请务必保留本文链接:https://java.coder-hub.com/61059598.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定