如何在首选项屏幕(checkboxpreferene)中创建一个简单的复选框以显示通知?我的意思是我想在我的应用程序设置(首选项屏幕)中输入,然后我会找到复选框(显示通知)。选中时启动通知(现在是:我的第一个通知之类的文本)。显然,当不检查通知消失时。我找不到任何关于它的指南。到目前为止,这是我的代码的一部分:首选项屏幕:
public class settings extends PreferenceActivity {
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
设置.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:summary="@string/menu_settings"
android:title="@string/Settings">
<CheckBoxPreference
android:key="firstDependent"
android:summary="@string/clicca_il_primo_check"
android:title="@string/Primocheck"
android:defaultValue="false"
/>
</PreferenceCategory>
<!--Any other categories include here-->
</PreferenceScreen>
在我的 MainActivity
@SuppressLint("NewApi")
private void checkPref(Intent intent){
SharedPreferences myPref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
boolean pref_opt1 = myPref.getBoolean("firstDependent", false);
if (pref_opt1){
NotificationManager notifi = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(getApplicationContext())
.setContentTitle("Hello Informations")
.setContentText("Hellow world")
.setSmallIcon(R.drawable.icon_small_not)
.setTicker("Helloooo")
.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);
}
}
但是当我选中复选框时通知不会开始。