android - How to use broadcastreceiver class in service class -


in example firstly starting activity class in calling startservice() , getting toast of "in on create" oncreate method of service class , in service class did coding

public void oncreate() {         // todo auto-generated method stub         super.oncreate();         toast.maketext(getapplicationcontext(), "in on create", toast.length_short).show();      }  public void onstart(intent arg0, int startid) {         // todo auto-generated method stub         super.onstart(arg0, startid);      }      public void broadcastintent(view view)        {           intent intent = new intent();           intent.setaction("android.intent.action.phone_state");           sendbroadcast(intent);        } 

in broadcast receiver class did this

public void onreceive(context context, intent intent) {         // todo auto-generated method stub          string state = intent.getstringextra(telephonymanager.extra_state);         if (state.equals(telephonymanager.extra_state_ringing)) {             toast.maketext(context,"phone ringing", toast.length_long).show();             intent i=new intent(context,mainactivity.class);             i.putextra("state", state);             context.startactivity(i);          }       } 

and in manifest file did...

<uses-permission android:name="android.permission.read_phone_state"/>   <receiver              android:name="start">              </receiver>              <service                  android:name="run"></service> 

declare receiver in manifest this...

    <receiver         android:name=".start"         android:priority="999" >         <intent-filter>             <action android:name="android.intent.action.phone_state" />         </intent-filter>     </receiver> 

here android:name=".start" represents class name of broadcastreceiver. here start class name of broadcastreceiver package equal package name of application (package name of app) , not allowed send android.intent.action.phone_state broadcast manually

and comments, may you.

    togglebutton             .setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() {                  @override                 public void oncheckedchanged(compoundbutton buttonview,                         boolean ischecked) {                     buttonview                             .getcontext()                             .getsharedpreferences("app_preference",                                     context.mode_private).edit()                             .putboolean("is_receiver_enabled", ischecked)                             .commit();                  }             }); 

and in onreceive()

    @override     public void onreceive(context context, intent intent) {         boolean isenabled = context.getsharedpreferences("app_preference",                 context.mode_private).getboolean("is_receiver_enabled",                 false);         if (isenabled) {             // show toast here.         }     } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -