[英]AlarmManager and Service not working on my app
I want a simple Timer that executes a set of commands and it needs to be accurate. 我想要一个简单的Timer,它可以执行一组命令,并且需要准确。 It must also continue if the App is minimised (hidden) or phone is on sleep (CPU sleep).
如果应用程序已最小化(隐藏)或手机处于睡眠状态(CPU睡眠),则必须继续执行。 I have looked at posts on these sites:
我看过这些网站上的帖子:
I tried to understand the code and add it to my own new project, but the App closes on start. 我试图理解代码并将其添加到我自己的新项目中,但该应用程序在启动时关闭。 This is what I have.
这就是我所拥有的。
MainActivity onCreate() 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 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 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 by 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. 该应用程序在启动时崩溃。 What is wrong with the code?
代码有什么问题?
All I want is the App to start an Alarm through the MainActivity which which executes a set of commands every certain interval (even if the phone is on sleep). 我想要的就是该应用通过MainActivity启动警报,该警报每隔一定间隔(即使手机处于睡眠状态)执行一组命令。 I heard that the way to do this is to create and Alarm which creates a Service and the commands goes inside the service.
我听说做到这一点的方法是创建并创建一个Service的Alarm,并且命令进入服务内部。
What am I doing wrong? 我究竟做错了什么?
LogCat 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 to instantiate service com.example.wifischedule.YourService: java.lang.ClassCastException: com.example.wifischedule.YourService cannot be cast to android.app.Service
04-25 15:49:45.799: E/AndroidRuntime(32359): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2388)
04-25 15:49:45.799: E/AndroidRuntime(32359): at android.app.ActivityThread.access$1600(ActivityThread.java:140)
04-25 15:49:45.799: E/AndroidRuntime(32359): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
04-25 15:49:45.799: E/AndroidRuntime(32359): at android.os.Handler.dispatchMessage(Handler.java:99)
04-25 15:49:45.799: E/AndroidRuntime(32359): at android.os.Looper.loop(Looper.java:137)
04-25 15:49:45.799: E/AndroidRuntime(32359): at android.app.ActivityThread.main(ActivityThread.java:4898)
04-25 15:49:45.799: E/AndroidRuntime(32359): at java.lang.reflect.Method.invokeNative(Native Method)
04-25 15:49:45.799: E/AndroidRuntime(32359): at java.lang.reflect.Method.invoke(Method.java:511)
04-25 15:49:45.799: E/AndroidRuntime(32359): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
04-25 15:49:45.799: E/AndroidRuntime(32359): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
04-25 15:49:45.799: E/AndroidRuntime(32359): at 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 be cast to android.app.Service
04-25 15:49:45.799: E/AndroidRuntime(32359): at 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 that MainActivity
is actually an Activity
, never instantiate Android components yourself. 假设
MainActivity
实际上是一个Activity
,请不要自己实例化Android组件。
Please use Log.d()
or equivalent methods to log information from a Service
for use in development. 请使用
Log.d()
或等效方法记录来自Service
信息以用于开发。
Beyond that, as Class Stacker indicates, without a stack trace, it is difficult to assist you.
除此之外,正如Class Stacker指出的那样,如果没有堆栈跟踪,将很难为您提供帮助。
As the stack trace indicates, YourService
needs to inherit from android.app.Service
. 如堆栈跟踪所示,
YourService
需要从android.app.Service
继承。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.