【问题标题】:Android how to pass array to activity from IntentService when Activity is already runningAndroid如何在Activity已经运行时将数组从IntentService传递给Activity
【发布时间】:2014-07-07 14:37:53
【问题描述】:

我有一个每 5 分钟运行一次的 IntentService。此服务检查服务器上是否有任何警报。警报位于二维 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

。 [编辑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();


    }

【问题讨论】:

  • 为什么选择 FLAG_ACTIVITY_NEW_TASK ?每次它都会开始一个新的活动并将前一个活动向下推..
  • @RanjitPati 我不记得为什么现在因为我在很久以前编写了它。我认为 Android 说我必须在从后台线程启动 Activity 时设置此标志?
  • ok..然后删除这一行并检查一次..也许它会解决你的问题..
  • 好的,我已经把它注释掉了,我在edit1中得到了错误
  • Manifest 中如何定义您的 Activity?

标签: android listview android-intentservice


【解决方案1】:

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

查看此答案了解更多详情:

Android singleTask or singleInstance launch mode?

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

看看这个答案:https://stackoverflow.com/a/5810336/1239966

【讨论】:

  • 我已经注释掉了服务中的 FLAG_ACTIVITY_NEW_TASK。我正在使用 android:launchMode="singleInstance" 作为清单中的活动,并且我已经在活动中使用了 onNewIntent。我仍然在edit1中遇到错误
  • 有完全不同的方法肯定会奏效,但在此之前,请尝试使用Intent.FLAG_FROM_BACKGROUND 代替Intent.FLAG_ACTIVITY_NEW_TASK);
  • 我的错误它现在有效。我以为 onNewIntent 永远不会执行,但我并没有等到我的活动的至少一个实例已经在堆栈上。是我当白痴!非常感谢。
  • 我将 Intent.FLAG_ACTIVITY_NEW_TASK 与 android:launchMode="singleInstance" 结合使用并在 Activity 中覆盖 onNewIntent。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-14
  • 1970-01-01
  • 2015-08-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多