java - Android not recreating service even when using START_STICKY -


i trying create service , set alarm through it. need run service in background keep broadcaster reciever running if app closed. tried using start_sticky, running service in separate child process global process, in vain. should ?

alarm.java

package com.simpleapps.simpleweather; import ...  public class alarm extends broadcastreceiver {     @override     public void onreceive(context context, intent intent)     {         tonegenerator tonegen1 = new tonegenerator(audiomanager.stream_music, 100);         tonegen1.starttone(tonegenerator.tone_cdma_pip,150);         toast.maketext(context, "alarm !!!!!!!!!!", toast.length_long).show();     }      public void setalarm(context context)     {         log.d("alarm","started");         alarmmanager =( alarmmanager)context.getsystemservice(context.alarm_service);         intent = new intent(context, alarm.class);         pendingintent pi = pendingintent.getbroadcast(context, 0, i, 0);         am.setrepeating(alarmmanager.rtc_wakeup, system.currenttimemillis(), 1000 * 60, pi); // millisec * second * minute     }      public void cancelalarm(context context)     {         intent intent = new intent(context, alarm.class);         pendingintent sender = pendingintent.getbroadcast(context, 0, intent, 0);         alarmmanager alarmmanager = (alarmmanager) context.getsystemservice(context.alarm_service);         alarmmanager.cancel(sender);     } } 

helloservice.java

package com.simpleapps.simpleweather; import ...  public class helloservice extends service {     alarm alarm = new alarm();     public void oncreate()     {         super.oncreate();     }      @override     public int onstartcommand(intent intent, int flags, int startid)     {         log.d("alarm","new service");         alarm.setalarm(this);         return start_sticky;     }      @override     public void onstart(intent intent, int startid)     {         alarm.setalarm(this);     }      @override     public ibinder onbind(intent intent)     {         return null;     } } 

manifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.simpleapps.simpleweather">      <uses-permission android:name="android.permission.internet" />     <uses-permission android:name="android.permission.wake_lock"/>     <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>         <activity android:name=".today_details" />         <activity android:name=".forecast_details"/>         <receiver android:process=":remote" android:name=".alarm" android:exported="true">         </receiver>          <service             android:name=".helloservice"             android:enabled="true">         </service>      </application>  </manifest> 

starting service -

intent intent = new intent(this, helloservice.class); startservice(intent); 

update : working on every device except lollipop 5.0 device asuszenfone2

i've tried code , it's working ok on emulator. 1 thing i've notice that:

 * setrepeating  * <b>note:</b> beginning in api 19, trigger time passed method  * treated inexact: alarm not delivered before time,  * may deferred , delivered time later.  os use  * policy in order "batch" alarms across entire system,  * minimizing number of times device needs "wake up" , minimizing  * battery use.  in general, alarms scheduled in near future not  * deferred long alarms scheduled far in future. 

so i've change alarm (it's illustration, without static fields):

public class alarm extends broadcastreceiver {     private static boolean salarmcanceled = false;      @override     public void onreceive(context context, intent intent)     {         if (salarmcanceled)             return;           tonegenerator tonegen1 = new tonegenerator(audiomanager.stream_music, 100);         tonegen1.starttone(tonegenerator.tone_cdma_pip,150);         toast.maketext(context, "alarm !!!!!!!!!!", toast.length_short).show();         schedulenextalarm(context);     }      public static void enablealarm(context context) {         salarmcanceled = false;         schedulenextalarm(context);     }      public static void disablealarm() {         salarmcanceled = true;     }      private static void schedulenextalarm(context context)     {         alarmmanager =( alarmmanager)context.getsystemservice(context.alarm_service);         intent = new intent(context, alarm.class);         pendingintent pi = pendingintent.getbroadcast(context, 0, i, pendingintent.flag_one_shot);         am.set(alarmmanager.rtc_wakeup, system.currenttimemillis() + 10000, pi);     } } 

and executed precisely every 10 sec. service runs normal , able remove application task , see alarm anyway.


Comments