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

时间:2014-07-07 14:37:53

标签: android listview android-intentservice

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

如果我启动应用程序并让它运行一小时,那么堆栈上将有12个活动。用户必须单击后退按钮12次以关闭所有活动。

我想要发生的是将新的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

。 [EDIT1]

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" />

EDIT3

@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();


    }

1 个答案:

答案 0 :(得分:1)

android:launchMode="singleInstance"用于Android Manifest中的活动。这样可以确保只维护一个活动实例,并且用户不必单击12次。

查看此答案以获取更多详细信息:

Android singleTask or singleInstance launch mode?

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

查看此答案:https://stackoverflow.com/a/5810336/1239966