1

我在哪里可以“呼叫”我的通知?我希望在单击 checkboxpreferenced 时出现。 编辑 MainActivity.java

import...//here all imports i need

public class MainActivity extends Activity {
CheckBoxPreference firtsDependent;
...
public void onCreate(Bundle savedInstanceState){
  super onCreate(savedInstanceState);
  setContentView(R.layout.main);
  //and your code
}
}
    private void sendSimpleNotification(){ 

        boolean pref_opt1= PreferenceManager.getDefaultSharedPreferences(MainActivity.this).getBoolean("firstDependent", false);

        if(pref_opt1) {
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(service.this);
            notificationBuilder.setContentTitle("Title");
            notificationBuilder.setContentText("Context");
            notificationBuilder.setTicker("TickerText");
            notificationBuilder.setWhen(System.currentTimeMillis());
            notificationBuilder.setSmallIcon(R.drawable.ic_stat_icon);

            Intent notificationIntent = new Intent(this, service.class);
            PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

            notificationBuilder.setContentIntent(contentIntent);
            notificationBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
            mNotificationManager.notify(1, notificationBuilder.build());
        } 
        else{
            mNotificationManager.cancel(SIMPLE_NOTIFICATION_ID);      
        }

    }

settings.java

import..//all imports i need
public class settings extends PreferenceActivity{
public void onCreate(Bundle savedInstanceState){
  super onCreate(savedInstanceState);
  setContentView(R.xml.settings);
  }
}

这是我的代码的结构..当然我有一个 xml 里面有带有 id 和 key 的 checkboxpreferences firstDependent

结束编辑。 我试过了onResume,只有当我从首选项屏幕出去时才能工作。我怎么能做我想做的事?

4

2 回答 2

0

你需要一个像这样的复选框监听器:

cb = (CheckBox)findViewById(R.id.checkBox);
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

   @Override
   public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
      sendSimpleNotification();
   }
}

对于复选框首选项,请使用:

final CheckBoxPreference cbPR= (CheckBoxPreference) getPreferenceManager().findPreference("cbPref");

cbPR.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {            
    public boolean onPreferenceChange(Preference preference, Object newValue) {

        sendSimpleNotification();
        return true;
    }
});
于 2013-08-01T12:07:28.153 回答
0

在您的 PreferenceActivity onCreate 中,注册一个偏好更改侦听器:

getSharedPreferences("YourPrefereceFile", 0).registerOnSharedPreferenceChangeListener(this);

然后,让您的活动实现 OnSharedPreferenceChangeListener。

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
        String key) {
    //Test on the key to know what preference has changed and do what ever you want

}

你修改了代码:

import..//all imports i need
public class settings extends PreferenceActivity implements OnSharedPreferenceChangeListener{
    public void onCreate(Bundle savedInstanceState){
      super onCreate(savedInstanceState);
      setContentView(R.xml.settings);
         PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
      }
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
                String key) {
        // test on your wanted key preference
        // Call your notification methode

    }    
    }
于 2013-08-01T13:15:04.563 回答