小编tra*_*ary的帖子

startForegroundService()没有调用startForeground(),但确实如此

我有Context.startForegroundService() did not then call Service.startForeground()我的Android服务,但我无法弄清楚它为什么会发生.

我的应用程序用于媒体流,只有当您从前台通知暂停(将其转换为常规通知)时才会出现此错误,然后您将通知刷掉,这是为了停止我的服务.

这里就是唯一的方法startForegroundService,startForegroundstopForeground方法被称为:

private void configureServiceState(long action) {
        if (action == PlaybackStateCompat.ACTION_PLAY) {
            if (!mServiceInStartedState) {
                ContextCompat.startForegroundService(
                        StreamingService.this,
                        new Intent(
                                StreamingService.this,
                                StreamingService.class));
                mServiceInStartedState = true;
            } startForeground(NOTIFICATION_ID,
                    buildNotification(PlaybackStateCompat.ACTION_PAUSE));
        } else if (action == PlaybackStateCompat.ACTION_PAUSE) {
            stopForeground(false);

            NotificationManager mNotificationManager
                    = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            assert mNotificationManager != null;
            mNotificationManager
                    .notify(NOTIFICATION_ID,
                            buildNotification(PlaybackStateCompat.ACTION_PLAY));
        } else if (action == PlaybackStateCompat.ACTION_STOP) {
            mServiceInStartedState = false;
            stopForeground(true);
            stopSelf();
        }
    }
Run Code Online (Sandbox Code Playgroud)

这里是我的通知的删除意图设置的地方:

.setDeleteIntent(
                        MediaButtonReceiver.buildMediaButtonPendingIntent(
                                this, PlaybackStateCompat.ACTION_STOP)); …
Run Code Online (Sandbox Code Playgroud)

java android android-notifications foreground-service remoteserviceexception

10
推荐指数
2
解决办法
5197
查看次数

精确检测正方形和圆形重叠的算法?

我正在实现(在C++中)一种方法来检测在2d平面上两个静态的轴对齐形状之间何时发生重叠.形状是正方形或圆形,因此有三种情况我需要考虑重叠:方形,圆形和圆形方形.

方形和圆形也很简单,但我很难在网上找到关于计算方形圆重叠的正确算法的任何可靠信息.

我知道我可以将方块嵌入圆圈内(反之亦然)作为一种粗略的方法,但我对于更精确地做到这一点的最干净的方法感兴趣?

在线研究表明这个问题有一个"正确"的答案,但尚不清楚答案究竟是什么.

c++ algorithm geometry collision-detection

5
推荐指数
1
解决办法
139
查看次数

如何使用交互式通知为Web广播流创建Android前台服务?

我正在尝试构建一个非常简单的无线电流应用程序,它存储一个网络无线电URL列表,可以选择流式传输音频; 使用服务允许在应用程序未激活时继续播放+来自通知的控制.

我需要的控件非常简单:播放/暂停和停止,这应该会终止服务并在清除通知或在应用程序中按下停止按钮时引发.

我为大量代码道歉,但这就是我所处的位置:

public class StreamingService extends Service
        implements MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener {

    // .. snipped out fields

    private AudioManager.OnAudioFocusChangeListener mOnAudioFocusChangeListener =
            new AudioManager.OnAudioFocusChangeListener() {
                @Override
                public void onAudioFocusChange(int focusChange) {
                    switch (focusChange) {
                        case AudioManager.AUDIOFOCUS_GAIN:
                            // set mCurrentAudioFocusState field
                    }

                    if (mMediaPlayer != null)
                        configurePlayerState();
                }
            };

    private int mCurrentAudioFocusState = AUDIO_NO_FOCUS_NO_DUCK;

    private final IntentFilter mAudioNoisyIntentFilter =
            new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY);

    private BroadcastReceiver mNoisyReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // Pause when headphones unplugged …
Run Code Online (Sandbox Code Playgroud)

java android android-service android-notifications android-mediaplayer

5
推荐指数
1
解决办法
1256
查看次数

在单独的控制器功能中使用 Multer 文件上传

我正在编写一个 Express 应用程序,为了避免我的routes.js文件混乱,我创建了一个单独的,UploadController如下所示:

// UploadController.js
const multer = require('multer')
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'public/' + file.fieldname + '/')
  },
  filename: function (req, file, cb) {
    cb(null, Date.now() + '-' + file.originalname)
  }
})

const upload = multer({
  storage: storage
})

module.exports = {
  upload
}
Run Code Online (Sandbox Code Playgroud)

routes像这样使用,具体来说这只是我的注册路线作为示例:

app.post('/register',
    RegistrationPolicy.validate,
    UploadController.upload.single('avatar'),
    UserController.register)
Run Code Online (Sandbox Code Playgroud)

这工作得很好。理想情况下,我需要multipart/form-data在上传运行之前检查请求类型是否为,然后在上传完成后运行:

req.body.avatarUri = req.file.destination + req.file.filename
Run Code Online (Sandbox Code Playgroud)

以便在UserController.register运行时上传文件的URI将存储在我的数据库中。

我能想到的最简洁的upload方法是在我的控制器中创建一个自定义方法,该方法执行检查、上传操作,然后是 URI 分配。问题是,将 multer …

javascript node.js express multer

3
推荐指数
1
解决办法
5217
查看次数

MediaBrowser 未连接到 MediaBrowserService

我正在尝试使用 MediaBrowser 从活动连接到 MediaBrowserService。但是,我的onConnected回调似乎从未被调用以允许我设置MediaController并开始操作播放。

在我的播放器/浏览器活动中:

private final MediaBrowserCompat.ConnectionCallback mConnectionCallback =
            new MediaBrowserCompat.ConnectionCallback() {
                @Override
                public void onConnected() {
                    try {
                        mMediaController =
                                new MediaControllerCompat(
                                        PlayActivity.this,
                                        mMediaBrowser.getSessionToken()
                                );

                        mMediaController.registerCallback(mControllerCallback);

                        mControllerCallback
                                .onMetadataChanged(mMediaController.getMetadata());
                        mControllerCallback
                                .onPlaybackStateChanged(mMediaController.getPlaybackState());
                    } catch (RemoteException e) {
                        throw new RuntimeException(e);
                    }

                    mMediaController
                            .getTransportControls()
                            .prepareFromUri(
                                    Uri.parse(
                                            getIntent()
                                                    .getStringExtra(StreamingService.STREAM_URI)),
                                    getIntent().getExtras());

                    mMediaController
                            .getTransportControls().play();

                    buildTransportControls();
                }

                @Override
                public void onConnectionSuspended() { }

                @Override
                public void onConnectionFailed() { }
            };

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play);
        setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true); …
Run Code Online (Sandbox Code Playgroud)

android android-activity foreground-service mediabrowserservice mediabrowser

2
推荐指数
1
解决办法
1372
查看次数