gpt4 book ai didi

java - AlarmManager 不会启动服务

转载 作者:太空狗 更新时间:2023-10-29 16:08:39 25 4
gpt4 key购买 nike

我只是按照 API Demo 中的确切代码操作,但我的服务在设置 AlarmManager 后无法启动。

所以我的服务是

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

}

同样在我的AndroidManifest.xml中,我放了这样的代码

<service
android:name=".CourseWatcherRefreshService"
android:process=":remote" >
</service>

我使用这样的代码来切换 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);

上面的代码与API Demo中的AlartService示例几乎相同,但是我的代码无法运行,我看不到我的服务开始运行的任何迹象。

最佳答案

当您应该使用 getService 时,您却在使用 getActivity。因此,将 PendingIntent 行更改为

PendingIntent refreshIntent = PendingIntent.getService(NaviScreen.this, 0, new Intent(NaviScreen.this, CourseWatcherRefreshService.class), 0);

关于java - AlarmManager 不会启动服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8291931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com