使用SharedPreference将值从MainActivity传递到Broadcast Receiver

时间:2017-05-30 11:28:03

标签: java android broadcastreceiver android-sharedpreferences

我是Android编程的新手,我正在尝试构建一个应用程序,在接收来自用户的消息时更改配置文件。该应用基本上有一个activity和一个broadcast receiver。我提示用户设置密码以更改配置文件,密码保存在我Shared Preference内使用的MainActivity中。我无法检索shared preference类中broadcast receiver中存储的密码。此外,我只能使用Shared Preference来存储密码。MainActivity的代码,{ {1}},布局和Broadcast Receiver文件如下。非常感谢你的帮助。

MainActivity.java

Android manifest

}

此处,在public class MainActivity extends AppCompatActivity { public EditText setPass; public Button submit; public static String password; SharedPreferences sharedPreferences; boolean b=false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setPass=(EditText)findViewById(R.id.setPassEditId); submit=(Button)findViewById(R.id.submitButtonId); sharedPreferences= getApplicationContext().getSharedPreferences("Pritom",Context.MODE_PRIVATE); submit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { password=setPass.getText().toString(); SharedPreferences.Editor editor=sharedPreferences.edit(); editor.putString("password",password); editor.commit(); Toast.makeText(getApplicationContext(),"Password saved successfully"+password,Toast.LENGTH_LONG).show(); //b=sharedPreferences.contains("password")?true:false; //Toast.makeText(getApplicationContext(),"b:"+b,Toast.LENGTH_LONG).show(); } }); } 类中,变量MainActivity返回true,表示b存在。

MyReceiver.java

shared preference

这里的问题是从public class MyReceiver extends BroadcastReceiver { public AudioManager audioManager; public String me = ""; public String last = ""; public static final String SMS_BUNDLE = "pdus"; public SharedPreferences sharedPreferences; public static final String MyPREFERENCES = "MyPrefs"; boolean b = false; @Override public void onReceive(Context context, Intent intent) { sharedPreferences = context.getSharedPreferences("Pritom", context.MODE_PRIVATE); String pass3 = sharedPreferences.getString("password", null); audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); Bundle bundle = intent.getExtras(); SmsMessage[] msgs = null; String str = ""; if (bundle != null) { 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 += "SMS from " + msgs[i].getOriginatingAddress(); str += " :"; str += msgs[i].getMessageBody().toString(); str += "n"; String smsBody = msgs[i].getMessageBody().toString(); if (smsBody.equals("@general" + pass3)) { AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); audioManager.setRingerMode(audioManager.RINGER_MODE_NORMAL); } } Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); } } } 检索值的pass3不会返回用于检查消息的shared preference语句不起作用的任何内容。

的AndroidManifest.xml

if

<?xml version="1.0" encoding="utf-8"?>

请帮帮我。谢谢。

2 个答案:

答案 0 :(得分:0)

//Inside onReceive method
sharedPreferences = context.getSharedPreferences("Pritom", context.MODE_PRIVATE);

// Instead of this line, use
sharedPreferences= getApplicationContext().getSharedPreferences("Pritom", Context.MODE_PRIVATE);

答案 1 :(得分:0)

在设置密码之前,您可能会收到广播。 收到SMS时注册的广播接收器意味着它在收到短信时呼叫。

什么时候收到短信,你仍然没有为共享偏好分配密码?在这种情况下,您无法从首选项获取密码。

请检查该方案并对其进行一些更改。