java - AlarmManager and Service not working on my app -
i want simple timer executes set of commands , needs accurate. must continue if app minimised (hidden) or phone on sleep (cpu sleep). have looked @ posts on these sites:
i tried understand code , add own new project, app closes on start. have.
mainactivity oncreate()
intent myalarm = new intent(getapplicationcontext(), alarmreceiver.class); pendingintent recurringalarm = pendingintent.getbroadcast(getapplicationcontext(), 0, myalarm, pendingintent.flag_cancel_current); alarmmanager alarms = (alarmmanager) this.getsystemservice(context.alarm_service); calendar updatetime = calendar.getinstance(); updatetime.settimeinmillis(5000); // first reoccurance int custominterval = 5000; // 5 seconds intervals alarms.setinexactrepeating(alarmmanager.rtc_wakeup, updatetime.gettimeinmillis(), custominterval, recurringalarm);
alarmreceiver.class/alarmreceiver.java
import android.content.broadcastreceiver; import android.content.context; import android.content.intent; public class alarmreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { intent myservice = new intent(context, yourservice.class); context.startservice(myservice); } }
yourservice.class/yourservice.java
import android.app.service; import android.content.intent; import android.os.ibinder; public class yourservice extends service { @override public ibinder onbind(intent intent) { // automatically added adding service extension // todo auto-generated method stub return null; } }
manifest
<manifest ... > <uses-sdk android:minsdkversion="8" android:targetsdkversion="17" /> <application ... > <service android:name=".yourservice"></service> <receiver android:name=".alarmreceiver"></receiver> <activity ... > ... </activity> </application> </manifest>
the app crashes on start. wrong code?
all want app start alarm through mainactivity which executes set of commands every interval (even if phone on sleep). heard way create , alarm creates service , commands goes inside service.
what doing wrong?
logcat
04-25 15:49:45.799: e/androidruntime(32359): fatal exception: main 04-25 15:49:45.799: e/androidruntime(32359): java.lang.runtimeexception: unable instantiate service com.example.wifischedule.yourservice: java.lang.classcastexception: com.example.wifischedule.yourservice cannot cast android.app.service 04-25 15:49:45.799: e/androidruntime(32359): @ android.app.activitythread.handlecreateservice(activitythread.java:2388) 04-25 15:49:45.799: e/androidruntime(32359): @ android.app.activitythread.access$1600(activitythread.java:140) 04-25 15:49:45.799: e/androidruntime(32359): @ android.app.activitythread$h.handlemessage(activitythread.java:1309) 04-25 15:49:45.799: e/androidruntime(32359): @ android.os.handler.dispatchmessage(handler.java:99) 04-25 15:49:45.799: e/androidruntime(32359): @ android.os.looper.loop(looper.java:137) 04-25 15:49:45.799: e/androidruntime(32359): @ android.app.activitythread.main(activitythread.java:4898) 04-25 15:49:45.799: e/androidruntime(32359): @ java.lang.reflect.method.invokenative(native method) 04-25 15:49:45.799: e/androidruntime(32359): @ java.lang.reflect.method.invoke(method.java:511) 04-25 15:49:45.799: e/androidruntime(32359): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1006) 04-25 15:49:45.799: e/androidruntime(32359): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:773) 04-25 15:49:45.799: e/androidruntime(32359): @ dalvik.system.nativestart.main(native method) 04-25 15:49:45.799: e/androidruntime(32359): caused by: java.lang.classcastexception: com.example.wifischedule.yourservice cannot cast android.app.service 04-25 15:49:45.799: e/androidruntime(32359): @ android.app.activitythread.handlecreateservice(activitythread.java:2385) 04-25 15:49:45.799: e/androidruntime(32359): ... 10 more 04-25 15:49:45.854: e/android.os.debug(2268): !@dumpstate > dumpstate -k -t -z -d -o /data/log/dumpstate_app_error 04-25 15:49:52.724: e/facedetectionservice(2268): enabled
thanks!
assuming mainactivity
activity
, never instantiate android components yourself.
please use log.d()
or equivalent methods log information service
use in development.
beyond that, class stacker indicates, without stack trace, difficult assist you.
as stack trace indicates, yourservice
needs inherit android.app.service
.