AlarmManager 不会启动服务

发布于 2024-12-18 03:04:13 字数 1865 浏览 3 评论 0原文

我只是按照 API 演示中的确切代码进行操作,但是在设置 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演示中的AlartService示例几乎相同,但是我的代码不起作用,我可以没有看到我的服务开始工作的任何迹象。

I just followed the exact code in API Demo, but my service just won't start after I set the 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

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

And I use such code to toggle the 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁吢 2024-12-25 03:04:13

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

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

You are using getActivity when you should be using getService. So change the PendingIntent line to

PendingIntent refreshIntent = PendingIntent.getService(NaviScreen.this, 0, new Intent(NaviScreen.this, CourseWatcherRefreshService.class), 0);
娇纵 2024-12-25 03:04:13

试试这个:

Intent updateIntent = new Intent() 
updateIntent.setClass(NaviScreen.this, CourseWatcherRefreshService.class) 
PendingIntent pendingIntent = PendingIntent.getService(this, 0, updateIntent, 0);

try this :

Intent updateIntent = new Intent() 
updateIntent.setClass(NaviScreen.this, CourseWatcherRefreshService.class) 
PendingIntent pendingIntent = PendingIntent.getService(this, 0, updateIntent, 0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文