在哪里打电话通知?

时间:2013-08-01 12:04:44

标签: android android-preferences android-notifications checkboxpreference

我在哪里“打电话”我的通知?我希望在单击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 firstDependent的checkboxpreferences。

结束编辑。 我试过onResume并且只有在我从偏好设置屏幕出来时才有效。我怎么能做我想做的事?

2 个答案:

答案 0 :(得分: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;
    }
});

答案 1 :(得分: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

    }    
    }