首页 > 解决方案 > 扩展 BroadcastReceiver onReceive 函数无法从其他类获取静态 ArrayList

问题描述

这个接收器

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

应用程序没有崩溃,但 onReceive 函数 Globals.productList.size 返回 0,我可以从 testArray 获取数据。productList arraylist 也是静态的,我可以在除 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());
}

标签: androidarraylistbroadcastreceiverstatic-variables

解决方案


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


推荐阅读