您好我有一個設置活動,其中有一個名爲「顯示預覽」的複選框。我想做的事情是,如果這個複選框被選中,那麼它會發送一些東西給廣播接收器。在廣播接收機中,我想獲得複選框的值,如果它是真的,即複選框被選中,那麼它會在收到新短信時在通知欄中顯示通知。如果複選框的值未選中,則不顯示通知。現在我可以通過共享pref存儲複選框的值,但我沒有在接收器類中獲得該值。如果複選框被選中,然後發送值給廣播接收器
這是我在設置活動代碼:`
CheckBox ch1;
private SharedPreferences loginPreferences;
private SharedPreferences.Editor loginPrefsEditor;
private Boolean saveLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
ch1 = (CheckBox) findViewById(R.id.checkBox1);
loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);
loginPrefsEditor = loginPreferences.edit();
saveLogin = loginPreferences.getBoolean("saveLogin", false);
if (saveLogin == true) {
ch1.setChecked(true);
}
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
if (ch1.isChecked())
{
loginPrefsEditor.putBoolean("saveLogin", true);
loginPrefsEditor.commit();
Intent intent = new Intent("my.action.string");
intent.putExtra("checked", "1");
sendBroadcast(intent);
}
else {
loginPrefsEditor.clear();
loginPrefsEditor.commit();
}
正如你看到我寫的代碼,當過使用停止活動,如果這個值被選中則該值應該去接收機類。 現在接收機類這裏是我的代碼:
String action = intent.getAction();
String state = "";
if(action.equals("my.action.string")){
state = intent.getExtras().getString("checked");
}
最後我檢查,如果狀態的值是1,則通知應該顯示。 這是我在清單文件中的接收器也寫了這對通知
if (state == "1")
{
NotificationCompat.Builder notify = new NotificationCompat.Builder(context);
notify.setSmallIcon(R.drawable.newnotifysmall);
notify.setContentTitle(title);
notify.setContentText(msgBody);
notify.setAutoCancel(true);
Notification notification = new Notification();
notification.defaults |= Notification.DEFAULT_VIBRATE;
//notify.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
notify.setLights(Color.GREEN, 2000, 2000);
notify.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
Intent notificationIntent = new Intent(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_DEFAULT);
notificationIntent.setType("vnd.android-dir/mms-sms");
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intentt = PendingIntent.getActivity(context, 0,notificationIntent, 0);
notify.setContentIntent(intentt);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notify.build());
}
代碼:
<action android:name="my.action.string"></action>
但我覺得值不流通TOR接收器類。請幫助
的可能的複製[我如何在Java中比較字符串?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) –
比較???我不是在比較,我只是沒有從活動中獲得價值。我是否需要在接收器中獲得偏好值? –
'state ==「1」'不是你比較'字符串'的方式。 –