android - 如何从外部应用程序启动 MusicPlaybackService?

标签 android android-intent android-music-player

我正在尝试通过位于 Google Play 音乐应用程序顶部的服务和接收器来扩展有线麦克风远程按钮的功能,并通过广播与其进行通信。我使用 Google Nexus S 使用 ICS。

我遇到的问题是,一旦 MusicPlaybackService 运行,我的代码就可以正常工作,但情况并非总是如此。如何在不显示任何 UI 的情况下启动此服务(用户不知道)。我的代码有以下定义:

public static final String SERVICECMD = "com.android.music.musicservicecommand";
public static final String CMDNAME = "command";
public static final String CMDTOGGLEPAUSE = "togglepause";
public static final String TOGGLEPAUSE_ACTION = "com.android.music.musicservicecommand.togglepause";

我尝试了以下选项:

1) 基于this SO question 。我不确定是否关于 SERVICECMD,因为问题指出我应该使用“com.android.music.musicservicecommand”,但对于 ICS,我相信包名称已更改。

Intent i = new Intent(SERVICECMD); 
i.putExtra(CMDNAME, CMDTOGGLEPAUSE); 
sendBroadcast(i);

我还尝试用 TOGGLEPAUSE_ACTION 替换 SERVICECMD。

2) 来自this android项目问题。

Intent musicIntent = new Intent();
musicIntent.setClassName("com.google.android.music", "com.google.android.music.MusicPlaybackService");
musicIntent.setAction(TOGGLEPAUSE_ACTION);
musicIntent.putExtra(CMDNAME, CMDTOGGLEPAUSE); // or "next" or "previous"
sendBroadcast(musicIntent);

3) 此代码崩溃并出现以下错误。我不确定如何进一步使用此选项。

Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.google.android.music/com.google.android.music.MusicPlaybackService}; have you declared this activity in your AndroidManifest.xml?

Intent intent = new Intent();
intent.putExtra(CMDNAME, CMDTOGGLEPAUSE); 
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClassName("com.google.android.music", "com.google.android.music.MusicPlaybackService");
startActivity(intent);

4) 我注意到,当耳机从手机上取下时,MusicPlaybackService 就会启动,因为它正在响应由 WiredAccessoryObserver 拾取的 Intent.ACTION_HEADSET_PLUG 广播。我尝试使用 LogCat 中显示的值在代码中复制该广播。

Intent intent = new Intent(Intent.ACTION_HEADSET_PLUG);
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
intent.putExtra("state", 0);
intent.putExtra("name", "h2w");
intent.putExtra("microphone", 1);
sendBroadcast(intent);

我真的在寻找一种在启动服务时始终有效的方法。我对任何事情都持开放态度。我仍然必须尝试绑定(bind)到使用 IMediaPlaybackService.aidl 的服务,该服务应该随机中断,并且并不理想。预先感谢您的帮助。

编辑

我尝试使用下面的代码绑定(bind)到服务,但抛出了错误:

Caused by: java.lang.SecurityException: Not allowed to bind to service Intent { cmp=com.google.android.music/.MusicPlaybackService }

在我的 OnCreate 函数中

if (mPlaybackService == null) { 
    Intent i = new Intent(); 
    i.setClassName("com.google.android.music","com.google.android.music.MusicPlaybackService" ); 
    bindService(i, connection, Context.BIND_AUTO_CREATE); 
}

在我的类(class)

IMediaPlaybackService mPlaybackService = null;
ServiceConnection connection = new ServiceConnection() {
    public void onServiceConnected(ComponentName name, IBinder service) {     
        mPlaybackService = IMediaPlaybackService.Stub.asInterface(service);
}

    public void onServiceDisconnected(ComponentName name) { 
         mPlaybackService = null; 
    } 
};

最佳答案

我认为这是不可能的。我的解决方法是让应用程序自己的广播接收器在按下按钮时唤醒服务。我只是为我的应用程序请求音频焦点,然后等待它丢失。

关于android - 如何从外部应用程序启动 MusicPlaybackService?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10250037/

相关文章:

java - 当它获得焦点时选择 EditText 内的所有文本而不滚动到末尾

android - 在 Kotlin 中打开另一个 fragment

android - 找不到处理 Intent 的 Activity { act=android.intent.action.EDIT typ=vnd.android.cursor.item/event

java - 创建一个 Intent 过滤器供应用程序内部使用

android - 广播接收器不影响 Android 4.0(可能是 3.1+)中的共享首选项

android - 无法解析符号环境

java - WebView Android应用——如何读取网站内容

android - Mediaplayer 串流在线 MP3 文件

android - 从 Android AppWidget 播放/控制歌曲

android - Google Play 音乐应用的歌曲轨道信息