1

5 分ごとに実行される IntentService があります。このサービスは、サーバーにアラートがあるかどうかを確認します。アラートは、リストに表示するアクティビティに渡す TwoDimensional ArrayList にあります。

現時点で、アプリを起動して 1 時間実行すると、スタックに 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!!!!!");

            }



    }

}

.

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

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


    }
4

1 に答える 1

1

android:launchMode="singleInstance"Android マニフェストのアクティビティに使用します。これにより、アクティビティのインスタンスが 1 つだけ維持され、ユーザーが 12 回クリックする必要がなくなります。

詳細については、この回答を確認してください。

Android singleTask または singleInstance 起動モード?

編集 1: アクティビティが既に実行されている場合は、onNewIntent() メソッドで新しいインテントを受け取ります。そこでリストを更新できます。

この回答を確認してください: https://stackoverflow.com/a/5810336/1239966

于 2014-07-07T14:50:28.077 に答える