Android 即使应用程序关闭,按钮也必须在5分钟后被点击

Android 即使应用程序关闭,按钮也必须在5分钟后被点击,android,timer,Android,Timer,我在第一页上有一个按钮,它指向MainActivity页,而MainActivity页上有另一个按钮。我希望MainActivity页按钮必须在15分钟后才能单击。单击Firstpage按钮时必须开始15分钟。我希望即使应用程序关闭或处于后台,也会发生这种情况,所以我使用的是SharedReference。但问题是MainActivity按钮在我输入后立即启用。下面是我的代码- First.java public class First extends Activity { Button

我在第一页上有一个按钮,它指向MainActivity页,而MainActivity页上有另一个按钮。我希望MainActivity页按钮必须在15分钟后才能单击。单击Firstpage按钮时必须开始15分钟。我希望即使应用程序关闭或处于后台,也会发生这种情况,所以我使用的是SharedReference。但问题是MainActivity按钮在我输入后立即启用。下面是我的代码-

First.java

public class First extends Activity {
    Button btnnext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first);
        btnnext = (Button) findViewById(R.id.next);
        btnnext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(First.this,MainActivity.class);
                startActivity(intent);
                finish();
            }
        });

    }
}
public class MainActivity extends Activity {
Button btn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this,"CLicked now",Toast.LENGTH_LONG).show();
            }
        });

    }

    public void onStop() {
        super.onStop();
        SharedPreferences prefs = this.getSharedPreferences("time", Context.MODE_PRIVATE);
        long currentTime = new Date().getTime();
        SharedPreferences.Editor editor = prefs.edit();
        editor.putLong("time", currentTime);
        editor.apply();
        btn.setEnabled(false);
    }



    public void onRestart(){
        SharedPreferences prefs = this.getSharedPreferences("time", Context.MODE_PRIVATE);
        long previousTime = prefs.getLong("time", 0);
        long currentTime = new Date().getTime();

        if (currentTime - previousTime > 15*60*1000){
            //enable the button
            btn.setEnabled(true);
        } else {
            //disable it and start a new CountdownTimer; this is needed in order for
            //it to to become enabled if you're still in the app and the time ran out
            btn.setEnabled(false);
            new CountDownTimer(currentTime - previousTime, 1000) {
                public void onTick(long millisUntilFinished) {
                    btn.setText("Timeover" + millisUntilFinished / 1000);
                    btn.setBackgroundColor(Color.parseColor("#8B0000"));
                }

                public void onFinish() {
                    btn.setEnabled(true);
                }
            }.start();



        }
    }
}
public class First extends Activity {
    Button btnnext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first);
        btnnext = (Button) findViewById(R.id.next);
        btnnext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                storeTime();
                Intent intent=new Intent(First.this,MainActivity.class);
                startActivity(intent);
                finish();
            }
        });

    }

    private void storeTime() {
        SharedPreferences prefs = this.getSharedPreferences("time", Context.MODE_PRIVATE);
        long currentTime = new Date().getTime();
        SharedPreferences.Editor editor = prefs.edit();
        editor.putLong("time", currentTime);
        editor.apply();
    }
}
MainActivity.java

public class First extends Activity {
    Button btnnext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first);
        btnnext = (Button) findViewById(R.id.next);
        btnnext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(First.this,MainActivity.class);
                startActivity(intent);
                finish();
            }
        });

    }
}
public class MainActivity extends Activity {
Button btn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this,"CLicked now",Toast.LENGTH_LONG).show();
            }
        });

    }

    public void onStop() {
        super.onStop();
        SharedPreferences prefs = this.getSharedPreferences("time", Context.MODE_PRIVATE);
        long currentTime = new Date().getTime();
        SharedPreferences.Editor editor = prefs.edit();
        editor.putLong("time", currentTime);
        editor.apply();
        btn.setEnabled(false);
    }



    public void onRestart(){
        SharedPreferences prefs = this.getSharedPreferences("time", Context.MODE_PRIVATE);
        long previousTime = prefs.getLong("time", 0);
        long currentTime = new Date().getTime();

        if (currentTime - previousTime > 15*60*1000){
            //enable the button
            btn.setEnabled(true);
        } else {
            //disable it and start a new CountdownTimer; this is needed in order for
            //it to to become enabled if you're still in the app and the time ran out
            btn.setEnabled(false);
            new CountDownTimer(currentTime - previousTime, 1000) {
                public void onTick(long millisUntilFinished) {
                    btn.setText("Timeover" + millisUntilFinished / 1000);
                    btn.setBackgroundColor(Color.parseColor("#8B0000"));
                }

                public void onFinish() {
                    btn.setEnabled(true);
                }
            }.start();



        }
    }
}
public class First extends Activity {
    Button btnnext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first);
        btnnext = (Button) findViewById(R.id.next);
        btnnext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                storeTime();
                Intent intent=new Intent(First.this,MainActivity.class);
                startActivity(intent);
                finish();
            }
        });

    }

    private void storeTime() {
        SharedPreferences prefs = this.getSharedPreferences("time", Context.MODE_PRIVATE);
        long currentTime = new Date().getTime();
        SharedPreferences.Editor editor = prefs.edit();
        editor.putLong("time", currentTime);
        editor.apply();
    }
}

