我是android的新手。我正在创建一个应用程序,我必须在单击后禁用该按钮,并在5分钟后再次启用它。即使应用程序关闭后,倒计时器仍应继续。任何帮助表示赞赏。提前致谢!
答案 0 :(得分:7)
您可以通过以下代码将时间存储在共享偏好中。
SharedPreferences settings = c.getSharedPreferences(PREFS_NAME, 1);
SharedPreferences.Editor editor = settings.edit();
editor.putString("click_time", your_time);
每当需要比较时,只需将当前时间与优先存储的时间进行比较。检查时间差是否小于5分钟。
从偏好中抽出时间:
SharedPreferences settings = c.getSharedPreferences(PREFS_NAME, 1);
String storedTime=settings.getString("clic_time", "");
分钟:
Date current, Date previous;
long diff = current.getTime() - previous.getTime();
long minutes = diff / (60 * 1000);
要显示计时器,当应用程序来自后台时,您可以通过使用2次之间的差异从偏好中获取时间并启动计时器。
答案 1 :(得分:0)
1)创建服务并在其中加入计时器代码
2)在服务中,在你的时间完成广播后的意图
3)在您的活动中再次编写广播接收器和广播接收器启用按钮。
关闭app后运行服务:How to keep a service running in background even after user quits the app?
从服务到活动的发送意图请查看:send intent from service to activity
一些经过测试的代码
1)创建服务:
var myarr = ['test', 'tost', 'aww'];
console.log(myarr.indexOf('tost')); // returns 1
console.log(myarr.indexOf('tosts')); // returns -1 (value not found)
2)你的活动
-create action string
public class TimeService extends IntentService {
public TimeService() {
super("TimeService");
}
@Override
protected void onHandleIntent(Intent intent) {
//Your Time Code
//broadcast intent in your app
Intent RTReturn = new Intent(MainActivity.RECEIVE);
RTReturn.putExtra("state", "done");
LocalBroadcastManager.getInstance(this).sendBroadcast(RTReturn);
}
- 创建BroadcastManager
//Your activity will respond to this action String
public static final String RECEIVE = "TIMING";
- 创建broadcastReceiver
private LocalBroadcastManager bManager;
onCreate()中的-put intent过滤器
private BroadcastReceiver bReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(RECEIVE)) {
String serviceString = intent.getStringExtra("state");
if(serviceString.equals("done"))
// enable your button
btnEnable();
}
}
};
点击
将其添加到按钮中 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//make an intent filter
bManager = LocalBroadcastManager.getInstance(this);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(RECEIVE);
bManager.registerReceiver(bReceiver, intentFilter);
}
最后记住两件事:
首先 - >将服务添加到您的清单
秒 - >在onDestroy()
中取消注册广播接收器答案 2 :(得分:0)
private long mLastClickTime = 0;
public static final long MAX_CLICK_INTERVAL = 1000; // here you can pass the time interval you want the user to wait for the next click
// and paste the below code on button click
if (SystemClock.elapsedRealtime() - mLastClickTime < MAX_CLICK_INTERVAL) {
return;
}
mLastClickTime = SystemClock.elapsedRealtime();
答案 3 :(得分:-1)
尝试处理程序
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
// this will disable your button
yourButton.setEnabled(false);
}
};
yourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// wait for 5 minutes
handler.postDelayed(runnable, 5*60*1000);
}
});