[英]AlarmManager won't start service
I just followed the exact code in API Demo, but my service just won't start after I set the AlarmManager. 我只是按照API演示中的确切代码进行操作,但是在设置AlarmManager后,我的服务将无法启动。
so my service is 所以我的服务是
public class CourseWatcherRefreshService extends Service {
private CourseDbAdapter mDbHelper;
private CourseWatcher watcher;
@Override
public void onCreate() {
Toast.makeText(this, "Watcher Refresh Service starts", Toast.LENGTH_SHORT).show();
mDbHelper = new CourseDbAdapter(this);
mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mDbHelper.open();
Thread thread = new Thread(null, mTast, "CourseWatcherRefreshService");
thread.start();
super.onCreate();
}
@Override
public void onDestroy() {
mDbHelper.close();
super.onDestroy();
}
Runnable mTast = new Runnable() {
// some work
};
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
private final IBinder mBinder = new Binder() {
@Override
protected boolean onTransact(int code, Parcel data, Parcel reply, int flags)
throws RemoteException {
return super.onTransact(code, data, reply, flags);
}
};
}
Also in my AndroidManifest.xml, I put such code 同样在我的AndroidManifest.xml中,我放了这样的代码
<service
android:name=".CourseWatcherRefreshService"
android:process=":remote" >
</service>
And I use such code to toggle the AlarmManager 而且我使用这样的代码来切换AlarmManager
PendingIntent refreshIntent = PendingIntent.getActivity(NaviScreen.this, 0, new Intent(NaviScreen.this, CourseWatcherRefreshService.class), 0);
long firstTime = SystemClock.elapsedRealtime();
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime, 30*1000, refreshIntent);
And the code above is almost the same with the AlartService example in API Demo, but my code won't work, I can't see any sign of my service starts to work. 上面的代码与API演示中的AlartService示例几乎相同,但是我的代码无法正常工作,我看不到任何服务迹象开始起作用。
You are using getActivity
when you should be using getService
. 应该在使用
getService
时使用getActivity
。 So change the PendingIntent line to 因此,将PendingIntent行更改为
PendingIntent refreshIntent = PendingIntent.getService(NaviScreen.this, 0, new Intent(NaviScreen.this, CourseWatcherRefreshService.class), 0);
try this : 尝试这个 :
Intent updateIntent = new Intent()
updateIntent.setClass(NaviScreen.this, CourseWatcherRefreshService.class)
PendingIntent pendingIntent = PendingIntent.getService(this, 0, updateIntent, 0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.