当用户单击
第一个活动中的按钮时,应将“时间”存储在共享首选项中。现在将其存储在
main活动的
onStop()
中,这意味着
onRestart()
代码第一次运行时,共享首选项中没有“time”值,因此您的代码认为“previousTime”值为0。由于您计算的
currentTime-previousTime
将超过15分钟,因此您将启用该按钮

更新

您只需将代码从
main活动
移动到
First
活动,以共享首选项的形式存储时间。执行如下操作:在
onClick()
处理程序中存储时间并启动
main活动。请参见下面的示例:

First.java

public class First extends Activity {
    Button btnnext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first);
        btnnext = (Button) findViewById(R.id.next);
        btnnext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(First.this,MainActivity.class);
                startActivity(intent);
                finish();
            }
        });

    }
}
public class MainActivity extends Activity {
Button btn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this,"CLicked now",Toast.LENGTH_LONG).show();
            }
        });

    }

    public void onStop() {
        super.onStop();
        SharedPreferences prefs = this.getSharedPreferences("time", Context.MODE_PRIVATE);
        long currentTime = new Date().getTime();
        SharedPreferences.Editor editor = prefs.edit();
        editor.putLong("time", currentTime);
        editor.apply();
        btn.setEnabled(false);
    }



    public void onRestart(){
        SharedPreferences prefs = this.getSharedPreferences("time", Context.MODE_PRIVATE);
        long previousTime = prefs.getLong("time", 0);
        long currentTime = new Date().getTime();

        if (currentTime - previousTime > 15*60*1000){
            //enable the button
            btn.setEnabled(true);
        } else {
            //disable it and start a new CountdownTimer; this is needed in order for
            //it to to become enabled if you're still in the app and the time ran out
            btn.setEnabled(false);
            new CountDownTimer(currentTime - previousTime, 1000) {
                public void onTick(long millisUntilFinished) {
                    btn.setText("Timeover" + millisUntilFinished / 1000);
                    btn.setBackgroundColor(Color.parseColor("#8B0000"));
                }

                public void onFinish() {
                    btn.setEnabled(true);
                }
            }.start();



        }
    }
}
public class First extends Activity {
    Button btnnext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first);
        btnnext = (Button) findViewById(R.id.next);
        btnnext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                storeTime();
                Intent intent=new Intent(First.this,MainActivity.class);
                startActivity(intent);
                finish();
            }
        });

    }

    private void storeTime() {
        SharedPreferences prefs = this.getSharedPreferences("time", Context.MODE_PRIVATE);
        long currentTime = new Date().getTime();
        SharedPreferences.Editor editor = prefs.edit();
        editor.putLong("time", currentTime);
        editor.apply();
    }
}

您可以针对这种情况实现cron作业。 您将从上面的链接获得参考


仔细阅读文档,这可能会对您有所帮助。祝你好运

最好且顺畅的方法是使用本地广播接收器和计时器服务。您可以在服务中运行计时器,当计时器完成时,您可以启动意图并发送意图数据以按下按钮。performClick()

你不想在手机关机后点击这个按钮吗强制广告人…是的,即使应用程序正在运行或已关闭,也希望相同。有人能给我一个倒数计时器的例子吗?我如何将其存储在共享pref中?已更改,但在我进入MainActivity时仍可单击按钮。嗯,
onRestart
仅当您的
MainActivity
进行配置更改时才会触发。尝试将onRestart替换为
onStart
。服务中是否有计时器运行的示例?如果您也想在后台运行计时器,那么我认为异步服务是提高应用程序性能的最佳方式。