前台服务通知未发送ArrayList值

时间:2019-02-28 11:14:51

标签: android service foreground foreground-service

基本问题是我在此服务中创建了一个前台服务,我使用ScheduledExecutorService通过Redis从服务器获取数据,关键是当用户退出应用程序时我希望数据存储在ArrayList中,但是当他单击时来自前台服务的通知我想将数据传递给片段,因此,当我尝试执行此操作时,每次获得空ArrayList时,只有在我在Service的onCreate方法中创建通知之前手动将值添加到ArrayList,也许是因为发生了,因为在执行onCreate方法时,他创建了通知并通过意图传递了ArrayList的当前状态,并且以后不再添加ArrayList的值。

希望我的解释足够好,您可以理解。 任何想法如何解决这个问题?

这是我用于前台服务的代码:我使用了可序列化的bundle put来传递ArrayList,并在片段中获取数据,但是当用户单击通知时,如何发送更新的ArrayList(在1分钟后说)。

感谢进阶!

  if (Build.VERSION.SDK_INT >= 26)
    {

        //CONTEXT == THIS



   bundle= new Bundle();





        String s1=""+testArray.size();




        bundle.putSerializable("lat",latService);
        bundle.putSerializable("lng",lngService);




        Intent intent = new Intent(this, TrackingActivityDrawer.class);
        intent.putExtras(bundle);
        intent.putExtra("testT","testT");
        intent.putExtra("size",s1);






        //Broadcast  Receiver
        //          Intent broad= new Intent ("name");
        //          broad.putExtra("name","name");
        //        sendBroadcast(broad);




        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);



        //github code
        String CHANNEL_ID = "my_channel_01";
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                "Channel human readable title",
                 NotificationManager.IMPORTANCE_DEFAULT);




        //.setContentIntent(pendingIntent);
        ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setSmallIcon(R.drawable.beni)
                .setTicker("RANDOM")
                .setContentTitle("aasgasgag")
                .setContentIntent(pendingIntent)
                .setContentText("dfgdfgfagfdgfgdfgdfgdfg").build();



        startForeground(1, notification);



    }else
    {





        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setContentTitle(getString(R.string.app_name))
                .setContentText("text")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setAutoCancel(true);

        Notification notification = builder.build();

        startForeground(1, notification);



    }

0 个答案:

没有答案