首页 > Java > StartForeground_Service仍然不起作用

StartForeground_Service仍然不起作用

WBOY
发布: 2024-02-08 20:42:23
转载
507 人浏览过
问题内容

我正在开发蓝牙广告程序。它在 sdk 28 中编译。根据文档的要求,我添加了 android.permission.foreground 权限。但即使是现在,如果单击“蓝牙广告”按钮并调用 startforeground(),应用程序也会崩溃。

在我添加权限后,捕获从未达到。在添加权限之前,我仍然会到达捕获序列,并且消息告诉我,我必须添加上面提到的 foreground_service 权限。

对于这个应用程序,我使用 java。

`/**
     * Starts BLE Advertising.
     */
    private void startAdvertising() {
        goForeground();

    Log.d(TAG, "Service: Starting Advertising");

    if (mAdvertiseCallback == null) {
        AdvertiseSettings settings = buildAdvertiseSettings();
        AdvertiseData data = buildAdvertiseData();
        mAdvertiseCallback = new SampleAdvertiseCallback();

        if (mBluetoothLeAdvertiser != null) {
            mBluetoothLeAdvertiser.startAdvertising(settings, data,
                    mAdvertiseCallback);
        }
    }
}

/**
 * Move service to the foreground, to avoid execution limits on background processes.
 *
 * Callers should call stopForeground(true) when background work is complete.
 */
private void goForeground() {
    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);
    Notification n = new Notification.Builder(this)
            .setContentTitle("Advertising device via Bluetooth")
            .setContentText("This device is discoverable to others nearby.")
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(pendingIntent)
            .build();
    //TODO: startForeground throws Exception. Has to be Fixed
    try{
        startForeground(FOREGROUND_NOTIFICATION_ID, n);
    } catch (SecurityException e) {
        Toast toast = Toast.makeText(this, e.getMessage() + " Exception Throwed", Toast.LENGTH_SHORT);
        toast.getView().setBackgroundColor(Color.parseColor("#FF0FF0"));
        toast.show();
    }

}`
登录后复制

有人要求我提供堆栈跟踪,所以这就是:

致命异常:主要 进程:com.example.android.bluetoothadvertisements,pid:9760 android.app.remoteserviceexception$cannotpostforegroundservicenotificationexception:startforeground 的错误通知 在 android.app.activitythread.throwremoteserviceexception(activitythread.java:1983) 在 android.app.activitythread.-$$nest$mthrowremoteserviceexception(来源未知:0) 在 android.app.activitythread$h.handlemessage(activitythread.java:2242) 在 android.os.handler.dispatchmessage(handler.java:106) 在 android.os.looper.looponce(looper.java:201) 在 android.os.looper.loop(looper.java:288) 在 android.app.activitythread.main(activitythread.java:7898) 在 java.lang.reflect.method.invoke(本机方法) 在 com.android.internal.os.runtimeinit$methodandargscaller.run(runtimeinit.java:548) 在 com.android.internal.os.zygoteinit.main(zygoteinit.java:936)


正确答案


您的代码已过时:替换两行

pendingintent pendingintent = ...
notification n = ...
登录后复制

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
NotificationChannel notificationChannel = new NotificationChannel("your_Channel_ID", "your channel name", NotificationManager.IMPORTANCE_LOW);
getSystemService(NotificationManager.class).createNotificationChannel(notificationChannel);
Notification n = new Notification.Builder(this, "your_Channel_ID")
登录后复制

以上是StartForeground_Service仍然不起作用的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:stackoverflow.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板