在我的應用程序中,我有一個廣播接收器,它在接收到一組文本字符串時打開GPS。在onLocationChanged方法中,我想將GPS數據和我的共享首選項中的值傳遞給字符串中的線程。
我有線程寫入日誌,可以看到字符串中的所有GPS值,但我的共享首選項中的最後一個值只是顯示爲'prefPhoneNum',我將字符串初始化爲接收器類的開頭。我有相同的代碼從主類中的共享首選項中讀取prefPhoneNum,它在那裏工作,任何人都可以看到我可能做錯了什麼?BroadcastReceiver未從SharedPreferences讀取存儲的值
public class SmsReceiver extends BroadcastReceiver implements LocationListener
{
LocationManager lm;
LocationListener loc;
public SharedPreferences sharedpreferences;
public static final String US = "usersettings";
public String prefPhoneNum = "prefPhoneNum";
@Override
public void onReceive(Context context, Intent intent)
{
sharedpreferences = context.getSharedPreferences(US, Context.MODE_PRIVATE);
prefPhoneNum = sharedpreferences.getString("prefPhoneNum" , "");
lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
loc = new SmsReceiver();
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++)
{
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += msgs[i].getMessageBody().toString() + "\n";
}
Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); //Display SMS
if ((msgs[0].getMessageBody().toString().equals("Enable")) ||
(msgs[0].getMessageBody().toString().equals("enable")))
{
enableGPS();
}
else { /* Do Nothing*/ }
}
}
public void enableGPS() {
//new CountDownTimer(10000, 1000) { //10 seconds
new CountDownTimer(300000, 1000) { //300 secs = 5 mins
public void onTick(long millisUntilFinished)
{
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, loc);
}
public void onFinish()
{
lm.removeUpdates(loc);
}
}.start();
}
@Override
public void onLocationChanged(Location location) {
String s = "";
s += location.getLatitude() + "\n";
s += location.getLongitude() + "\n";
s += location.getAltitude() + "\n";
s += location.getAccuracy() + "\n" + prefPhoneNum;
Thread cThread = new Thread(new SocketsClient(s));
cThread.start();
}
@Override
public void onProviderDisabled(String provider) { }
@Override
public void onProviderEnabled(String provider) { }
@Override
public void onStatusChanged(String provider, int status, Bundle extras) { }
}
這裏是當應用程序關閉的logcat的 -
D/LocationManager(3912): requestLocationUpdates: provider = gps, listener = [email protected]
D/GpsLocationProvider( 96): setMinTime 0
D/GpsLocationProvider( 96): startNavigating
D/GpsLocationProvider( 96): TTFF: 3227
D/AndroidRuntime(3912): Shutting down VM
W/dalvikvm(3912): threadid=1: thread exiting with uncaught exception (group=0x400259f8)
E/AndroidRuntime(3912): FATAL EXCEPTION: main
E/AndroidRuntime(3912): java.lang.NullPointerException
E/AndroidRuntime(3912): at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:146)
E/AndroidRuntime(3912): at accel.working.TrackGPS.onLocationChanged(TrackGPS.java:63)
E/AndroidRuntime(3912): at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:191)
E/AndroidRuntime(3912): at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:124)
E/AndroidRuntime(3912): at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:140)
E/AndroidRuntime(3912): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(3912): at android.os.Looper.loop(Looper.java:144)
E/AndroidRuntime(3912): at android.app.ActivityThread.main(ActivityThread.java:4937)
E/AndroidRuntime(3912): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(3912): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(3912): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(3912): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(3912): at dalvik.system.NativeStart.main(Native Method)
好的我已經改變了它,以便所有的BroadcastReceiver所做的就是創建一個服務來完成所有的GPS工作,謝謝Falmarri。但我的問題仍然是,在'onLocationChanged'方法中,prefPhoneNum的值是'prefPhoneNum'的初始化值。你知道如何從該方法的sharedpreferences中讀取數據,或者訪問enableGPS()中的GPS數據以傳遞給我創建的線程? – bobby123 2010-12-13 05:44:01
代碼中沒有錯誤,但是如果我調用sharedpreferences = getSharedPreferences(US,MODE_WORLD_READABLE);在'onLocationChanged()',那麼應用程序的力量關閉 – bobby123 2010-12-13 06:26:01
什麼是堆棧跟蹤?發佈你的logcat – Falmarri 2010-12-13 16:00:55