0
您好我有问题启动IntentService作为前台服务。不幸的是,官方教程并没有告诉我很多方法不存在,有些已被弃用,而且也没有说它们提供的代码的放置位置。前景中的Android IntentService
我创建了自己IntentService,我已经重写onCreate方法。它看起来如下:
@Override
public void onCreate(){
super.onCreate();
Intent notificationIntent = new Intent(this, Settings.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new Notification.Builder(this)
.setContentTitle(getText(R.string.serviceName))
.setContentText(getText(R.string.serviceDescription))
.setSmallIcon(R.mipmap.ic_launcher)
.setOngoing(true)
.setContentIntent(pendingIntent)
.build();
startForeground(101, notification);
我知道这不是调试现场,但肯定有一些东西很明显,我很想念。 Settings类是我的Activity类,从中调用startService,我还将所有需要的东西都设置为通知,并将startForeground称为非零第一个参数。尽管我很确定该服务在后台工作,但仍然没有通知出现。
任何帮助,将不胜感激(顺便说一句,我已经搜索了在前台SO woth服务不同的主题,但没有帮助。)
你的用例是什么让它在前台? – apelsoczi
应用程序是:你设置一些设置即。电话号码。从设置活动中,按下启动按钮,服务启动并监听一些事件,如果发生事件,服务会发出呼叫(例如,在电话发起动作时有用,但它独自留在家中)。我希望通知用户,该服务处于活动状态。您可以从设置活动中禁用服务。 – DawidPi
由于一个IntentService是onHandleIntent后立即销毁()完成后,你可能不希望一个IntentService – ianhanniballake