- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个每 5 分钟运行一次的 IntentService。此服务检查服务器上是否有任何警报。警报位于二维数组列表中,我将其传递给 Activity 以显示在列表中。
目前,如果我启动该应用程序并让它运行一个小时,堆栈中将有 12 个 Activity。用户必须单击后退按钮 12 次才能关闭所有 Activity 。
我希望发生的事情是将新的 ArrayList 传递给堆栈上的原始 Activity,并使用新数据更新它的 listView。
我确定我必须覆盖 onNewIntent 并在适配器上调用 notifyDataSetChanged。
有没有人有任何想法如何做到这一点?我在正确的路线上吗?
public class ShowAlertsIntentService extends IntentService{
private static final String TAG = ShowAlertsIntentService.class.getSimpleName();
RROnCallApplication rrOnCallApp;
DateTime from;
DateTime to;
TwoDimensionalArrayList<String> alertsArray;
public ShowAlertsIntentService() {
super("ShowAlertsIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.e(TAG, "inside onHandleIntent of ShowAlertsIntentService");
rrOnCallApp = (RROnCallApplication) getApplication();
from = new DateTime();
to = from.plusDays(1);
DateTimeFormatter fmt = DateTimeFormat.forPattern("d-MMM-Y");
String formattedfrom = fmt.print(from);
String formattedTo = fmt.print(to);
alertsArray = rrOnCallApp.webService.getAlerts("1", formattedfrom, formattedTo);
if(alertsArray != null){
Log.e(TAG, "size of alertArray = " + alertsArray.size());
String noCallsStatus = null;
String outOfRangeStatus = null;
ArrayList arrayList = (ArrayList) alertsArray.get(0);
noCallsStatus = (String) arrayList.get(0);
if((alertsArray.size() == 1) && (noCallsStatus.equalsIgnoreCase("no sig") || noCallsStatus.equalsIgnoreCase("no data"))){
//do nothing
Log.e(TAG, "no data or no sig sent back when getting alerts");
}else{
Intent intentShowAlertsActivity = new Intent(this, ShowAlertsActivity.class);
Bundle b = new Bundle();
b.putSerializable("alertsarray", alertsArray);
intentShowAlertsActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intentShowAlertsActivity.putExtra("alertsarraybundle", b);
startActivity(intentShowAlertsActivity);
}
}else{
Log.e(TAG, "alertsArray = null!!!!!");
}
}
}
.
public class AlertsFragment extends ListFragment{
private static final String TAG = AlertsFragment.class.getSimpleName();
MySimpleArrayAdapter myAdapter;
ArrayList<String> alertsArray;
TextView alertTitle;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle b = getArguments();
if(b != null){
alertsArray = (ArrayList<String>) b.get("alertsArray");
Log.e(TAG, "alertsArray in AlertsFragment has size " + alertsArray.size());
}else{
Log.e(TAG, "Bundle b = null!!!!!!!!!!!");
}
}//end of onCreate
.[编辑1]
07-07 15:46:26.056: E/AndroidRuntime(8253): FATAL EXCEPTION: IntentService[ShowAlertsIntentService]
07-07 15:46:26.056: E/AndroidRuntime(8253): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
07-07 15:46:26.056: E/AndroidRuntime(8253): at android.app.ContextImpl.startActivity(ContextImpl.java:864)
07-07 15:46:26.056: E/AndroidRuntime(8253): at android.content.ContextWrapper.startActivity(ContextWrapper.java:276)
07-07 15:46:26.056: E/AndroidRuntime(8253): at com.carefreegroup.rr3.carefreeoncall.ShowAlertsIntentService.onHandleIntent(ShowAlertsIntentService.java:90)
07-07 15:46:26.056: E/AndroidRuntime(8253): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
07-07 15:46:26.056: E/AndroidRuntime(8253): at android.os.Handler.dispatchMessage(Handler.java:99)
07-07 15:46:26.056: E/AndroidRuntime(8253): at android.os.Looper.loop(Looper.java:137)
07-07 15:46:26.056: E/AndroidRuntime(8253): at android.os.HandlerThread.run(HandlerThread.java:60)
.
[编辑 2]
<activity
android:name=".ShowAlertsActivity"
android:screenOrientation="landscape" />
编辑3
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.e(TAG, "Inside onNewIntent");
alertsArray = (ArrayList<String>) getIntent().getBundleExtra("alertsarraybundle").get("alertsarray");
Log.e(TAG, "size of alertArray in ShowAlertsActivity = " + alertsArray.size());
Bundle b = new Bundle();
b.putSerializable("alertsArray", alertsArray);
Fragment newFragment = new AlertsFragment();
newFragment.setArguments(b);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.showalertslistfragment_container, newFragment);
//transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
最佳答案
为 Android list 中的 Activity 使用 android:launchMode="singleInstance"
。这将确保仅维护 Activity 的单个实例,并且用户不必单击 12 次。
查看此答案以获取更多详细信息:
Android singleTask or singleInstance launch mode?
编辑 1:如果您的 Activity 已经在运行,您将在 onNewIntent() 方法中收到新的 Intent 。您可以在那里刷新您的列表。
关于Android 如何在 Activity 已经运行时将数组从 IntentService 传递给 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24613214/
我有一个包含很多歌曲的 ListView 。每次点击一首歌曲时,我都会调用一个 IntentService 来按顺序下载这首歌曲。 但有时我想取消下载(例如:第 5 次)。这意味着我需要停止第 5 个
我有这个 IntentService (HttpService),它从网络服务中获取原始 json 数据: public class HttpService extends IntentService
我想将回调传递给 IntentService。这是我当前的代码: 我的回调接口(interface) import android.os.Message; public interface Sampl
在我的 Android 应用程序中,我有一个带有适配器的简单 ListView 。有一个繁重的查询是用数据填充 ListView 。所以我把它放到另一个线程中运行的 IntentService 中。
我只想写一个“Hello World IntentService App”。不幸的是,我无法做到这一点。 问题: MyIntentService.enqueueWork() 方法不起作用。 Inten
我有 5 个 IntentServices,它们在与主 UI 不同的时间和单独的进程中运行。当服务完成时,它们通过 Intent 将数据发送到主进程。我想知道 5 个服务是否占用了我的应用程序过多的内
我正在构建一个应用程序,通知将在特定时间响起,如果无人看管 15 分钟,通知就会消失。当我插入设备并运行代码时它就可以工作。然而,一旦我拔掉设备并运行应用程序,通知就会起作用,但如果无人看管,它不会在
我构建了一个应用程序,它使用 IntentService 来操作应用程序的数据库和调用网络服务。 例子: 我有一个 Activity ,它是一个项目列表。此列表由 CursorAdapter 填充。每
我遇到了一个问题。 IntentService UpdateService02 偶尔会运行失败。我放入了日志条目以便进行调试,这就是我得到的... 02-28 21:37:32.461: Main -
我正在使用 IntentService 执行后台任务,例如从远程服务器更新数据。 在某些情况下,同一个请求可以被用户多次排队,但我只想执行一次(连续两次从服务器更新数据没有意义)。 是否有使用 Int
我正在使用 Android AlarmManger 安排 IntentService 在较小的间隔后运行。 这是我的 AlarmManger: static void setNextSchedule(
我的 IntentService 看起来像这样: public class MusicService extends IntentService{ final static String NA
我遇到了一个奇怪的情况,我构建的对象在方法 onHandleIntent 中返回为 null,我不知道为什么或如何解决它。 我的 Activity @Override protected vo
我搜索了很多,但我不知道如何在单击按钮时停止 IntentService。我有一个启动 IntentService 的按钮,然后 progressDialog 显示通过 ResultReceiver
我正在尝试用 Kotlin 编写一个 CallRecorder 应用程序。我想要做的是开始使用 BroadcastReceiver 启动的服务录制音频。我认为我在 MediaRecorder 初始化方
只是想阐明我对 IntentService 在达到 terminating 状态后如何由操作系统管理的理解。通过终止,我的意思是当当前 Activity 被销毁或应用程序进程被终止时,根据以下文档:
我正在使用 IntentService 访问不同的网络服务方法并更新我的 ContentProvider。我看到的问题是,有时,我向 IntentService 发送了一个新的 Intent,但是 I
我想我明白了 IntentService 类的意义。它处理一个队列,防止太多或更多的服务实例一起执行。 我发现它很有用,但是如果我从 IntentService 派生了太多具有不同用途的类,并且我希望
我有一个 IntentService,它在启动时启动并在后台保持运行。没有启动器 Activity,IntentService 由广播接收器调用。它适用于 2.3.4 和 4.0.4 设备,但不适用于
我编写了一个发送命令行的 IntentService。与我尝试对控制台进行编程的 Activity 有关。该 Activity 的目的是执行“命令提示符”,我可以在其中向服务器发送消息或从服务器接收消
我是一名优秀的程序员,十分优秀!