当Activity已经在运行时,Android如何将数组从IntentService传递给Activity

乌龟

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

目前,如果我启动该应用程序并使其运行一个小时,则堆栈上将有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" />

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


    }
西瓦姆·维玛(Shivam Verma)

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

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

Android singleTask或singleInstance启动模式?

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

当Activity已经在运行时,Android如何从IntentService将数组传递给Activity

来自分类Dev

在运行时将变量传递给方法

来自分类Dev

如何将StatusBarNotification传递给Activity

来自分类Dev

如何在运行时将indexed_property的值传递给索引?

来自分类Dev

如何在运行时将模型传递给partialview?

来自分类Dev

如何在运行时将值从窗口传递给用户控件?

来自分类Dev

为什么C ++允许在运行时将数组大小传递给函数以构造固定大小的数组?

来自分类Dev

如何将运行时参数传递给EF Core谓词表达式

来自分类Dev

通过cygwin从Shell脚本运行时,如何将参数传递给Powershell脚本?

来自分类Dev

使用bash脚本运行时如何将参数传递给python脚本?

来自分类Dev

如何在运行时将命令行参数(字符串数组类型)传递给 servlet init 方法

来自分类Dev

Python:仅在运行时确定列表时,如何将可迭代列表传递给zip?

来自分类Dev

如何在运行时使用简单的注入器将值传递给实例集合的构造函数?

来自分类Dev

当python脚本已经在运行时,从Java调用python函数

来自分类Dev

如何将 MethodChannel 结果传递给新的 Activity?

来自分类Dev

使用void *指针将未知/混合类型传递给printf()(在运行时推断类型)

来自分类Dev

对于每个调用,在运行时将连接字符串传递给Entity Framework

来自分类Dev

在运行时将参数传递给std :: bind到调度队列

来自分类Dev

在运行时将txt文件作为参数传递给脚本

来自分类Dev

Python:在运行时将任意值传递给 chrome 扩展中的 js 函数

来自分类Dev

如何将Integer传递给Android Studio上的另一个Activity

来自分类Dev

如何将按钮单击事件传递给Android 中PagedList 中的Activity?

来自分类Dev

当应用程序已经在运行时,如何在基于py2app的Python应用程序中接受“打开文档”事件?

来自分类Dev

android将activity.this传递给方法

来自分类Dev

Java:在运行时将类作为参数传递

来自分类Dev

从脚本运行时,如何将用户定义的参数传递给scrapy Spider

来自分类Dev

如何在运行时将java中的字符串参数作为参数传递

来自分类Dev

如何在运行时将java中的字符串参数作为参数传递

来自分类Dev

如何在运行时将参数从ActionLink传递到控制器

Related 相关文章

  1. 1

    当Activity已经在运行时,Android如何从IntentService将数组传递给Activity

  2. 2

    在运行时将变量传递给方法

  3. 3

    如何将StatusBarNotification传递给Activity

  4. 4

    如何在运行时将indexed_property的值传递给索引?

  5. 5

    如何在运行时将模型传递给partialview?

  6. 6

    如何在运行时将值从窗口传递给用户控件?

  7. 7

    为什么C ++允许在运行时将数组大小传递给函数以构造固定大小的数组?

  8. 8

    如何将运行时参数传递给EF Core谓词表达式

  9. 9

    通过cygwin从Shell脚本运行时,如何将参数传递给Powershell脚本?

  10. 10

    使用bash脚本运行时如何将参数传递给python脚本?

  11. 11

    如何在运行时将命令行参数(字符串数组类型)传递给 servlet init 方法

  12. 12

    Python:仅在运行时确定列表时,如何将可迭代列表传递给zip?

  13. 13

    如何在运行时使用简单的注入器将值传递给实例集合的构造函数?

  14. 14

    当python脚本已经在运行时,从Java调用python函数

  15. 15

    如何将 MethodChannel 结果传递给新的 Activity?

  16. 16

    使用void *指针将未知/混合类型传递给printf()(在运行时推断类型)

  17. 17

    对于每个调用,在运行时将连接字符串传递给Entity Framework

  18. 18

    在运行时将参数传递给std :: bind到调度队列

  19. 19

    在运行时将txt文件作为参数传递给脚本

  20. 20

    Python:在运行时将任意值传递给 chrome 扩展中的 js 函数

  21. 21

    如何将Integer传递给Android Studio上的另一个Activity

  22. 22

    如何将按钮单击事件传递给Android 中PagedList 中的Activity?

  23. 23

    当应用程序已经在运行时,如何在基于py2app的Python应用程序中接受“打开文档”事件?

  24. 24

    android将activity.this传递给方法

  25. 25

    Java:在运行时将类作为参数传递

  26. 26

    从脚本运行时,如何将用户定义的参数传递给scrapy Spider

  27. 27

    如何在运行时将java中的字符串参数作为参数传递

  28. 28

    如何在运行时将java中的字符串参数作为参数传递

  29. 29

    如何在运行时将参数从ActionLink传递到控制器

热门标签

归档