android - How to cancel the repeated alarm and scheduled so that it fire up every after 5 sec? -


i use broadcast receiver repeated notification notify user every after 5 sec when receiver cut call or come in idle state. **

mainactivity.java

    protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     btn1 = (button) findviewbyid(r.id.callbutton);      phonecalllistener phonelistener = new phonecalllistener();     telephonymanager telephonymanager = (telephonymanager)             .getsystemservice(context.telephony_service);     telephonymanager.listen(phonelistener,  phonestatelistener.listen_call_state);     btn1.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {              intent callintent = new intent(intent.action_call);             callintent.setdata(uri.parse("tel:0000000000"));              startactivity(callintent);         }     }); }  private class phonecalllistener extends phonestatelistener {     string log_tag = "logging 123 check phone call";     private boolean iscallconnect = false;     private boolean makecallonce = false;      public void oncallstatechanged(int state, string incoming_no) {         if (telephonymanager.call_state_ringing == state) {             // active             log.i(log_tag, "ringing");             toast.maketext(mainactivity.this, "ringing",      toast.length_short).show();             iscallconnect = true;             makecallonce = true;         }         if (telephonymanager.call_state_offhook == state) {             // active             log.i(log_tag, "offhook");             toast.maketext(mainactivity.this, "offhook state", toast.length_short).show();             iscallconnect = true;             makecallonce = true;         }         if (telephonymanager.call_state_idle == state) {             log.i(log_tag, "call state idle");             toast.maketext(mainactivity.this, "idle state", toast.length_short).show();              if (makecallonce) {                 alertnotification();                 toast.maketext(mainactivity.this, "call disconnected", toast.length_short).show();              }         }     }      public void alertnotification() {         toast.maketext(this, "alert notification", length_long).show();         alarmintent = new intent(mainactivity.this, alertreceiver.class);         pendingintent1 = pendingintent.getbroadcast(mainactivity.this, 0, alarmintent, 0);         manager = (alarmmanager) getsystemservice(context.alarm_service);         int interval = 5000;         manager.setinexactrepeating(alarmmanager.elapsed_realtime_wakeup,alarmmanager.interval_fifteen_minutes,             alarmmanager.interval_fifteen_minutes, pendingintent1);         toast.maketext(this, "alarm set", toast.length_short).show();         isbuttonpressed = true;     }      public void stopalertnotification(view v) {         manager = (alarmmanager) getsystemservice(context.alarm_service);         manager.cancel(pendingintent1);         toast.maketext(this, "alarm cancelled", toast.length_short).show();     } } 

activity_main.xml

  <button     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="call"     android:textsize="25sp"     android:gravity="center"     android:layout_margintop="25dp"     android:id="@+id/callbutton"      />   <button     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="cancel reminder"     android:textsize="25sp"     android:onclick="stopalertnotification"     android:gravity="center"     android:layout_below="@+id/callbutton"     android:id="@+id/cancelbutton"     />     </relativelayout> 

alertreceiver.java

    public class alertreceiver extends broadcastreceiver {     notificationmanager notificationmanager;      @override     public void onreceive(context context, intent intent) {         createnotification(context,"times "," 5sec has passed","alert");      }          public void createnotification(context context,string msg,string    msgtext,string msgalert)     {         long alerttime=new gregoriancalendar().gettimeinmillis()+5*1000;         toast.maketext(context, "i running ", length_long).show();         intent intent=new intent(context, mainactivity.class);         notificationcompat.builder mbuilder=new       notificationcompat.builder(context)             .setcontenttitle(msg)             .setticker(msgalert)             .setcontenttext(msgtext)             .setsmallicon(r.drawable.fail);         pendingintent pendingintent=pendingintent.getactivity(context, 0,                 intent, pendingintent.flag_update_current);         mbuilder.setcontentintent(pendingintent);         mbuilder.setdefaults(notificationcompat.default_sound);         mbuilder.setautocancel(true);         notificationmanager notificationmanager=(notificationmanager)             context.getsystemservice(context.notification_service);         notificationmanager.notify(0,mbuilder.build());     }      public void stopnotification()     {         notificationmanager.cancel(0);     } } 

androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?> 

<uses-permission android:name="android.permission.call_phone" /> <uses-permission android:name="android.permission.read_phone_state" />  <application     android:allowbackup="true"     android:icon="@mipmap/ic_launcher"     android:label="@string/app_name"     android:supportsrtl="true"     android:theme="@style/apptheme">     <activity android:name=".mainactivity">         <intent-filter>             <action android:name="android.intent.action.main" />              <category android:name="android.intent.category.launcher" />         </intent-filter>     </activity>     <receiver android:name=".alertreceiver"/> </application> 


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -