【问题标题】:How can I start the MusicPlaybackService from an external app?如何从外部应用程序启动 MusicPlaybackService?
【发布时间】:2012-04-20 16:33:06
【问题描述】:

我正在尝试通过位于 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);

我也尝试将 SERVICECMD 替换为 TOGGLEPAUSE_ACTION。

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) 此代码因以下错误而崩溃。我不确定如何使用此选项更进一步。

原因:android.content.ActivityNotFoundException:找不到明确的活动类 {com.google.android.music/com.google.android.music.MusicPlaybackService};您是否在 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);

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

编辑

我尝试使用以下引发错误的代码绑定到服务:

原因: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); 
}

在我的课堂上

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 android-intent android-music-player


    【解决方案1】:

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

    【讨论】:

    • 你找到解决方案了吗?
    猜你喜欢
    • 2019-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多