我是android編程的新手,我試圖構建一個應用程序,它可以在接收用戶的消息時更改配置文件。該應用程序基本上有一個activity
和broadcast receiver
。我正在提示用戶設置密碼以更改配置文件,密碼保存在Shared Preference
中,我在MainActivity
中使用了該密碼。我無法檢索shared preference
中存儲的broadcast receiver
類中的密碼。另外,我只能使用Shared Preference
來存儲密碼。下面給出MainActivity
,Broadcast Receiver
,佈局和Android manifest
文件的代碼。非常感謝你的幫助。使用SharedPreference將MainActivity中的值傳遞給Broadcast Receiver
MainActivity.java
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
類可變b
返回true,這意味着shared preference
存在。這裏
MyReceiver.java
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
任何東西的價值爲其if
語句檢查消息不起作用。
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MyReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
請幫助我。謝謝。
***我正在檢索共享首選項中的廣播接收器內的密碼,但我無法檢索它*** ....好,有點混亂......你能清除嗎? – Opiatefuchs
我認爲問題在於您的活動和廣播接收器正在訪問兩個不同的共享首選項文件。嘗試在廣播接收器中使用'context.getApplicationContext()。getSharedPreferences(....)'。 – Titus
@Opiatefuchs,我已經更新了我的問題。 –