0

I want a simple Timer that executes a set of commands and it needs to be accurate. It must also continue if the App is minimised (hidden) or phone is on sleep (CPU sleep). I have looked at posts on these sites:

Site 1

Site 2

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()

        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

<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). I heard that the way to do this is to create and Alarm which creates a Service and the commands goes inside the service.

What am I 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 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!

Community
  • 1
  • 1
KickAss
  • 4,210
  • 8
  • 33
  • 41

1 Answers1

1

Assuming that MainActivity is actually an Activity, never instantiate Android components yourself.

Please use Log.d() or equivalent methods to log information from a Service for use in development.

Beyond that, as Class Stacker indicates, without a stack trace, it is difficult to assist you.

As the stack trace indicates, YourService needs to inherit from android.app.Service.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • The error you get should now be a different one. Is that correct @GBA? – Fildor Apr 25 '13 at 15:23
  • @GBA: It is better. Delete your `onCreate()` method, or at minimum remove the current code from it, as I indicated in my answer. – CommonsWare Apr 25 '13 at 15:57
  • If I remove the onCreate() Method, where will my commands (which are called by the Alarm service) go? :) Yes, I get a new error now. Let me post LogCat :) – KickAss Apr 25 '13 at 16:28
  • Actually, as it turns out, after removing the onCreate(), I get no errors now. But the only problem is, where does my commands go? Which method do I place them in? – KickAss Apr 25 '13 at 16:34
  • Inside the Service or AlarmReceiver class? – KickAss Apr 25 '13 at 16:37
  • 3
    @GBA: In the `Service`. You might consider reading the documentation on `Service`: http://developer.android.com/reference/android/app/Service.html – CommonsWare Apr 25 '13 at 16:42
  • Thanks :) Believe me, I have tried the Service documentation. Posting on this forum is my last resort. Sorry I'm a newbie. – KickAss Apr 25 '13 at 16:43
  • "[The System will] call its onStartCommand(Intent, int, int) method with the arguments supplied by the client." So you implement that and react as to what is requested in the Intent. – Fildor Apr 25 '13 at 16:51
  • Maybe this for you: http://developer.android.com/reference/android/app/IntentService.html – Fildor Apr 25 '13 at 16:54
  • I missed to put @GBA in above comments, so GBA: please see above :) – Fildor Apr 25 '13 at 17:07
  • Question: Why does the Alarm runs the service immediately on MainActivity's onStart? The AlarmManager setRepeating/setInexactRepeating long triggerAtTime is not working!. It's meant to start the first execution after 5 seconds. Also, is there a way to set the Alarm so it only executes once?? – KickAss Apr 25 '13 at 19:17