進入首選項屏幕我使用此代碼從首選項屏幕進行設置,如果我想顯示通知或不顯示通知。問題是,這個代碼只有當我創建一個按鈕去偏好屏幕上的作品。我想要使用我的子菜單「設置」按鈕進行偏好設置,而不是佈局中的按鈕。 Thsi是密碼使用子菜單
// 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();
}
我該怎麼辦?
謝謝..但你的意思是整個代碼或只是一個部分?從'@Override public void onClick(View arg0)...'還是別的? –