我制作了一个简单的Android音乐播放器.我希望有一个TextView,以分钟为单位显示歌曲中的当前时间:秒格式.所以我尝试的第一件事就是让活动Runnable并把它放在run()中:
int position = 0;
while (MPService.getMP() != null && position<MPService.duration) {
try {
Thread.sleep(1000);
position = MPService.getSongPosition();
} catch (InterruptedException e) {
return;
}
// ... convert position to formatted minutes:seconds string ...
currentTime.setText(time); // currentTime = (TextView) findViewById(R.id.current_time);
Run Code Online (Sandbox Code Playgroud)
但这失败了因为我只能在创建它的线程中触摸TextView.所以我尝试使用runOnUiThread(),但是这不起作用,因为在主线程上重复调用Thread.sleep(1000),因此活动只挂在空白屏幕上.那么我有什么想法可以解决这个问题?
新代码:
private int startTime = 0;
private Handler timeHandler = new Handler();
private Runnable updateTime = new Runnable() {
public void run() {
final int start = startTime;
int millis = appService.getSongPosition() - start;
int seconds = (int) ((millis …Run Code Online (Sandbox Code Playgroud) 我正在尝试让我的倒计时应用程序执行其更新我当前时间和日期TextView的功能,并每秒触发其MyOnItemSelectedListener,以便应用程序动态倒计时而不是仅在启动onCreate时.如果有一个更有效的方法然后快速解雇MyOnItemSelectedListener我会很感激批评.
public class TheCount extends Activity {
TextView description=null;
TextView dates=null;
TextView times=null;
TextView output=null;
TextView cDateDisplay=null;
TextView cTimeDisplay=null;
private int mYear;
private int mMonth;
private int mDay;
private int mHour;
private int mMinute;
private int mSecond;
CountdownHelper helper=null;
String countdownId=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.thecount);
helper=new CountdownHelper(this);
cTimeDisplay = (TextView)findViewById(R.id.lblCurTime);
cDateDisplay = (TextView)findViewById(R.id.lblCurDate);
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
mSecond = c.get(Calendar.SECOND); …Run Code Online (Sandbox Code Playgroud) 你好,我想做每1秒呼叫功能或做其他事情的apliacation.我有这个代码不工作你能说出什么问题吗?
public class App5_Thread extends Activity implements Runnable {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Thread thread = new Thread(this);
thread.start();
}
@Override
public void run() {
TextView tv1 = (TextView) findViewById(R.id.tv);
showTime(tv1);
try {
Thread.sleep(1000);
}catch (Exception e) {
tv1.setText(e.toString());
}
}
public void showTime(TextView tv1 ){
String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
tv1.setText(sdf.format(cal.getTime())+" "+System.currentTimeMillis());
}
Run Code Online (Sandbox Code Playgroud)
}
我正在按照本教程为 Android 创建自定义表盘。我已经实现了广播接收器来检测时间变化,如下所示:
在我的活动中,我有静态块来过滤以下意图:
static {
intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_TIME_CHANGED);
intentFilter.addAction(Intent.ACTION_TIME_TICK);
intentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
}
Run Code Online (Sandbox Code Playgroud)
我的接收者类别:
public class MyReciever extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
c = Calendar.getInstance();
Log.d("myapp", "time changed");
hrs = c.get(Calendar.HOUR_OF_DAY);
min = c.get(Calendar.MINUTE);
sec = c.get(Calendar.SECOND);
txt_hrs.setText(String.valueOf(hrs));
txt_mins.setText(String.valueOf(min));
txt_sec.setText(String.valueOf(sec));
}
}
Run Code Online (Sandbox Code Playgroud)
我已经在 oncreate() 中注册了接收器:
MyReciever myReciever = new MyReciever();
registerReceiver(myReciever,intentFilter);
Run Code Online (Sandbox Code Playgroud)
上面的代码在几小时和几分钟内工作正常,但在几秒钟内不起作用。
问题Intent.ACTION_TIME_TICK是它每分钟广播一次,而不是每秒一次。
我需要检测表盘上时钟每秒的时间变化。有人有“检测每秒时间变化”的解决方案吗?
android android-intent android-broadcast watch-face-api wear-os