如果选中复选框,则将值发送到广播接收器
If Checkbox is Checked then send value to Broadcast receiver
您好,我有一个设置 activity,其中有一个名为 "Display Preview" 的复选框。我想做的是,如果选中此复选框,则它会向广播接收器发送一些内容。在广播接收器中,我想获取复选框值,如果它是真的,即复选框被选中,那么当收到新短信时,它会在通知栏中显示通知。如果未选中复选框的值,则不会显示通知。现在我可以通过共享首选项存储复选框的值,但我没有在我的接收器中获得该值 class。
这是我在设置中的代码 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();
}
如你所见,我编写了一段代码,每当使用停止 activity 时,如果检查了该值,则该值应发送给接收者 class。
现在在接收器中 class 这是我的代码:
String action = intent.getAction();
String state = "";
if(action.equals("my.action.string")){
state = intent.getExtras().getString("checked");
}
最后,我检查如果状态的值为 1,则应显示通知。
这是 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());
}
我也在接收器的清单文件中写了这个:
<action android:name="my.action.string"></action>
但我认为该值未传递给接收方 class。请帮忙
好吧,这是您可以做的事情
在设置activity中,在onStop方法中intent.putExtra("checked", "1");
改为intent.putExtra("checked", 1)
取出“1”的引号,因为您只是发送数字而不是字符串。
然后在接收器class中,确保你已经声明并初始化了意图。
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
}
就是这样,
希望有用
您好,我有一个设置 activity,其中有一个名为 "Display Preview" 的复选框。我想做的是,如果选中此复选框,则它会向广播接收器发送一些内容。在广播接收器中,我想获取复选框值,如果它是真的,即复选框被选中,那么当收到新短信时,它会在通知栏中显示通知。如果未选中复选框的值,则不会显示通知。现在我可以通过共享首选项存储复选框的值,但我没有在我的接收器中获得该值 class。
这是我在设置中的代码 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();
}
如你所见,我编写了一段代码,每当使用停止 activity 时,如果检查了该值,则该值应发送给接收者 class。 现在在接收器中 class 这是我的代码:
String action = intent.getAction();
String state = "";
if(action.equals("my.action.string")){
state = intent.getExtras().getString("checked");
}
最后,我检查如果状态的值为 1,则应显示通知。 这是 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());
}
我也在接收器的清单文件中写了这个:
<action android:name="my.action.string"></action>
但我认为该值未传递给接收方 class。请帮忙
好吧,这是您可以做的事情
在设置activity中,在onStop方法中intent.putExtra("checked", "1");
改为intent.putExtra("checked", 1)
取出“1”的引号,因为您只是发送数字而不是字符串。
然后在接收器class中,确保你已经声明并初始化了意图。
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
}
就是这样, 希望有用