Android AlarmManager lifecycle not working -


i'm trying make app fires user set timed notification, once pressed send user requested website. thing is, since these notifications can fire after hours, android restarts application, erasing alarms , notifications. i've tried putting persistent attribute in manifest extend life still erases everything. tried finding way save timers in bundle not find way make fire notification on time.

note: happens when app set in background long.

this mainactivity

 protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     // makes app continues last session if created     if(!istaskroot()) {         finish();     }   } protected void onactivityresult(int requestcode, int resultcode, intent data) {     if (requestcode == 15){         name = data.getstringextra("name");         string x = data.getstringextra("url");         hours = data.getintextra("hour", 0);         minutes = data.getintextra("minute",0);           alarmmanager alarmmanager =                 (alarmmanager) getsystemservice(alarm_service);          if((!x.contains("http://") && (!x.contains("https://")))){             x = "http://" + x ;         }         final uri u = uri.parse(x);         calendar executetime = calendar.getinstance();         executetime.add(calendar.minute, minutes);         executetime.add(calendar.hour, hours);         /*broadcastreceiver br = new broadcastreceiver() {             @override             public void onreceive(context context, intent intent) {                 notification(name, u);                 startactivity(new intent(intent.action_view, u));              }         };         registerreceiver(br, new intentfilter(this.getclass().getsimplename()) );          pendingintent pi =                 pendingintent.getbroadcast(getapplicationcontext(), 0, new intent(this.getclass().getsimplename()), 0);         */         //intent = new intent(this,alarmreceiver.class);         alarmreceiver ar = new alarmreceiver(name,u);         intent = new intent(this,ar.getclass());          pendingintent pi =                 pendingintent.getbroadcast(this,0,i,0);         alarmmanager.set(alarmmanager.rtc_wakeup, executetime.gettimeinmillis(), pi);          //notification(name,u,executetime);          listview listview = (                 listview) findviewbyid(r.id.bountyview);          arrayadapter<string> madapter =                 new arrayadapter<>(getapplicationcontext(),android.r.layout.simple_list_item_1,list);         listview.setadapter(madapter);          decimalformat time = new decimalformat("00");         //adds said data list(in case time gets executed)         madapter.add(name + " | " + executetime.get(calendar.hour) + ":" + time.format(executetime.get(calendar.minute))); //+" " +calendar.getinstance().get(calendar.am_pm));         madapter.notifydatasetchanged();     }     //if (resultcode == result_canceled) {         //write code if there's no result     //} }  public void notification (string name,uri u){     // prepare intent triggered if     // notification selected      //intent intent = new intent(this, notificationreceiver.class);     pendingintent pintent = pendingintent.getactivity(this, 0, (new intent(intent.action_view, u)), 0);      // build notification     // addaction re-use same intent keep example short     notification n  = new notification.builder(this)             .setcontenttitle("time attack " + name)             .setcontenttext("timer has finished, time attack!")             .setsmallicon(r.drawable.ic_launcher)             .setcontentintent(pintent)             .setautocancel(true)             .build();       notificationmanager notificationmanager =             (notificationmanager) getsystemservice(notification_service);     n.flags = notification.flag_auto_cancel;     n.defaults |= notification.default_sound;      notificationmanager.notify(0, n); } 

this broadcast receiver class public class alarmreceiver extends broadcastreceiver {

string name; uri url;   @override public void onreceive(context context, intent intent) {     notification(name, url ,context);     context.startactivity(new intent(intent.action_view,url));  } public alarmreceiver(){} public alarmreceiver(string name, uri u){     name = name;     url = u; } public void setname(string name){     name = name; } public void seturl(uri u){     url = u; } public void notification (string name,uri u, context context ){     // prepare intent triggered if     // notification selected      //intent intent = new intent(this, notificationreceiver.class);     pendingintent pintent = pendingintent.getactivity(context, 0, (new intent(intent.action_view, u)), 0);      // build notification     // addaction re-use same intent keep example short     notification n  = new notification.builder(context)             .setcontenttitle("time attack " + name)             .setcontenttext("timer has finished, time attack!")             .setsmallicon(r.drawable.ic_launcher)             .setcontentintent(pintent)             .setautocancel(true)             .build();       notificationmanager notificationmanager =             (notificationmanager) context.getsystemservice(context.notification_service);     n.flags = notification.flag_auto_cancel;     n.defaults |= notification.default_sound;      notificationmanager.notify(0, n); } 

}

my manifest

    <?xml version="1.0" encoding="utf-8"?> <manifest     xmlns:android="http://schemas.android.com/apk/res/android"     package="com.omnination.ossusum.bountyurltimer"     android:versioncode="7"     android:versionname="7.0"      >     <uses-permission android:name="com.android.alarm.permission.set_alarm"/>     <application         android:persistent="true"         android:allowbackup="true"         android:icon="@drawable/ic_launcher"         android:label="@string/app_name"         android:theme="@style/apptheme" >         <activity             android:name=".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>         <activity             android:name=".newtimer"             android:label="@string/title_activity_new_timer" >         </activity>         <receiver             android:name=".alarmreceiver"             android:enabled="true"             />     </application>  </manifest> 

some code might redundant because of me playing around it, trying figure out.

you should not use timer classes such use cases timer class not work when device goes idle. instead please use alarm manager


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -