-1

Hi I have a settings activity in which there is a checkbox named "Display Preview". The thing i want to do that is if this checkbox is checked then it send something to Broadcast receiver. In Broadcast receiver i want to get the checkbox value and if it is true i.e. checkbox was checked then it display the notification in notiification bar when new Sms received. If the value of checkbox is not checked then no Notification show. Now i am able to store the value of checkbox by shared pref but I am not getting that value in my receiver class.

Here is my code in settings activity:`

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();

     }

As u see i write a code that when ever uses stop that activity and if the value is checked then the value should go to receiver class. Now in the receiver class here is my code:

String action = intent.getAction();
  String state = "";
  if(action.equals("my.action.string")){
         state = intent.getExtras().getString("checked");

      }

And finally I am checking that if the value of state is 1 then notification should show. Here is the code for Notification

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());

      }

I have written this in my manifest file in the receiver too:

<action android:name="my.action.string"></action>

But i think the value is not passing tor receiver class. please Help

Arslan Ali
  • 371
  • 5
  • 20
  • Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Mike M. May 20 '16 at 08:10
  • Compare??? I am not comparing I am just not getting the value from activity to receiver. Should i need to get the preference value in receiver ? – Arslan Ali May 20 '16 at 08:12
  • `state == "1"` is not how you compare `String`s. – Mike M. May 20 '16 at 08:12
  • Comparison is the next step sir. I will handle that on my own but the first issue is I am not receiving the value in my receiver – Arslan Ali May 20 '16 at 08:13
  • You said "i think the value is not passing tor receiver class". Are you certain? Have you debugged, and checked the extra's value in the Receiver? – Mike M. May 20 '16 at 08:15

1 Answers1

1

Well Here is a what you could do

In setting activity, in onStop Method intent.putExtra("checked", "1");

Change it to intent.putExtra("checked", 1) Take out the quotes of "1" as you are just sending a number not a string.

Then in receiver class, Make sure that you have declared and initialized the intent.

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

What i have done is that since you only need to check the number not string, i have changed the state to integer. the value 1 which when received will be stored in state.

You would probably know what this means intent.getIntExtra("checked", 0); Here 0 is the default value i have set.

Next in notification code

if (state == 1){
  //all the notification code
}

That's all , Hope it works