- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个小应用程序可以计算到指定日期和时间的时间。它运作良好。
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.widget.DigitalClock;
import android.widget.TextView;
public class Day2GoActivity extends Activity {
private TextView tvWeeks, tvDays, tvHours, tvMinutes, tvSeconds, timeUntil;
public Date curDateTime = new Date();
public Calendar today = Calendar.getInstance();
public Calendar leaving = Calendar.getInstance();
protected SimpleDateFormat sdf = new SimpleDateFormat("E dd-MMM-yyyy HH:mm");
protected Handler h = new Handler();
protected Runnable r = new Runnable() {
public void run() {
afficher();
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvDays = (TextView) findViewById(R.id.tvDays);
timeUntil = (TextView) findViewById(R.id.timeUntil);
tvWeeks = (TextView) findViewById(R.id.tvWeeks);
tvHours = (TextView) findViewById(R.id.tvHours);
tvMinutes = (TextView) findViewById(R.id.tvMinutes);
tvSeconds = (TextView) findViewById(R.id.tvSeconds);
}
@Override
protected void onResume() {
super.onResume();
Log.i("TAG", "Resuming");
SharedPreferences sharedPreferences = getSharedPreferences("preferences", MODE_PRIVATE);
Log.i("MyActvity", "Year = " + sharedPreferences.getInt("year", Calendar.YEAR));
leaving.set(sharedPreferences.getInt("year", Calendar.YEAR), sharedPreferences.getInt("month", Calendar.MONTH),
sharedPreferences.getInt("dayOfMonth", Calendar.DAY_OF_MONTH), sharedPreferences.getInt("hour", Calendar.HOUR_OF_DAY),
sharedPreferences.getInt("minute", Calendar.MINUTE));
timeUntil.setText("Time until: " + sdf.format(leaving.getTime()));
r.run();
Log.w("TAG", "Resumed");
}
private void afficher() {
today = Calendar.getInstance();
long millisToGo = leaving.getTimeInMillis() - today.getTimeInMillis();
long seconds2go = TimeUnit.MILLISECONDS.toSeconds(millisToGo);
tvSeconds.setText(String.format("%,d seconds", seconds2go));
long minutes2go = TimeUnit.MILLISECONDS.toMinutes(millisToGo);
tvMinutes.setText(String.format("%,d minutes", minutes2go));
long hours2go = TimeUnit.MILLISECONDS.toHours(millisToGo);
tvHours.setText(String.format("%d hours", hours2go));
tvDays.setText(String.format("%d days %d hours", (int) hours2go / 24, hours2go % 24));
long days2go = TimeUnit.MILLISECONDS.toDays(millisToGo);
tvWeeks.setText(String.format("%d weeks %d days", (int) days2go / 7, days2go % 7));
h.postDelayed(r, 1000);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Intent myIntent = new Intent(getApplicationContext(), editDate.class);
startActivity(myIntent);
return true;
}
但是,当用户更改日期和时间时,我遇到了一个问题。我有第二个 Activity
,如您所见,它在 onCreateOptionsMenu
中调用。它也可以正常工作。
import java.util.Calendar;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.TimePicker;
public class editDate extends Activity {
protected DatePicker dp1;
protected TimePicker tp1;
protected Calendar c;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit);
dp1 = (DatePicker) findViewById(R.id.datePicker1);
tp1 = (TimePicker) findViewById(R.id.timePicker1);
tp1.setIs24HourView(true);
LoadPreferences();
}
@Override
protected void onDestroy() {
super.onDestroy();
SavePreferences();
}
private void SavePreferences() {
SharedPreferences sharedPreferences = this.getSharedPreferences("preferences", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("year", dp1.getYear());
editor.putInt("month", dp1.getMonth());
editor.putInt("dayOfMonth", dp1.getDayOfMonth());
editor.putInt("hour", tp1.getCurrentHour());
editor.putInt("minute", tp1.getCurrentMinute());
editor.commit();
}
private void LoadPreferences() {
SharedPreferences sharedPreferences = this.getSharedPreferences("preferences", MODE_PRIVATE);
c = Calendar.getInstance();
try {
dp1.init(sharedPreferences.getInt("year", c.YEAR), sharedPreferences.getInt("month", c.MONTH),
sharedPreferences.getInt("dayOfMonth", c.DAY_OF_MONTH), null);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
dp1.init(2012, 5, 1, null);
}
tp1.setCurrentHour(sharedPreferences.getInt("hour", c.HOUR_OF_DAY));
tp1.setCurrentMinute(sharedPreferences.getInt("minute", c.MINUTE));
}
}
应用程序运行良好,Log cat 文件中包含所有预期的条目:
06-01 18:21:04.049: I/ApplicationPackageManager(7750): cscCountry is not German : XEU
06-01 18:21:04.079: I/TAG(7750): Resuming
06-01 18:21:04.089: I/MyActvity(7750): Year = 2014
06-01 18:21:04.089: W/TAG(7750): Resumed
06-01 18:21:05.459: W/KeyCharacterMap(7750): No keyboard for id 0
06-01 18:21:05.459: W/KeyCharacterMap(7750): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
06-01 18:21:05.519: I/ApplicationPackageManager(7750): cscCountry is not German : XEU
06-01 18:21:05.619: W/Picker(7750): beforeTextChanged: 01, 0, 2, 2
06-01 18:21:05.619: W/Picker(7750): onTextChanged: 01
06-01 18:21:05.619: W/Picker(7750): onTextChanged: 01, 0, 2, 2
06-01 18:21:05.619: W/Picker(7750): aftertextchanged: 01
06-01 18:21:05.619: W/Picker(7750): beforeTextChanged: 01, 0, 2, 2
06-01 18:21:05.619: W/Picker(7750): onTextChanged: 01
06-01 18:21:05.619: W/Picker(7750): onTextChanged: 10, 0, 2, 2
06-01 18:21:05.619: W/Picker(7750): aftertextchanged: 10
06-01 18:21:05.619: W/Picker(7750): beforeTextChanged: 2012, 0, 4, 4
06-01 18:21:05.619: W/Picker(7750): onTextChanged: 2012
06-01 18:21:05.619: W/Picker(7750): onTextChanged: 2014, 0, 4, 4
06-01 18:21:05.619: W/Picker(7750): aftertextchanged: 2014
06-01 18:21:08.239: E/DatePicker(7750): adjustMaxDat : 31 10
06-01 18:21:08.239: W/Picker(7750): beforeTextChanged: 10, 0, 2, 2
06-01 18:21:08.249: W/Picker(7750): onTextChanged: 10
06-01 18:21:08.249: W/Picker(7750): onTextChanged: 01, 0, 2, 2
06-01 18:21:08.249: W/Picker(7750): aftertextchanged: 01
06-01 18:21:08.259: W/Picker(7750): beforeTextChanged: 01, 0, 2, 2
06-01 18:21:08.259: W/Picker(7750): onTextChanged: 01
06-01 18:21:08.259: W/Picker(7750): onTextChanged: 10, 0, 2, 2
06-01 18:21:08.259: W/Picker(7750): aftertextchanged: 10
06-01 18:21:08.259: W/Picker(7750): beforeTextChanged: Jun, 0, 3, 3
06-01 18:21:08.259: W/Picker(7750): onTextChanged: Jun
06-01 18:21:08.259: W/Picker(7750): onTextChanged: Jul, 0, 3, 3
06-01 18:21:08.269: W/Picker(7750): aftertextchanged: Jul
06-01 18:21:10.759: I/TAG(7750): Resuming
06-01 18:21:10.759: I/MyActvity(7750): Year = 2014
06-01 18:21:10.759: W/TAG(7750): Resumed
但是它不会更新 TextView
的 timeUntil()
。我可以相信我在这里做错了什么,但有两个问题。首先 timeUntil()
在 onResume()
启动时更新(在 onCreate()
之后),其次 timeUntil( )
,如果您更改屏幕方向,IS 会更新。我知道屏幕重新定位会重新运行 onCreate()
,这表明 onResume()
在 onCreate()
之后工作,但不是自己跑的时候。我完全不知所措。
有没有人有什么想法?
根据 Proxy32 的输入(见下文),我已将 onResume
void 更改为
@Override
protected void onResume() {
super.onResume();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
h.post(new Runnable() {
@Override
public void run() {
Log.i("TAG", "Resuming");
SharedPreferences sharedPreferences = getSharedPreferences("preferences", MODE_PRIVATE);
Log.i("MyActvity", "Year = " + sharedPreferences.getInt("year", Calendar.YEAR));
leaving.set(sharedPreferences.getInt("year", Calendar.YEAR), sharedPreferences.getInt("month", Calendar.MONTH),
sharedPreferences.getInt("dayOfMonth", Calendar.DAY_OF_MONTH),
sharedPreferences.getInt("hour", Calendar.HOUR_OF_DAY), sharedPreferences.getInt("minute", Calendar.MINUTE));
Log.d("TAG", "date " + sdf.format(leaving.getTime()));
timeUntil.setText("Time until: " + sdf.format(leaving.getTime()));
timeUntil.invalidate();
today = Calendar.getInstance();
long millisToGo = leaving.getTimeInMillis() - today.getTimeInMillis();
long seconds2go = TimeUnit.MILLISECONDS.toSeconds(millisToGo);
tvSeconds.setText(String.format("%,d seconds", seconds2go));
long minutes2go = TimeUnit.MILLISECONDS.toMinutes(millisToGo);
tvMinutes.setText(String.format("%,d minutes", minutes2go));
long hours2go = TimeUnit.MILLISECONDS.toHours(millisToGo);
tvHours.setText(String.format("%,d hours", hours2go));
tvDays.setText(String.format("%,d days %d hours", (int) hours2go / 24, hours2go % 24));
long days2go = TimeUnit.MILLISECONDS.toDays(millisToGo);
tvWeeks.setText(String.format("%,d weeks %d days", (int) days2go / 7, days2go % 7));
}
});
}
}, 0, 1000);
Log.w("TAG", "Resumed");
}
我也删除了 afficher。这现在做了我想要它做的。问题解决了。
最佳答案
我不确定我是否理解这个问题,但我会尽力澄清。
onResume 在 onCreate 之后的 onStart(和/或 onRestart)之后被调用。这就是为什么它会在您更改屏幕配置后或应用程序首次启动时按照您的建议运行。
详情请看这里: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
这也意味着当您返回到最初的主 Activity 时,timeUntil 应该被更新。但是,我没有在您的代码中看到您在哪里完成补充 Activity 。另外,“自行运行”是什么意思?
关于java - onResume 在从其他 Activity 返回时未按预期表现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10854148/
假设我有 3 个 Fragment,BaseFragment,以及第一个 fragment 和第二个 fragment 。 我已经实现了 BaseFragment 的 onResume() 并编写了一
这是我的第一个应用程序,所以这个问题/答案可能非常基本。我目前有 onPause(); ,当玩家离开屏幕时停止播放音乐。我尝试做类似的事情,但使用 onResume,以便再次播放音乐(backgrou
如何重载onResume()以正确的方式工作?我想从 activity 返回到 MainActivity ,我希望在其中具有与应用程序启动后相同的状态。我想使用 recreate() 但它循环了或者类
出于某种原因,当我在代码中声明 onResume() 时,它会无缘无故地让我的程序崩溃,甚至无法打开。 这是我的 onResume() 代码: public void onResume() {
我遇到了一个棘手的问题。我有两个标签页主机 ...标签页主机 A 和标签页主机 B。每个选项卡主机都有三个选项卡(我们称它们为 A1、B1、A2、B2 等)。 选项卡主机 B 从 Activity A
我的应用程序有一个问题,如果我返回到一个 Activity ,我会收到一条错误消息,指出数据库已关闭: ERROR/AndroidRuntime(3566): Caused by: java.lang
在我的 android 应用程序中,我有一个问题已经 3 天无法解决了。我阅读了很多帖子和答案,但仍然找不到我的解决方案。 我的问题是当我按下主页按钮并在很长时间后返回应用程序时它崩溃了(NullPo
我如何实现一个无限滚动到 onResume(),目前是 onCreateView,我实现了一个无限滚动,它工作得很好,但是当涉及到 onResume() 时,它在另一个显示空白数据时不起作用。 这是o
我在我的 Android 应用程序中使用谷歌地图。一切都很好,但是当我没有启用 GPS 服务时,我会显示一条消息来启用。当我启用 GPS 并在我的 Fragment der map 中返回时,仅当我按
我一直在搜索这个,但不知道会发生什么...... 当我回到上一个 Activity 时,我的组件已正确初始化,但它们上面有一个新组件... 我的意思是,在 Debug模式下,我遵循并执行了 onRes
我读过一些与我的问题相似的帖子,最相似的问题在这里:Strange onPause(), onResume() behaviour in fragments 我正在使用 FragmentPagerAd
我已经阅读了关于 onResume() 和 onStart() 的文档,但我仍然不明白的一件事是 onResume() 在没有 onStart() 之前被调用? 最佳答案 请引用Android Act
我正在尝试制作一个简单的记事本应用程序,我想在“新建笔记” Activity 完成且主屏幕恢复时刷新笔记。但是,当我尝试使用此代码打开应用程序时,我会强制关闭。如果我删除 OnResume 东西,它不
这个问题在这里已经有了答案: Android: Under what circumstances would a Dialog appearing cause onPause() to be cal
我有一个问题。在为预览初始化相机并将另一个应用程序聚焦后,然后返回我的应用程序:预览显示为黑色。如果我继续拍照,它会拍摄我正常指向相机的位置的照片。 我在 OnResume() 覆盖上做错了什么吗?相
我正在使用列表适配器来显示不同的商店,当有人选择商店时,它会将他们带到一个新的 Activity 中,他们可以在该 Activity 中将商店添加到该屏幕上的收藏夹。 有一个调用 finish();
我有一个 Intent,它通过一个长的 Extra 从我的数据库中获取信息。它还通过 String Extra 设置 Activity 的标题。 现在,当我第一次启动 Activity 时,一切都完美
所以我有我的主要 Activity ,当您按下按钮时,它会启动 Activity 2。启动 activity2 后,我按下主页按钮,它将在 activity2 触发 onPause,然后 androi
我正在编写一个购物车应用,其中我允许用户选择要购买的商品并将他们需要的商品添加到购物车,为此我给出了两种不同的标签, 第一个标签,浏览产品 第二个标签,浏览购物车中的商品 [用户添加] 这里我遇到了一
我有问题。当我第一次启动我的 android 应用程序时,在主要 Activity 中 onCreate 和 onResume 都会被调用。但我只想被称为 onCreate。 我能做什么? 最佳答案
我是一名优秀的程序员,十分优秀!