[英]NullPointerExceptoin when passing value from activity to broadcast receiver
[英]Passing value from MainActivity to Broadcast Receiver using SharedPreference
我是android编程的新手,我正在尝试构建一个应用程序,该应用程序会在接收到来自用户的消息时更改配置文件。 该应用程序基本上具有一个activity
和一个broadcast receiver
。 我提示用户设置用于更改配置文件的密码,并且该密码保存在MainActivity
内部使用的“ Shared Preference
中。 我无法在broadcast receiver
检索以shared preference
Shared Preference
存储的密码。此外,我只能使用Shared Preference
存储密码MainActivity
, Broadcast Receiver
,布局和Android manifest
文件的代码如下。 非常感谢你的帮助。
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();
}
}
}
这里的问题是从shared preference
检索值的pass3
不会返回任何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>
请帮我。 谢谢。
//Inside onReceive method
sharedPreferences = context.getSharedPreferences("Pritom", context.MODE_PRIVATE);
// Instead of this line, use
sharedPreferences= getApplicationContext().getSharedPreferences("Pritom", Context.MODE_PRIVATE);
设置密码之前,您可能会收到广播。 在接收到SMS上注册的广播接收器意味着接收到SMS时会进行呼叫。
当收到短信并且您仍然没有为共享首选项分配密码时该怎么办? 在这种情况下,您将无法从首选项中获取密码。
请检查该方案并对此进行一些更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.