0

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

站点 1

站点 2

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

主活动 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;
    }
}

显现

<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 启动警报,它每隔一定的时间间隔执行一组命令(即使手机处于睡眠状态)。我听说这样做的方法是创建和创建服务的警报,并且命令进入服务内部。

我究竟做错了什么?

日志猫

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

谢谢!

4

1 回答 1

1

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

请使用Log.d()或等效方法记录来自 a的信息以Service用于开发。

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

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

于 2013-04-25T14:49:03.213 回答