首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android前台服务

Android前台服务
EN

Stack Overflow用户
提问于 2019-03-09 13:32:59
回答 2查看 652关注 0票数 0

我有一个用来调用的服务

代码语言:javascript
运行
复制
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    getActivity().startForegroundService(new Intent(getActivity(), 
Background.class));
} else {
    getActivity().startService(new Intent(getActivity(), Background.class));
}

以及它自身所提供的服务

代码语言:javascript
运行
复制
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this,"Creating Notification",Toast.LENGTH_SHORT).show();

    //
    initChannels(this);

    Intent notificationIntent = new Intent(this, MainActivity.class);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);

    Notification notification = new NotificationCompat.Builder(this, "default")
            .setContentTitle("Zeep!?")
            .setTicker("Zeep!?")
            .setContentText("We're currently working in the background")
            .setSmallIcon(R.mipmap.zeep_icon_b)
            .setOngoing(true)
            .setPriority(Notification.PRIORITY_MIN)
            .setContentIntent(pendingIntent)
            .build();

    startForeground(1337, notification);
    //

    return START_NOT_STICKY;
}

但每当我启动应用程序和关闭应用程序时,它都会崩溃,并导致手机软重启,我对此感到非常困惑,谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-03-09 16:56:52

我的onStartCommand()看起来像这样:

代码语言:javascript
运行
复制
     @Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // Tells the system to not try to recreate the service after it has been killed.
    return START_NOT_STICKY;
}

相反,我负责onCreate()中的通知内容。此外,您需要在调用startForegroundService()之后立即调用startForeground():

代码语言:javascript
运行
复制
     @Override
public void onCreate() {
    mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    // Android O requires a Notification Channel.
    if (Build.VERSION.SDK_INT >= 26) {
        CharSequence name = getString(R.string.app_name);
        // Create the channel for the notification
        @SuppressLint("WrongConstant")
        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_LOW);
        // Set the Notification Channel for the Notification Manager.
        if (notificationManager != null) {
            notificationManager.createNotificationChannel(mChannel);
        }

        //Since MainActivity binds with the service and calls onCreate, we can actually call startForegroundService from within the service itself.
        startForegroundService(new Intent(ForegroundService.this, ForegroundService.class));
        //We only need to call this for SDK 26+, since startForeground always has to be called after startForegroundService.
        startForeground(NOTIFICATION_ID, getNotification());
    }
    else {
        //Since MainActivity binds with the service and calls onCreate, we can actually call startService from within the service itself.
        startService(new Intent(ForegroundService.this, ForegroundService.class));
    }

并不是说这就是解决方案,但它对我来说是有效的。

票数 0
EN

Stack Overflow用户

发布于 2019-03-10 11:22:29

代码语言:javascript
运行
复制
START_NOT_STICKY

如果系统在onStartCommand()返回后终止了服务,请不要重新创建服务,除非有挂起的意图要交付。这是最安全的选择,可以避免在不必要的情况下运行您的服务,并且当您的应用程序可以简单地重新启动任何未完成的作业时。

代码语言:javascript
运行
复制
START_STICKY

如果系统在onStartCommand()返回后终止了服务,请重新创建服务并调用onStartCommand(),但不要重新传递最后的意图。相反,系统会使用null意图调用onStartCommand(),除非有未完成的意图来启动服务。在这种情况下,这些意图就被传递了。这适用于不执行命令但正在无限期运行并等待作业的媒体播放器(或类似服务)。

代码语言:javascript
运行
复制
START_REDELIVER_INTENT

如果系统在onStartCommand()返回后终止了该服务,请重新创建该服务并使用传递给该服务的最后意图调用onStartCommand()。任何挂起的意图都会依次递送。这适用于正在主动执行应立即恢复的作业的服务,例如下载文件。

您可以使用START_NOT_STICKY,但是您必须手动处理服务的停止。还要记住,当您从活动中调用服务时,onCreate()并不总是被调用。只有当您从非活动调用服务时,它才会被调用,否则就会调用onStartCommand()

我认为这个库有最好的android服务实现。请查看MockGeoFix

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55074333

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档