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
Aucun commentaire:
Enregistrer un commentaire