我看过很多关于从服务向 Activity 发送消息的帖子,但我就是无法让它发挥作用。我的接收器永远不会被调用。 Activity 不在前台有关系吗?
这是我的场景 - 我将网络服务器作为服务运行。当 Web 服务器收到带有播放列表链接的 URL 时,我需要向充当媒体播放器的 Activity 发送一条消息。媒体播放器将读取播放列表并开始按顺序播放歌曲、视频等。
我不希望该服务启动媒体播放器 Activity 的新实例,因为有时它可能会发送快进或暂停等命令。
来 self 的网络服务器服务:
private void sendMessage() {
Log.d("juice NonoHTTPD sender", "Sending message from Hub Web Server to Hub Player");
Intent intent = new Intent();
intent.putExtra(HubPlayer.ACTIVITY_PARAM_HOST, sAppHost);
intent.putExtra(HubPlayer.ACTIVITY_PARAM_PORT, sAppPort);
intent.putExtra(HubPlayer.ACTIVITY_PARAM_TYPE, sAppType);
intent.putExtra(HubPlayer.ACTIVITY_PARAM_URL, sAppPath);
intent.setAction("com.jigawattlabs.hubplayer.play");
appContext.sendBroadcast(intent);
}
来 self 的 Activity :
public class HubPlayer extends Activity implements
OnBufferingUpdateListener, OnCompletionListener,
OnPreparedListener, OnVideoSizeChangedListener, SurfaceHolder.Callback
{
private IntentFilter intentFilter = new IntentFilter("com.jigawattlabs.hubplayer.play");
MyBroadcastReceiver mReceiver = new MyBroadcastReceiver();
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.mediaplayer_2);
registerReceiver(mReceiver , intentFilter);
}
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
extras = intent.getExtras();
sHost = extras.getString(ACTIVITY_PARAM_HOST);
sPort = extras.getString(ACTIVITY_PARAM_PORT);
sType = extras.getString(ACTIVITY_PARAM_TYPE);
sXMLURL = extras.getString(ACTIVITY_PARAM_URL);
DebugMsg("received broadcast in MyBroadcastReceiver.");
processInputRequest();
}
}
}
尽管似乎没有必要将它放在 list 中,因为我在我的代码中注册了 BroadcastReceiver,但我还添加了一个 Intent 过滤器:
<activity android:label="Media Hub Player" android:launchMode="singleTask"
android:screenOrientation="unspecified"
android:name=".HubPlayer" >
<intent-filter>
<action android:name="com.jigawattlabs.hubplayer.play" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
提前致谢。
最佳答案
好的,我有一个解决方法,但我希望有更好的方法。这是我做的
- 如果服务需要向 Activity 发送消息,它会检查 Activity 是否正在运行
- 如果 Activity 正在运行,它会执行上述的 sendBroadcast
- 如果 Activity 没有运行,我会执行一个 StartActivity 并向其传递与使用 sendBroadcast 时相同的信息
- 在 Activity 中,我从已注册的 BroadcastReceiver 或 onResume 上传递的 Intent (如果调用了 StartActivity)中获取 Intent 。
这是检查 Activity 是否正在运行的代码
public boolean isHubRunning()
{
boolean isServiceFound = false;
ActivityManager activityManager = (ActivityManager)appContext.getSystemService (Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> services = activityManager.getRunningTasks(Integer.MAX_VALUE);
isServiceFound = false;
for (int i = 0; i < services.size(); i++)
{
//DebugMsg(services.get(i).topActivity.toString());
if (services.get(i).topActivity.toString().contains("juicemediahub.HubPlayer"))
{
isServiceFound = true;
}
}
DebugMsg("Hub Player Running = " + String.valueOf(isServiceFound));
return isServiceFound;
}
关于android - 没有收到从我的服务到我的 Activity 的广播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10726330/