如果我想显示通知,我使用此代码从首选项屏幕进行设置。问题是此代码仅在我创建一个按钮以进入首选项屏幕时才有效。我想使用我的子菜单“设置”按钮而不是布局内的按钮继续首选项。这是代码
// prefer
setPref.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(
MainActivity .this,
settings.class);
startActivityForResult(intent, 0);
}});
checkPref();
}
@SuppressLint("NewApi")
private void checkPref(){
SharedPreferences myPref
= PreferenceManager.getDefaultSharedPreferences(
MainActivity.this);
boolean pref_opt1 = myPref.getBoolean("pref_opt1", false);
if (pref_opt1){
NotificationManager notifi = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(getApplicationContext())
.setContentTitle("Battery Informations")
.setContentText("Batteria al")
.setSmallIcon(R.drawable.icon_small_not)
//.setLargeIcon(aBitmap)
.setTicker("Livello")
.build();
notification.flags = Notification.FLAG_ONGOING_EVENT;
Intent i = new Intent(MainActivity.this, MainActivity.class);
PendingIntent penInt = PendingIntent.getActivity(getApplicationContext(), 0 , i , 0);
notifi.notify(215,notification);
} else {
NotificationManager notifi = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notifi.cancel(215);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
checkPref();
}
我该怎么做?