【问题标题】:extends BroadcastReceiver onReceive function Can't get static ArrayList from other class扩展 BroadcastReceiver onReceive 函数无法从其他类获取静态 ArrayList
【发布时间】:2018-11-02 13:15:40
【问题描述】:

这是接收者

    public class Alarm extends BroadcastReceiver
      {

    @Override
    public void onReceive(Context context, Intent intent)
    {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");

        wl.acquire();

 NotificationClass.createNotification("Total Item In List is :"+Globals.productList.size(),context);
        wl.release();
    }

    public void setAlarm(Context context) {
        Log.i("ALARM", "Alarm has been set it");
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(context, Alarm.class);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 1 * 1, pi); 

    }

    public void cancelAlarm(Context context)
    {
        Log.i("ALARM","Alarm has been Cancel");
        Intent intent = new Intent(context, Alarm.class);
        PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(sender);
    }

这里是全局类

public class Globals  {
  public static List <ProductClass> productList=new ArrayList<ProductClass>();
public static String testArray[]={"test1","test2","test3"};
}

App 没有崩溃,但是 onReceive 函数 Globals.productList.size 返回 0,我可以从 testArray 中获取数据。 productList 数组列表也是静态的,我可以在除 onReceuve 函数之外的所有活动中从 Globals.ProductList 获取数据。

那么,我怎样才能从 Global static ArrayList 中获取和获取数据?

MainActivity 类

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

            GetDataClass.getAllData();
  List <ProductList> pList=new ArrayList<ProductList>();
                pList=GetDataClass.data;
        for(int i=0;i<GetDataClass.data.size();i++)
        {

            ProductList temp=new ProductList();

            temp.set(pList.get(i).pName,pList.get(i).stock,
                    pList.get(i).provider,pList.get(i).jk,pList.get(i).starCounter);

            Globals.productList.add(temp);
        }
        Log.i("Count", String.valueOf(Globals.productList.size()));//Return Correct number.

   Alarm alarm=new Alarm();
    alarm.setAlarm(getApplicationContext());
}

【问题讨论】:

  • 在循环内声明pListtemp是没有意义的,只要在作用域外声明它们并在循环内将它们初始化为new

标签: android arraylist broadcastreceiver static-variables


【解决方案1】:

我相信您需要对此进行调试并观察您的 productList 的更改。

从没有其余代码的第一眼看,我相信在程序开始时,您会将列表初始化为一个新列表,该列表会创建一个大小为 0 的空列表 (将返回给您) 所以这对我来说看起来很正常。

BroadcastReceiver 应该如下调用,你不要创建它的对象:

Intent intent = new Intent(getApplicationContext(), Alarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(appContext, alarmRequestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendarTime, pendingIntent);

查看BroadcastReceiver 文档了解更多详情。

** 请注意,将数据保存到静态列表并不意味着它们会在程序关闭后保留在那里,BroadcastReceiver 可能会在应用程序未运行时运行,这就是您能够检索数据的原因在应用程序中,而不是在此类中。也许您需要将数据保存在数据库中或能够访问它的东西中,我相信您的问题不在于检索数据。 (再多一点代码会帮助我更好地帮助你)

【讨论】:

  • 我已经在 MainActivity ProductList 中初始化 productList temp=new ProductList(); temp.set("test1",123,"test","test",5); Globals.productList.add(temp);
  • 我可以从其他活动中获取该列表
  • 什么时候调用BroadcastReceiver
  • 此时调用BroadcastReceiver需要调试检查列表大小。但是另一种方法可能是将size 作为ExtraInt 传递给调用接收者的意图并从那里检索大小,但是如果您只需要列表的大小而不需要其他任何内容,这将有所帮助
  • 得到尺寸后我也会列出项目
猜你喜欢
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-05
  • 1970-01-01
  • 1970-01-01
  • 2016-08-27
相关资源
最近更新 更多