扩展 BroadcastReceiver onReceive 函数无法从其他 class 获取静态 ArrayList

extends BroadcastReceiver onReceive function Can't get static ArrayList from other class

这是接收器

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

这里是全局 Class

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

应用程序没有崩溃,但 onReceive 函数 Globals.productList.size return 0,我可以从 testArray 获取数据。 productList arraylist 也是静态的,我可以从 Globals.ProductList 中的所有 activity 中获取数据,除了 onReceuve 函数。

那么,如何访问全局静态 ArrayList 并从中获取数据?

主要活动class

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

我相信您需要对此进行调试并观察 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 可能是 运行 而应用程序不是 运行 这就是为什么您将能够在应用程序中而不是在此 class 中检索数据的原因。也许您需要将数据保存在数据库或其他东西中才能访问它,我相信您的问题不在于检索数据的方式。 (再多一点代码会帮助我更好地帮助你)