2013-04-25 84 views
0

我想要一個簡單的計時器執行一組命令,它需要是準確的。如果應用程序被最小化(隱藏)或手機處於睡眠狀態(CPU睡眠),它也必須繼續。我已經看過帖子在這些網站上:AlarmManager和服務不能在我的應用程序工作

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; 
    } 
} 

清單

<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啓動一個鬧鐘,它每隔一定的時間間隔執行一組命令(即使手機處於睡眠狀態)。我聽說要做到這一點的方法是創建一個Alarm並創建一個Service,並將命令放入服務中。

我在做什麼錯?

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): [email protected] > dumpstate -k -t -z -d -o /data/log/dumpstate_app_error 
04-25 15:49:52.724: E/FaceDetectionService(2268): enabled 

謝謝!

+4

堆棧溢出俱樂部的規則第一:如果你的應用程序崩潰,發佈logcat(和指導人員關於行號)。 – 2013-04-25 14:44:57

+0

我的歉意。 LogCat發佈:) – KickAss 2013-04-25 14:50:44

+0

似乎你的服務不是服務;) – Fildor 2013-04-25 14:53:12

回答

1

假設MainActivity實際上是Activity,從來沒有自己實例化Android組件。

請使用Log.d()或同等方法記錄Service的信息以供開發使用。

除此之外,正如Class Stacker所示,沒有堆棧跟蹤,很難爲您提供幫助。

作爲堆棧跟蹤指示,YourService需要從android.app.Service繼承。

+0

請參閱上面的LogCat :) – KickAss 2013-04-25 14:51:34

+0

@GBA:請參閱更新。 – CommonsWare 2013-04-25 14:53:52

+0

查看上面更新的YouService。那是對的嗎? – KickAss 2013-04-25 14:59:37

相關問題