android - BroadcastReceiver class must work only when the toggle button is checked -
hey guys want broadcastreceiver
must work when togglebutton
checked.... code have used is...
in manifest.xml
file...
<receiver android:name="start"> <intent-filter> <action android:name="android.intent.action.phone_state" /> </intent-filter> </receiver>
in activity
class...
togglebutton tgl; string prefs="app_preference"; sharedpreferences sharedprefs; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); tgl=(togglebutton)findviewbyid(r.id.togglebutton1); tgl.setoncheckedchangelistener(this); sharedprefs = getsharedpreferences(prefs,mode_private); tgl.setchecked(sharedprefs.getboolean("is_receiver_enabled", false)); public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { // todo auto-generated method stub if(tgl.ischecked()){ sharedpreferences.editor editor = sharedprefs.edit(); editor.putboolean("is_receiver_enabled", true); editor.commit(); }else{ sharedpreferences.editor editor = sharedprefs.edit(); editor.putboolean("is_receiver_enabled", false); editor.commit(); }
in broadcastreceiver
class...
public void onreceive(context context, intent intent) { // todo auto-generated method stub boolean isenabled = context.getsharedpreferences("app_preference", context.mode_private).getboolean("is_receiver_enabled", false); if (isenabled) { toast.maketext(context,"intent detected", toast.length_long).show(); } }
this possible if register receiver @ activity level(not manifest) using
when toggle button disable
registerreceiver(broadcastreceiver, intentfilter)
and when toggle button disable can unregister using..
unregisterreceiver(broadcastreceiver receiver)
Comments
Post a Comment