在BroadcastReceiver中,静态数组总是用零填充

时间:2015-01-17 21:12:57

标签: java android arrays broadcastreceiver static-array

我想从static内的某些Array BroadcastReceiver获取值。问题是它总是在BroadcastReceiver内填充零,而它不是应用程序中的任何其他位置。在MainActivity内部,我创建了一个testArray方法,该方法也在logcat中显示相同的数组,并且值不同。

AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context k1, Intent k2) {
        // TODO Auto-generated method stub
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);

        for (int i=0; i<24; i++) {
            Log.d("Receiver " + Integer.toString(i), Double.toString(Data.mean_temp_day[i]));
        }
    }

}

调用onReceive后我在logcat中得到的内容

01-17 22:03:02.007  10911-10911/com.example.nimboo:remote D/Receiver 0﹕ 0.0
01-17 22:03:02.007  10911-10911/com.example.nimboo:remote D/Receiver 1﹕ 0.0
01-17 22:03:02.007  10911-10911/com.example.nimboo:remote D/Receiver 2﹕ 0.0
01-17 22:03:02.007  10911-10911/com.example.nimboo:remote D/Receiver 3﹕ 0.0
01-17 22:03:02.007  10911-10911/com.example.nimboo:remote D/Receiver 4﹕ 0.0
01-17 22:03:02.007  10911-10911/com.example.nimboo:remote D/Receiver 5﹕ 0.0
01-17 22:03:02.007  10911-10911/com.example.nimboo:remote D/Receiver 6﹕ 0.0
01-17 22:03:02.007  10911-10911/com.example.nimboo:remote D/Receiver 7﹕ 0.0
01-17 22:03:02.007  10911-10911/com.example.nimboo:remote D/Receiver 8﹕ 0.0
01-17 22:03:02.007  10911-10911/com.example.nimboo:remote D/Receiver 9﹕ 0.0
01-17 22:03:02.017  10911-10911/com.example.nimboo:remote D/Receiver 10﹕ 0.0
01-17 22:03:02.017  10911-10911/com.example.nimboo:remote D/Receiver 11﹕ 0.0
01-17 22:03:02.027  10911-10911/com.example.nimboo:remote D/Receiver 12﹕ 0.0
01-17 22:03:02.027  10911-10911/com.example.nimboo:remote D/Receiver 13﹕ 0.0
01-17 22:03:02.027  10911-10911/com.example.nimboo:remote D/Receiver 14﹕ 0.0
01-17 22:03:02.027  10911-10911/com.example.nimboo:remote D/Receiver 15﹕ 0.0
01-17 22:03:02.027  10911-10911/com.example.nimboo:remote D/Receiver 16﹕ 0.0
01-17 22:03:02.027  10911-10911/com.example.nimboo:remote D/Receiver 17﹕ 0.0
01-17 22:03:02.027  10911-10911/com.example.nimboo:remote D/Receiver 18﹕ 0.0
01-17 22:03:02.027  10911-10911/com.example.nimboo:remote D/Receiver 19﹕ 0.0
01-17 22:03:02.037  10911-10911/com.example.nimboo:remote D/Receiver 20﹕ 0.0
01-17 22:03:02.037  10911-10911/com.example.nimboo:remote D/Receiver 21﹕ 0.0
01-17 22:03:02.037  10911-10911/com.example.nimboo:remote D/Receiver 22﹕ 0.0
01-17 22:03:02.037  10911-10911/com.example.nimboo:remote D/Receiver 23﹕ 0.0

testArray方法 - MainActivity.java代码的一部分

public void testArray(View v) {
        for(int i=0; i<24; i++) {
            Log.d("Test " + Integer.toString(i), Double.toString(Data.mean_temp_day[i]));
        }
    }

调用testArray时我在logcat中得到的内容

01-17 22:02:23.387    9929-9929/com.example.nimboo D/Test 0﹕ 0.0
01-17 22:02:23.387    9929-9929/com.example.nimboo D/Test 1﹕ 0.0
01-17 22:02:23.387    9929-9929/com.example.nimboo D/Test 2﹕ 0.0
01-17 22:02:23.387    9929-9929/com.example.nimboo D/Test 3﹕ 0.0
01-17 22:02:23.387    9929-9929/com.example.nimboo D/Test 4﹕ 0.0
01-17 22:02:23.387    9929-9929/com.example.nimboo D/Test 5﹕ 0.0
01-17 22:02:23.387    9929-9929/com.example.nimboo D/Test 6﹕ 0.0
01-17 22:02:23.387    9929-9929/com.example.nimboo D/Test 7﹕ 0.0
01-17 22:02:23.387    9929-9929/com.example.nimboo D/Test 8﹕ 0.0
01-17 22:02:23.387    9929-9929/com.example.nimboo D/Test 9﹕ 0.0
01-17 22:02:23.387    9929-9929/com.example.nimboo D/Test 10﹕ 0.0
01-17 22:02:23.387    9929-9929/com.example.nimboo D/Test 11﹕ 0.0
01-17 22:02:23.387    9929-9929/com.example.nimboo D/Test 12﹕ 0.0
01-17 22:02:23.387    9929-9929/com.example.nimboo D/Test 13﹕ 24.721666666666664
01-17 22:02:23.387    9929-9929/com.example.nimboo D/Test 14﹕ 12.02
01-17 22:02:23.387    9929-9929/com.example.nimboo D/Test 15﹕ 0.0
01-17 22:02:23.387    9929-9929/com.example.nimboo D/Test 16﹕ 0.0
01-17 22:02:23.387    9929-9929/com.example.nimboo D/Test 17﹕ 21.323333333333334
01-17 22:02:23.387    9929-9929/com.example.nimboo D/Test 18﹕ 22.481666666666666
01-17 22:02:23.387    9929-9929/com.example.nimboo D/Test 19﹕ 22.06833333333333
01-17 22:02:23.387    9929-9929/com.example.nimboo D/Test 20﹕ 22.098
01-17 22:02:23.387    9929-9929/com.example.nimboo D/Test 21﹕ 21.98166666666667
01-17 22:02:23.387    9929-9929/com.example.nimboo D/Test 22﹕ 21.67
01-17 22:02:23.387    9929-9929/com.example.nimboo D/Test 23﹕ 0.0

Data.java

public final class Data {
    public static double[] mean_temp_day = new double[24];
    private Data() {
    }
}

这种结果的原因是什么?我不明白为什么static Array包含不同的值?如何在static内找到Array BroadcastReceiver的正确值?

0 个答案:

没有答案