简体   繁体   English

在Android中按服务恢复活动

[英]Resume Activity by service in Android

I want to develop a program which reminds word (english-turkish). 我想开发一个程序来提醒单词(英语-土耳其语)。 The things what I have to do below and please correct me if I'm wrong or bad way I'm using. 我在下面必须做的事情,如果我使用的方法有误或错误,请纠正我。

  1. Create and Main.java class as an activity which includes a textview to show english word 创建和Main.java类作为一个活动,其中包括一个显示英语单词的textview
  2. Create another activity which shows Preferences to setup interval time to remind new words 创建另一个活动,该活动显示“首选项”以设置间隔时间以提醒新单词
  3. Write some code under the Save button click(inside the Prefs.java class) to save settings to SharedPreferences 在“保存”按钮单击(在Prefs.java类内部)下编写一些代码,以将设置保存到SharedPreferences
  4. Inflate menu inside Main activity to show Preferences Activity 在主活动中添加菜单以显示首选项活动
  5. Create a service with MyService name. 创建一个具有MyService名称的服务。
  6. Get interval from SharedPreferences inside the OnCreate method of MyService . MyService的OnCreate方法内的SharedPreferences获取间隔。
  7. Inside the OnStart method Run a Timer according to interval and continousley connect to web service to get a new word. 在OnStart方法中,根据时间间隔运行一个Timer,然后continousley连接到Web服务以获取一个新单词。
  8. Periodically bring to front Main activity(don't want to create every time from the begining, just want to resume activity) and show new word. 定期展示主要活动(不想从头开始每次创建,只想恢复活动)并显示新单词。
  9. When pressed New Word use the service's function to connect and retrieve new word and show in TextView in Main activity 当按下New Word使用服务的功能连接和检索新单词并在Main活动的TextView中显示
  10. When pressed Ok set Activity to Pause mode and show Home Screen 按下Ok将活动设置为暂停模式并显示主屏幕

I have some difficulties to Resume Main activity and passing new word. 我在恢复主要活动和传递新单词方面遇到一些困难。

Do you know a way to bring front Main activity periodically while it is in resume state? 您是否知道一种在恢复状态下定期进行主要活动的方法?

Try to make an intent in the onResume() call with this flag: FLAG_ACTIVITY_REORDER_TO_FRONT which causes the launched activity to be brought to the front.. 尝试使用以下标志在onResume()调用中添加意图:FLAG_ACTIVITY_REORDER_TO_FRONT,这将导致启动的活动被带到最前面。

For more information click here . 有关更多信息,请单击此处

There is no difference between starting activity and bringing them to foreground - read about activity lifecycle. 开始活动与将其置于前台之间没有什么区别-了解活动生命周期。 Service is ineffective if you want to use it as simple timer. 如果您想将其用作简单计时器,该服务将无效。 Much better approach is to use AlarmManager and scheduling next activity start. 更好的方法是使用AlarmManager并计划下一个活动开始。 Here you have an example. 这里有一个例子。 Then just override Activity.onStart() method, to fill all fields you want. 然后,只需重写Activity.onStart()方法即可填充所需的所有字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM