简体   繁体   中英

Android how to pass array to activity from IntentService when Activity is already running

I have an IntentService that runs every 5 minutes. This service checks if there are any Alerts on the server. The Alerts are in a TwoDimensional ArrayList which i pass to an Activity to display in a list.

At the moment if i start the app and let it run for an hour, there will be 12 Activities on the stack. The user would have to click the back button 12 times to dismiss all the activities.

What i would like to happen is the new ArrayList gets passed to the original Activity on the stack and it's listView gets updated with the new data.

I'm sure i would have to override onNewIntent and call notifyDataSetChanged on the adapter.

Has anyone any ideas how to do this? Am i on the right lines?

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)

.

[edit 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();


    }

Use android:launchMode="singleInstance" for the activity in the Android Manifest. This'll ensure that only a single instance of the activity is maintained and the user won't have to click 12 times.

Check this answer for more details :

Android singleTask or singleInstance launch mode?

Edit 1 : If your activity is already running, you'll receive the new intent in onNewIntent() method. You can refresh your list there.

Check out this answer : https://stackoverflow.com/a/5810336/1239966

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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