如果Checkbox已选中,则将值发送到广播接收器

时间:2016-05-20 08:01:10

标签: android checkbox broadcastreceiver sharedpreferences

您好我有一个设置活动,其中有一个名为"显示预览"的复选框。我想要做的是,如果选中此复选框,则它会向广播接收器发送内容。在广播接收器中,我想获取复选框值,如果是真,即选中复选框,则在收到新的Sms时,它会在通知栏中显示通知。如果未选中复选框的值,则不显示通知。现在我能够通过共享pref存储checkbox的值,但是我没有在我的接收器类中获得该值。

以下是设置活动中的代码:`

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

      }

最后我检查如果state的值为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>

但我认为价值不是通过接收器类。请帮忙

1 个答案:

答案 0 :(得分:1)

这是你能做的事情

在设置活动时,在onStop方法select id from ( select ID, ROW_NUMBER() over (partition by LATEST_RECEIPT order by ID) rownumber from Table where LATEST_RECEIPT in ( select LATEST_RECEIPT from Table group by LATEST_RECEIPT having COUNT(1) > 1 ) ) t where rownumber <> 1;

将其更改为intent.putExtra("checked", "1"); 取出&#34; 1&#34;的引用因为你只是发送一个不是字符串的数字。

然后在接收器类中,确保已声明并初始化了意图。

intent.putExtra("checked", 1)

我所做的是,因为你只需要检查数字而不是字符串,我已经将状态更改为整数。接收时的值1将存储在状态中。

您可能知道这意味着什么String action = intent.getAction(); int state; if(action.equals("my.action.string")){ state = intent.getIntExtra("checked", 1); } 这里0是我设置的默认值。

接下来的通知代码

intent.getIntExtra("checked", 0);

这就是全部, 希望它有效