我是android開發新手。我有一個問題,SharedPreferences
沒有在BroadcastReceiver
在BroadcastReceiver中未正確讀取共享首選項
背景閱讀:
我試圖寫一個Android應用程序沒有GUI。每次單擊應用程序圖標時,應用程序都會執行一些操作,將設置寫入共享首選項,將來設置鬧鐘3秒鐘,然後退出(完成())。應用程序圖標將快速連續點擊3或4次。 BroadcastReceiver和Shared Preferences可以讓我跟蹤它被點擊的次數,上次點擊的時間和其他一些設置。
,我使用的清單以抑制GUI設置是
android:theme="@android:style/Theme.NoDisplay"
有3個文件
- 清單文件
- 我的主要活動文件MyFirstTestActivity.java
- 我的廣播接收器類MyReceiver.java
當我快速連續多次單擊應用程序圖標時。 MyFirstTestActivity類正確讀取共享首選項「計數」設置並正確地增加它。 它設置了警報並傳遞了一個名爲「CurrentCount」的額外數據。 在我的廣播接收器中,當我讀取共享首選項和通過意圖傳遞的額外數據時,它停留在1,它不會正確增加。
我所試圖實現的是在MyReceiver.onReceive通過比較
值「計數」共享偏好和「CURRENTCOUNT」傳入意圖額外的設置是一樣的價值,我必須知道用戶已經停止然後我可以向用戶顯示一條消息,而不是在每次調用消息時顯示消息。
下面是Eclipse的logcat的區段登錄
11-288 09:43:10.652:I/UpdateSettings(782):從偏好-7
9月11日至28日閱讀count的值:43:10.712:I/UpdateSettings(782):從首選項中讀回計數 - 8
11-28 09:43:10.892:W/InputManagerService(112):窗口已經聚焦,忽略焦點增益:com.android .internal.view.IInputMethodClient $ Stub $ Proxy @ 415ef6a8
11-28 09:43:11.012:W/NetworkManagementSocketTagger(112):setKernelCountSet(10040,0)失敗,錯誤號-2
9月11日至28日:43:13.772:I/MyReceiver(797):測試
9月11日至28日:43:13.782:I/MyReceiver(797):設定包含該字段計數
11-28 09:43:13.792:I/MyReceiver(797):count = 1
11-28 09:43:13.792:I/MyReceiver(797):currentCount = 1
11-28 09:43: 36.162:來自pid 212的I/ActivityManager(112):START {act = android.intent.action.MAIN cat = [android.intent.category.LAUNCHER] flg = 0x10200000 cmp = com.mywebsite.myfirsttest/.MyFirstTestActivity}
11-28 09:43:36。162:W/WindowManager(112):無法截圖(180x300)至圖層21005 11-28 09:43:36.212:W/NetworkManagementSocketTagger(112):setKernelCountSet(10040,1)失敗,出錯-2
9月11日至28日:43:36.272:I/UpdateSettings(782):43:36.362:從設定-8
9月11日至28日閱讀count的值I/UpdateSettings(782):從設定回讀計數 - 9
11-28 09:43:36.583:W/InputManagerService(112):窗口已經聚焦,忽略焦點增益:[email protected]
11-28 09:43 :36.653:W/NetworkManagementSocketTagger(112):setKernelCountSet(10040,0)失敗,errno -2
11-28 09:43:39.402:I/MyReceiver(797):測試
11-28 09:43:39.412:I/MyReceiver(797):首選項包含字段計數
11-28 09:43 :39.412:I/MyReceiver(797):計數= 1
9月11日至28日:43:39.412:I/MyReceiver(797):CURRENTCOUNT = 1
9月11日至28日:44:27.072:d/dalvikvm( 196):GC_CONCURRENT釋放398K,6%遊離10399K/10951K,暫停5ms的+ 4ms的
9月11日至28日:47:46.312:W/ThrottleService(112):無法找到IFACE rmnet0統計
的使用標記UpdateSett記錄日誌英格斯顯示爲更新正確 與MyReceiver的標籤的日誌不會閱讀最新的共享偏好值(或意圖額外的數據)正確
這裏是我的2個源文件 MyFirstTestActivity.java
public class MyFirstTestActivity extends Activity {
public static final String MY_PREFS = "TestPrefs";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Note: This application has no display
// See the manifest @android:style/Theme.NoDisplay"
//Toast.makeText(this,"in onCreate", Toast.LENGTH_SHORT).show();
// Increment the count shared setting
int count = updateSharedPreferenceSettings();
// Send the alarm
setMessageAlarm(count);
// Exit the application
finish();
}
private int updateSharedPreferenceSettings() {
SharedPreferences prefs = this.getSharedPreferences(MyFirstTestActivity.MY_PREFS, Context.MODE_WORLD_READABLE);
int count = 1;
if(prefs.contains("Count")) {
count = prefs.getInt("Count", 1);
Log.i("UpdateSettings", "Read Value of Count from Preferences -" + String.valueOf(count));
count = count+1;
}
Editor editor = prefs.edit();
editor.putInt("Count", count);
editor.commit();
Log.i("UpdateSettings", "Read Back Count from Preferences - " + prefs.getInt("Count", 0));
return count;
}
private void setMessageAlarm(int currentCount) {
Intent intent = new Intent(MyFirstTestActivity.this, MyReceiver.class);
intent.putExtra("CurrentCount", currentCount);
PendingIntent appIntent =
PendingIntent.getBroadcast(MyFirstTestActivity.this, 0, intent, 0);
Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
c.add(Calendar.SECOND, 3);
// Set the alarm for 3 seconds in the future
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), appIntent);
}
}
我接收器源文件MyReceiver.java
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("MyReceiver", "Test");
SharedPreferences prefs = context.getSharedPreferences(MyFirstTestActivity.MY_PREFS, Context.MODE_WORLD_READABLE);
if(prefs.contains("Count")) {
Log.i("MyReceiver", "Preferences Contains the field Count");
int count = prefs.getInt("Count", -2);
Log.i("MyReceiver", "count=" + String.valueOf(count));
int currentCount = intent.getIntExtra("CurrentCount", -3);
Log.i("MyReceiver", "currentCount=" + String.valueOf(currentCount));
}
else {
Log.i("MyReceiver", "Preferences Does NOT Contains the field Count");
}
}
}
我的清單文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mywebsite.myfirsttest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name="com.mywebsite.myfirsttest.MyFirstTestActivity"
android:theme="@android:style/Theme.NoDisplay"
>
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.mywebsite.myfirsttest.MyReceiver" android:process=":remote"></receiver>
</application>
</manifest>
爲什麼MyReceiver.java不接最後SharedPreferences,而是卡住與值在第一時間報警設置? 使用傳遞給MyReceiver的上下文是否合適?
我認爲你從來沒有達到這個底部? :) –
你有沒有解決這個問題? –