- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试通过位于 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);
}
在我的类里面
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;
}
};
最佳答案
我认为这是不可能的。我的解决方法是让应用程序自己的广播接收器在按下按钮时唤醒服务。我只是为我的应用程序请求 audiofocus 并等待它丢失。
关于android - 如何从外部应用启动 MusicPlaybackService?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10250037/
我正在尝试通过位于 Google Play 音乐应用程序之上并通过广播与其通信的服务和接收器来扩展有线麦克风遥控按钮的功能。我使用 Google Nexus S 使用 ICS。 我遇到的问题是,一旦
我是一名优秀的程序员,十分优秀!