我想要一个执行一组命令的简单Timer,它需要准确。如果应用程序最小化(隐藏)或手机处于睡眠状态(CPU睡眠),它也必须继续。我查看了这些网站上的帖子:
我试图理解代码并将其添加到我自己的新项目中,但应用程序在启动时关闭。这就是我所拥有的。
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启动警报,MainActivity每隔一定时间间隔执行一组命令(即使手机处于休眠状态)。我听说这样做的方法是创建和报警,它创建一个服务,命令进入服务。
我做错了什么?
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
谢谢!
答案 0 :(得分:1)
假设MainActivity
实际上是Activity
,从不自己实例化Android组件。
请使用Log.d()
或等效方法记录Service
中的信息,以便用于开发。
除此之外,正如Class Stacker所指出的那样,没有堆栈跟踪,很难为你提供帮助。
如堆栈跟踪所示,YourService
需要从android.app.Service
继承。