こんにちは私は、 "表示プレビュー"という名前のチェックボックスがある設定アクティビティを持っています。私がしたいことは、このチェックボックスがチェックされていれば、ブロードキャスト受信機に何かを送ることです。ブロードキャスト受信機では、チェックボックスの値を取得したいと思います。チェックボックスがチェックされている場合は、新しいSmsが受信されたときに通知バーに通知を表示します。チェックボックスの値がチェックされていない場合、通知は表示されません。今私は共有のprefでチェックボックスの値を格納することができますが、私は自分のレシーバクラスでその値を取得していません。 uは私が今まで使用している場合、その活動を停止し、値がチェックされている場合、その値は、受信機のクラスに行くべきだとコードを書くご覧のよう `チェックボックスがオンの場合、ブロードキャスト受信者に値を送信します
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 "'は 'String'を比較する方法ではありません。 –