gpt4 book ai didi

android - BroadcastReceiver 中的共享首选项未被正确读取

转载 作者:行者123 更新时间:2023-11-29 14:17:28 25 4
gpt4 key购买 nike

我是安卓开发的新手。我有一个问题,SharedPreferences 没有被 BroadcastReceiver

读取

背景:
我正在尝试编写一个没有 GUI 的 Android 应用程序。每次应用程序都会做一些工作单击应用程序图标,将设置写入共享首选项,设置 3 秒后的警报,然后退出(finish())。应用程序图标将被快速连续单击 3 或 4 次。 BroadcastReceiver 和 Shared Preferences 将允许我跟踪它被点击的次数、最后一次点击的时间以及一些其他设置。

我在 list 中用来抑制 GUI 的设置是

android:theme="@android:style/Theme.NoDisplay"

有3个文件

  1. list 文件
  2. 我的主要 Activity 文件 MyFirstTestActivity.java
  3. 我的广播接收器类 MyReceiver.java

当我快速连续多次单击“应用程序”图标时。 MyFirstTestActivity 类正确读取共享首选项“计数”设置并正确递增它。它设置警报并传递一个名为“CurrentCount”的额外数据在我的 Broadcast Receiver 中,当我读取 Shared Preferences 和传递的额外数据时,它停留在 1 的 Intent 不会正确递增。

我想要实现的是在 MyReceiver.onReceive 中通过比较
的值“Count” Shared Preference 和传入的“CurrentCount” Intent extra setting 是同一个值我会知道用户连续快速停止点击然后我可以给用户显示一个消息,而不是每次都显示消息它被称为。

下面是Eclipse LogCat日志的一部分

11-288 09:43:10.652: I/UpdateSettings(782): Read Value of Count from Preferences -7
11-28 09:43:10.712: I/UpdateSettings(782): Read Back Count from Preferences - 8

11-28 09:43:10.892: W/InputManagerService(112): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@415ef6a8
11-28 09:43:11.012: W/NetworkManagementSocketTagger(112): setKernelCountSet(10040, 0) failed with errno -2
11-28 09:43:13.772: I/MyReceiver(797): Test
11-28 09:43:13.782: I/MyReceiver(797): Preferences Contains the field Count
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: I/ActivityManager(112): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.mywebsite.myfirsttest/.MyFirstTestActivity} from pid 212
11-28 09:43:36.162: W/WindowManager(112): Failure taking screenshot for (180x300) to layer 21005 11-28 09:43:36.212: W/NetworkManagementSocketTagger(112): setKernelCountSet(10040, 1) failed with errno -2
11-28 09:43:36.272: I/UpdateSettings(782): Read Value of Count from Preferences -8
11-28 09:43:36.362: I/UpdateSettings(782): Read Back Count from Preferences - 9

11-28 09:43:36.583: W/InputManagerService(112): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@41640fa8
11-28 09:43:36.653: W/NetworkManagementSocketTagger(112): setKernelCountSet(10040, 0) failed with errno -2
11-28 09:43:39.402: I/MyReceiver(797): Test
11-28 09:43:39.412: I/MyReceiver(797): Preferences Contains the field Count
11-28 09:43:39.412: I/MyReceiver(797): count=1
11-28 09:43:39.412: I/MyReceiver(797): currentCount=1

11-28 09:44:27.072: D/dalvikvm(196): GC_CONCURRENT freed 398K, 6% free 10399K/10951K, paused 5ms+4ms
11-28 09:47:46.312: W/ThrottleService(112): unable to find stats for iface rmnet0

带有标签 UpdateSettings 的日志显示为正确更新带有 MyReceiver 标签的日志没有正确读取最新的 Shared Preference 值(或 intent extra Data)

这是我的 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");
}

}

我的 list 文件

<?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 的 Context 是否合适?

最佳答案

我对您的代码有几点意见......

  1. 我在 list 或 Activity 中没有看到广播接收器的任何 Intent 过滤器。

  2. 你可能应该写

    SharedPreferences.Editor editor = prefs.edit();

updateSharedPreferenceSettings()方法中将其保存在SharedPreferences中。

关于android - BroadcastReceiver 中的共享首选项未被正确读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8298026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com