复选框可打开/关闭GCM通知

时间:2013-12-30 16:22:52

标签: android checkbox google-cloud-messaging

我做了很多研究,但仍然无法找到任何解决方案。我需要创建一个复选框来选择是否接收GCM通知。我尝试使用全局复选框变量,而checkbox.isChecked()等于true,然后将接收通知,反之亦然。但是GCMIntentService类不允许我修改方法。感谢帮助,抱歉我的英语不好。

公共类GCMIntentService扩展GCMBaseIntentService {

public GCMIntentService() {
    super(SENDER_ID);
}

private static final String TAG = "===GCMIntentService===";

@Override
protected void onRegistered(Context arg0, String registrationId) {
    Log.i(TAG, "Device registered: regId = " + registrationId);
}

@Override
protected void onUnregistered(Context arg0, String arg1) {
    Log.i(TAG, "unregistered = " + arg1);
}

@Override
protected void onMessage(Context context, Intent intent) {
    Log.i(TAG, "new message= ");
    String message = intent.getExtras().getString("message");
        generateNotification(context, message);     
}

@Override
protected void onError(Context arg0, String errorId) {
    Log.i(TAG, "Received error: " + errorId);
}

@Override
protected boolean onRecoverableError(Context context, String errorId) {
    return super.onRecoverableError(context, errorId);
}
@SuppressWarnings("deprecation")
private static void generateNotification(Context context, String message) {
    int icon = R.drawable.icon;
    long when = System.currentTimeMillis();

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification = new Notification(icon, message, when);
    String title = context.getString(R.string.app_name);
    Intent notificationIntent = new Intent(context, SplashScreenActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0,
            notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);
}

}

公共类SettingsActivity扩展了Activity {

public static CheckBox chkBox;
ImageView img;
TextView t1,t2,t3,t4;

View v;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);

    this.getActionBar().setCustomView(R.layout.settings_actionbar_layout);
    this.getActionBar().setDisplayShowCustomEnabled(true);
    this.getActionBar().setDisplayUseLogoEnabled(false);
    this.getActionBar().setDisplayShowHomeEnabled(false);

    chkBox = (CheckBox)findViewById(R.id.checkBox1);

    img = (ImageView)findViewById(R.id.img1);
    t1 = (TextView)findViewById(R.id.txt1);
    t2 = (TextView)findViewById(R.id.txt2);
    t3 = (TextView)findViewById(R.id.txt3);
    t4 = (TextView)findViewById(R.id.txt4);
    v = (View)findViewById(R.id.line);

    t4.setText(Html.fromHtml("<a href=\"mailto:abc@abc.com\">Send Feedback</a>"));
    t4.setMovementMethod(LinkMovementMethod.getInstance());
}

1 个答案:

答案 0 :(得分:0)

这个怎么样?

CheckBoxPreference checkboxPref = (CheckBoxPreference)getPreferenceManager().findPreference("my_checkbox");

   checkboxPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {            

   public boolean onPreferenceChange(Preference preference, Object newValue) {

          boolean myValue = (Boolean) newValue;

          if(myValue)
                  startService(new Intent(Settings.this, MyService.class));
          else
                  stopService(new Intent(Settings.this, MyService.class));

         return true;
    }
});