IntentService的StartForeground

时间:2012-03-29 17:53:21

标签: android intentservice foreground-service

我有一个IntentService我希望通过持续通知让它变得粘稠。问题是通知出现然后立即消失。该服务继续运行。我应该如何在startForeground()中使用IntentService

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    Notification notification = new Notification(R.drawable.marker, "Notification service is running",
            System.currentTimeMillis());
    Intent notificationIntent = new Intent(this, DashboardActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|
        Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.setLatestEventInfo(this, "App",
            "Notification service is running", pendingIntent);
    notification.flags|=Notification.FLAG_NO_CLEAR;
    startForeground(1337, notification);
    return START_STICKY;
}

@Override
protected void onHandleIntent(Intent intent) {

    String id = intent.getStringExtra(ID);
    WebSocketConnectConfig config = new WebSocketConnectConfig();
    try {
        config.setUrl(new URI("ws://" + App.NET_ADDRESS
                + "/App/socket?id="+id));
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    ws = SimpleSocketFactory.create(config, this);
    ws.open();
}

由于

2 个答案:

答案 0 :(得分:16)

这不应该是IntentService。如上所述,您的IntentService将活一毫秒左右。一旦onHandleIntent()返回,服务就会被销毁。这应该是常规Service,您可以在其中分叉自己的线程并管理线程和服务的生命周期。

Notification立即离开的原因是服务立即消失。

答案 1 :(得分:4)

IntentService状态为documentation

  

...根据需要启动服务,使用a依次处理每个Intent   工作线程,并在失去工作时自行停止。

所以,我想问题是,onHandleIntent()完成后你的服务失效了。因此,服务自行停止并且通知被拒绝。因此,IntentService的概念可能不是您的任务的最佳案例。

问题的标题是“IntentService的StartForeground”,我想澄清一些事情:

让IntentService在前台运行非常简单(参见下面的代码),但是你肯定需要考虑以下几点:

  • 如果只需要几秒钟,就不要在前台运行服务 - 这可能会给用户带来麻烦。想象一下,你定期运行短期任务 - 这将导致通知出现和消失 - 呃嗯*

  • 您可能需要让您的服务能够保持设备清醒(但这是另一个故事,在stackoverflow上很好地介绍了)*

  • 如果您将多个Intent排队到IntentService,则以下代码最终会显示/隐藏通知。 (所以可能有更好的解决方案适用于你的情况 - 因为@CommonsWare建议扩展服务并自己做所有事情,但是想提一下 - 在javadoc中没有任何内容可以让IntentService说它只能工作几秒钟 - 它的工作时间与它必须做点什么。)

public class ForegroundService extends IntentService {

    private static final String TAG = "FrgrndSrv";

    public ForegroundService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        Notification.Builder builder = new Notification.Builder(getBaseContext())
                .setSmallIcon(R.drawable.ic_foreground_service)
                .setTicker("Your Ticker") // use something from something from R.string
                .setContentTitle("Your content title") // use something from something from
                .setContentText("Your content text") // use something from something from
                .setProgress(0, 0, true); // display indeterminate progress

        startForeground(1, builder.build());
        try {
            doIntesiveWork();
        } finally {
            stopForeground(true);
        }
    }

    protected void doIntesiveWork() {
        // Below should be your logic that takes lots of time
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}