【问题标题】:Start Service from BroadcastReceiver never call service.onStartCommand() method从 BroadcastReceiver 启动服务从不调用 service.onStartCommand() 方法
【发布时间】:2015-04-01 12:11:43
【问题描述】:

我有播放音频文件的音乐服务。我从我的活动开始这项服务。当我从通知中单击播放按钮时,我发送带有播放/暂停按钮的通知,我发送广播接收器并且它正在工作。在 onReceive(Context context, Intent intent) 方法中我调用 context.startService(intentService) 问题是 MusicService.onStartCommand() 从未调用过。

我的接收器

public class PlayEpisodeReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Service Started", Toast.LENGTH_SHORT).show();
        if(MusicService.isRunning()) {

            Intent serviceIntent = new Intent(context, MusicService.class);

            serviceIntent.putExtra(AppConstants.ACTION_TYPE, AppConstants.ACTION_PLAY);

            context.startService(intent);
        }
    }

} 

我的清单:

<service
            android:enabled="true"
            android:name=".backgroundtasks.MusicService">
            <intent-filter>
                <action
                    android:name = "cc.gm.oscarradio.backgroundtasks.MusicService">
                </action>
            </intent-filter>
        </service>
        <receiver
            android:enabled="true"
            android:name=".Receivers.PlayEpisodeReceiver">
            <intent-filter>
                <action android:name = "cc.gm.oscarradio.ACTION_MUSIC"/>
            </intent-filter>
        </receiver>

【问题讨论】:

    标签: android service broadcastreceiver


    【解决方案1】:

    我觉得你的代码应该有细微的变化,你应该替换以下行:

    context.startService(intent);
    

    通过

    context.startService(serviceIntent);
    

    希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 2011-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多