Android如何在活动已经运行时将数组从IntentService传递给活动

Android如何在活动已经运行时将数组从IntentService传递给活动,android,listview,android-intentservice,Android,Listview,Android Intentservice,我有一个每5分钟运行一次的IntentService。此服务检查服务器上是否有任何警报。警报位于二维ArrayList中,我将其传递给活动以在列表中显示 目前,如果我启动应用程序并让它运行一个小时,堆栈上将有12个活动。用户必须单击后退按钮12次才能取消所有活动 我希望新的ArrayList被传递到堆栈上的原始活动,并且它的listView被新数据更新 我确信我必须重写onNewIntent并调用适配器上的notifyDataSetChanged 有人知道怎么做吗? 我说的对吗 public c

我有一个每5分钟运行一次的IntentService。此服务检查服务器上是否有任何警报。警报位于二维ArrayList中,我将其传递给活动以在列表中显示

目前,如果我启动应用程序并让它运行一个小时,堆栈上将有12个活动。用户必须单击后退按钮12次才能取消所有活动

我希望新的ArrayList被传递到堆栈上的原始活动,并且它的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!!!!!");

            }



    }

}

[编辑2]

<activity
            android:name=".ShowAlertsActivity"
            android:screenOrientation="landscape" />

编辑3

@覆盖
受保护的void onNewIntent(意图){
super.onNewIntent(意向);
Log.e(标签“内部帐篷”);
alertsArray=(ArrayList)getIntent().getBundleExtra(“alertsarraybundle”).get(“alertsArray”);
Log.e(标记“ShowAlertsActivity=“+alertsArray.size()”)中alertArray的大小);
Bundle b=新Bundle();
b、 可编程(“alertsArray”,alertsArray);
Fragment newFragment=新警报片段();
newFragment.setArguments(b);
FragmentTransaction=getFragmentManager().beginTransaction();
//用这个片段替换fragment_容器视图中的任何内容,
//并将事务添加到后堆栈中
事务.replace(R.id.showartslitfragment\u容器,newFragment);
//transaction.addToBackStack(空);
//提交事务
commit();
}

对android清单中的活动使用
android:launchMode=“singleInstance”
。这将确保只维护活动的一个实例,用户不必单击12次

查看此答案以了解更多详细信息:

编辑1:如果您的活动已经在运行,您将在onNewIntent()方法中收到新的意图。你可以在那里刷新你的列表


查看以下答案:

为什么要标记活动和新任务?每次它都会启动一个新的活动,并将之前的活动向下推。@RanjitPati我不记得为什么,因为我很久以前就编写了它。我想Android说,当从后台线程启动活动时,我必须设置这个标志?好的。然后删除这一行并检查一次。这可能会解决你的问题。好的,我已经注释掉了它,我在edit1中得到了错误。你的活动在清单中是如何定义的?我已经注释掉了服务中的标志\u Activity\u NEW\u TASK。我在清单中使用android:launchMode=“singleInstance”作为actrivity,并且我已经在活动中删除了onNewIntent。我仍然在编辑中遇到错误1这里有一个完全不同的方法可以肯定有效,但在此之前,请尝试使用
Intent.FLAG\u FROM\u BACKGROUND
代替
Intent.FLAG\u ACTIVITY\u NEW\u TASK)我的错误,现在可以了。我以为onNewIntent从未执行过,但我没有等到我的活动的最后一个实例已经在堆栈上。我是个白痴!非常感谢。我将Intent.FLAG_ACTIVITY_NEW_任务与android:launchMode=“singleInstance”结合使用,并在活动中覆盖onNewIntent。
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)
<activity
            android:name=".ShowAlertsActivity"
            android:screenOrientation="landscape" />
@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();


    }