how can create a.simple check box in preferences screen (a checkboxpreferene) show notification?what mean want enter in application settings(the preferences screen) , there i'll find checkbox (show notification).when checked start notification (for text :my first notification). when not checked notification disappear.i can't find guide it. part of code far: preferences screen:
public class settings extends preferenceactivity { @suppresswarnings("deprecation") @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); addpreferencesfromresource(r.xml.settings); } }
the 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>
and in 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 = new intent(mainactivity.this, mainactivity.class); pendingintent penint = pendingintent.getactivity(getapplicationcontext(), 0 , , 0); notifi.notify(215,notification); } else { notificationmanager notifi = (notificationmanager)getsystemservice(context.notification_service); notifi.cancel(215); } }
but notification not start when check checkbox.
look @ link : android sdk: using alerts, toasts , notifications
here have tested , works (tested on real tablet) :
public class mainactivity extends preferenceactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button button = (button) findviewbyid(r.id.button); button.setonclicklistener(new button.onclicklistener() { public void onclick(view v){ final int notif_id = 1234; notificationmanager notifmanager = (notificationmanager) getsystemservice(notification_service); notification note = new notification(r.drawable.ic_launcher, "new e-mail", system.currenttimemillis()); pendingintent intent = pendingintent.getactivity(getapplicationcontext(), 0, new intent(getapplicationcontext(), mainactivity.class), 0); note.setlatesteventinfo(getapplicationcontext(), "new e-mail", "you have 1 unread message.", intent); notifmanager.notify(notif_id, note); // notifmanager.cancel(notif_id); } }); }
}
i've done on button , not checkbox, same.
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk android:minsdkversion="11" android:targetsdkversion="17" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name="com.example.za.mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <listview android:id="@android:id/list" android:layout_width="wrap_content" android:layout_height="wrap_content" ></listview> <button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" />
Comments
Post a Comment