gpt4 book ai didi

java - AlarmManager 和服务无法在我的应用程序上运行

转载 作者:行者123 更新时间:2023-12-01 14:38:40 25 4
gpt4 key购买 nike

我想要一个简单的计时器来执行一组命令,并且它需要准确。如果应用程序最小化(隐藏)或手机处于 sleep 状态(CPU sleep ),它也必须继续。我查看了这些网站上的帖子:

Site 1

Site 2

我尝试理解代码并将其添加到我自己的新项目中,但应用程序在启动时关闭。这就是我所拥有的。

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 by adding Service extension
// TODO Auto-generated method stub
return null;
}
}

list

<manifest
... >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />

<application
... >

<service android:name=".YourService"></service>
<receiver android:name=".AlarmReceiver"></receiver>

<activity
... >
...
</activity>
</application>
</manifest>

应用程序在启动时崩溃。代码有什么问题?

我想要的只是应用程序通过 MainActivity 启动闹钟,它每隔一定的时间间隔执行一组命令(即使手机处于 sleep 状态)。我听说执行此操作的方法是创建警报,它创建一个服务,并且命令进入服务内部。

我做错了什么?

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

谢谢!

最佳答案

假设 MainActivity 实际上是一个 Activity永远不要自己实例化 Android 组件。

请使用Log.d()或等效方法从Service记录信息以供开发使用。

除此之外,正如 Class Stacker 所指出的,如果没有堆栈跟踪,就很难为您提供帮助。

正如堆栈跟踪所示,YourService 需要继承自 android.app.Service

关于java - AlarmManager 和服务无法在我的应用程序上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16217840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com