2016-05-20 13 views
-1

こんにちは私は、 "表示プレビュー"という名前のチェックボックスがある設定アクティビティを持っています。私がしたいことは、このチェックボックスがチェックされていれば、ブロードキャスト受信機に何かを送ることです。ブロードキャスト受信機では、チェックボックスの値を取得したいと思います。チェックボックスがチェックされている場合は、新しい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レシーバクラスを渡していないと思います。さてここで

+0

可能性のある重複した作品ホープ[私はJavaで文字列を比較するにはどうすればよいですか?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) –

+0

比較する???私は活動から受信機への価値を得ていないだけではない。私は受信機の設定値を取得する必要がありますか? –

+0

'state ==" 1 "'は 'String'を比較する方法ではありません。 –

答えて

1

は、あなただけの番号を送信しているようintent.putExtra("checked", 1) にあなたはonStop方法で、アクティビティを設定するには

を何ができるかintent.putExtra("checked", "1");

変更、それが「1」の引用符を取り出しです助けてください文字列ではありません。

受信者クラスでは、宣言してインテントを初期化していることを確認してください。私が行っている何

String action = intent.getAction(); 
    int state; 
    if(action.equals("my.action.string")){ 
     state = intent.getIntExtra("checked", 1); 
    } 

あなただけの数字ではない文字列をチェックする必要があるため、私は整数に状態を変更したということです。受け取ったときの状態1の値が格納されます。

あなたはおそらくこれが意味することを知っているでしょうintent.getIntExtra("checked", 0); ここで0が私が設定したデフォルト値です。

すべてです
if (state == 1){ 
    //all the notification code 
} 

通知コードで

次に、 は、それがの

+0

これで最後に –

+0

作品を確認させていただきます。 –

+0

うれしいです。ハッピーコーディング:) –

関連する問題