在C中使用计时器的方法是什么?我需要等到500毫秒才能找到工作.请提及做这项工作的任何好方法.我用过sleep(3);但是这个方法在那段时间内没有做任何工作.我有一些东西会尝试,直到那个时间来获得任何输入.
有时我们需要在代码运行之前延迟它.
这是可以通过Handler.postDelayed(Runnable)或CountdownTimer.
哪一个在性能方面更好?
请参阅下面的示例代码
处理器
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//DO SOMETHING
}
}, 1000);
Run Code Online (Sandbox Code Playgroud)
倒计时器
new CountDownTimer(1000, 1000) {
public void onFinish() {
//DO SOMETHING
}
public void onTick(long millisUntilFinished) {}
}.start();
Run Code Online (Sandbox Code Playgroud) 我想表明我的每个单元格内剩下多少时间RecyclerView......因为我已经为每个单元格使用了倒数计时器.在每一行我开始一个计数器并管理onTick()...所有工作都按预期工作...我有一个计时器勾选每一行,我的单元格也在更新但我的单元格现在闪烁....当它变得疯狂时我滚动.
这是我的适配器......
if (product.getProductType().equalsIgnoreCase("Auction Product")) {
isAuction=true;
viewHolder.img_product_type.setImageResource(R.drawable.bid);
viewHolder.txt_timeleft.setVisibility(View.VISIBLE);
start_countDown(product.getStart(),product.getStop(),viewHolder.txt_timeleft);
}
Run Code Online (Sandbox Code Playgroud)
计数器代码如下....
private void start_countDown(String start, String stop, final TextView txt_timeleft) {
try {
//Log.e("hetal",start+"....."+stop);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar start_date = Calendar.getInstance();
start_date.setTime(format.parse(start));
Calendar end_date = Calendar.getInstance();
end_date.setTime(format.parse(stop));
final Calendar today = Calendar.getInstance();
CountDownTimer timer;
txt_timeleft.setTextColor(Color.DKGRAY);
if(today.before(start_date)){
txt_timeleft.setTextColor(context.getResources().getColor(R.color.red));
txt_timeleft.setText(context.getString(R.string.auction_not_start));
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(1000); //You can manage the time of the blink with this parameter
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
txt_timeleft.startAnimation(anim);
return;
} …Run Code Online (Sandbox Code Playgroud) 我有一个小型的MVC网站,这是一个朋友美发沙龙.在这个页面上,我有一个div,用于显示从数据库记录中获取的数字.这个数字是当前排队等待理发的人数.
我目前的能力是登录"管理"页面并使用表格更新此号码,从"2"到"5",然后将"5"更改为"6",具体取决于有多少人坐在这个队列.
这是目前的手动操作.代码如下:
=============================
[HttpPost]
public ActionResult Update(Data data)
{
if (ModelState.IsValid)
{
data.ID = 1; //EF need to know which row to update in the database.
db.Entry(data).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index", "Home");
}
return View(data);
}
Run Code Online (Sandbox Code Playgroud)
====================================
{
public class Data
{
public int ID { get; set; }
public string Queue_Number { get; set; }
}
public class DataDBContext : DbContext
{
public DbSet<Data>Queue { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
我真正想要的是,一旦你从"管理"页面上的表单手动更新了队列号,我想自动倒计时20分钟(理发所需的大致时间),然后有队列号自动调整为1,直到达到"0".
例如我们队列中有5个人,20分钟后自动调整为4个人,网页会自动更新/刷新,然后又有2个人进入,所以我们手动将其调整为队列中的6个人并启动计时器再次,每20分钟通过队列调整-1直到它下降到"0".一旦它变为"0",它就会一直停留在那里,直到我们手动将更多人添加到队列中.
我恐怕我不知道如何开始这样的请求,或者即使有可能?
我非常感谢这里的专家提供的任何帮助,这些帮助可以为我"babystep".我没有提供的任何信息我会努力补充 - …
c# database asp.net-mvc-3 visual-web-developer-2010 countdowntimer
我想在我的应用程序中做什么:1.当用户打开应用程序时,他将能够看到已经运行的倒数计时器.因此,在这种情况下,我想在textview中显示倒计时数字,每秒都会更新.2.用户可以将其停止.3.用户可以离开应用程序,但倒计时应该仍然继续,并在他返回应用程序UI时显示更新时间.
基于以上几点,我了解到我需要实现在后台运行的服务.我已阅读此链接,但我的问题是实施.我对如何实现这种方式感到很遗憾.话虽这么说,一旦用户回到应用程序,我也无法想到在UI中显示时间的方法.我也看过这个链接 ,但我不确定是否只实现CountDownTimer使它成为一个正在运行的服务.那我该如何进行和实施呢?任何指向特定教程/代码的链接都表示赞赏.谢谢.
更新:到目前为止,我已经能够执行以下操作:MainActivity.java
public class MainActivity extends Activity {
Button btnStart,btnStop;
Intent serviceIntent;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStart = (Button) findViewById(R.id.btnStart);
btnStop = (Button) findViewById(R.id.btnStop);
tv = (TextView) findViewById(R.id.timeView);
//final MyCounter timer = new MyCounter(100000,1000);
//tv.setText("100");
//timer.start();
serviceIntent = new Intent(MainActivity.this,MyService.class);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startService(serviceIntent);
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated …Run Code Online (Sandbox Code Playgroud) 我正在使用倒数计时器进行音频通知......从一开始就不准确......
使用初始参数
private final long startCountDown;
private final long intervalCountDown;
...
startCountDown = 180 * 1000; // 3 mns - to be set from Preferences later
intervalCountDown = 60 * 1000; // 1 mns - to be set from Preferences later
...
public void onTick(long millisUntilFinished) {
Log.d(TAG, "notify countDown: " + millisUntilFinished + " msecs");
}
countDownTimer = new SwimCountDownTimer(startCountDown,intervalCountDown);
....
public void startCountDown() {
Log.d(TAG, "start countDown for " + startCountDown + " msecs" );
countDownTimer.start();
}
Run Code Online (Sandbox Code Playgroud)
我可以在日志中看到初始倒计时正确设置为180000但下一个应该是120000并且它设置为119945! …
我是Android编程的开始,我必须做一个CountdownTimer,从用户使用两个数字选择器选择的数字开始(一个用于小时,另一个用于分钟).
特征必须是:
- CountdownTimer必须在后台工作,并在到达00:00时通知用户.我有这个代码:
package com.android.application;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.NumberPicker;
public class MainActivity extends Activity {
NotificationManager nm;
private static final int ID_NOTIFICACION_PERSONAL =1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
NumberPicker number1 = (NumberPicker)findViewById(R.id.horas);
NumberPicker number2 = (NumberPicker)findViewById(R.id.minutos);
Button btn_lanzar=(Button)findViewById(R.id.btn_notificacion);
number1.setMaxValue(10);
number1.setMinValue(0);
number2.setMinValue(0);
number2.setMaxValue(59);
int number3=number1.getValue();
int number4=number2.getValue();
nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
btn_lanzar.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Notification notification = new Notification(R.drawable.estacionamiento,"Estacionamiento …Run Code Online (Sandbox Code Playgroud) android background android-activity countdowntimer android-studio
在我的应用程序中,我必须显示listview中每个项目的倒数计时器.我已经能够这样做了CountDownTimer.但是,问题是当向上或向下滚动列表视图时,计时器开始闪烁.搜索了很多,但无法得到解决方案.
我的适配器类
public class BuyListingListAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<VehicleAttributes> mVehicleList = new ArrayList<VehicleAttributes>();
private Date currentDate;
//Saving position in map to check if timer has been //started for this row
private HashMap<Integer, Boolean> timerMap = new HashMap<Integer, Boolean>();
public BuyListingListAdapter(Context context,
ArrayList<VehicleAttributes> vehicleList, boolean showGridView) {
this.mContext = context;
this.mVehicleList = vehicleList;
this.showGridView = showGridView;
currentDate = new Date(System.currentTimeMillis());
int listSize = mVehicleList.size();
for (int i = 0; i < listSize; i++)
timerMap.put(i, false); …Run Code Online (Sandbox Code Playgroud) 写作时:
CountDownTimer timer = new CountDownTimer(1000, 100)
{
@Override
public void onTick(long l)
{
}
@Override
public void onFinish()
{
};
}.start();
Run Code Online (Sandbox Code Playgroud)
我们真的开始一个处理蜱的新线程吗?如果没有,真正发生了什么?
使用相机API,我能够成功拍照并将其保存到文件夹中.这是我正在使用的代码:
main.xml中:
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
<Button
android:id="@+id/button_capture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Capture" />
Run Code Online (Sandbox Code Playgroud)
助手类:
import java.io.IOException;
import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class CameraPreview extends SurfaceView implements
SurfaceHolder.Callback {
private SurfaceHolder mSurfaceHolder;
private Camera mCamera;
// Constructor that obtains context and camera
@SuppressWarnings("deprecation")
public CameraPreview(Context context, Camera camera) {
super(context);
this.mCamera = camera;
this.mSurfaceHolder = this.getHolder();
this.mSurfaceHolder.addCallback(this);
this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
try {
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.setDisplayOrientation(90);
mCamera.startPreview();
} catch (IOException e) { …Run Code Online (Sandbox Code Playgroud) countdowntimer ×10
android ×8
background ×1
baseadapter ×1
c ×1
c# ×1
database ×1
java ×1
performance ×1
service ×1
timer ×1