安卓活动也要求应用程序的其他服务,这不叫 [英] Android: Activity also calling other Service of the application, which is not called

查看:196
本文介绍了安卓活动也要求应用程序的其他服务,这不叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个Android应用程序基本上包含2 媒体播放器。其中,从手机存储兼播放音乐;其他的可以播放从互联网流(网址)。

I creating an android app which basically contains 2 media players. One that plays music from phone memory & other that plays streams from internet(URLs).

每个媒体播放器在其独立的活动定义。我使用服务来播放。 (即当用户presses播放按钮,服务得到启动和放大器;播放媒体)

Each media player is defined in its separate activity. I'm using Service to playback. (i.e. when the user presses the play button, a Service gets started & plays the media).

该MusicPlayer是应用程序和放大器的默认的活动;在包 com.example.musicplayer 。然而,码流播放器是在不同的包 com.example.streamplayer

The MusicPlayer is the default activity of the application & is in the package com.example.musicplayer. Whereas, the stream player is in the different package com.example.streamplayer.

问题::当应用程序启动和放大器;我选择视频流播放器和放大器; preSS播放按钮,虽然URL流被打了,但我也注意到, onStartCommand 其他媒体播放器的,即音乐播放器也被调用。

Issue: When the application starts & I select the stream player & press the play button, although the URL stream gets played but I've also noticed that onStartCommand of other mediaplayer i.e. the music player also gets called.

这意味着活动B正在调用服务A,即使双方都在不同的包。

This means that Activity B is calling Service A, even if both are in different packages.

code为音乐播放器活性放大器;服务

Code for Music player activity & service

package com.example.musicplayer;

public class MusicPlayerActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        ....
        Intent service = new Intent(this, MusicService.class);
        startService(service);
        ....
    }

}

package com.example.musicplayer;

public class MusicService extends Service {

    ....

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        Toast.makeText(this, "Music Service onStartCommand()", Toast.LENGTH_LONG).show();       // This gets displayed even in the Stream Activty

        return START_STICKY;
    }
    ....

}

code为流活动和放大器;服务:

Code for stream activity & service:

package com.example.streamplayer;

public class StreamPlayer extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ....
        Intent service = new Intent(this, StreamService.class);
        service.putExtra("URL", url);
        startService(service);
        ....
    }
}

package com.example.streamplayer;

public class StreamService extends Service {

    protected void startStream() {
        MediaPlayer mPlayer = new MediaPlayer();
        mPlayer.reset();
        mPlayer.setDataSource(url);
        mPlayer.prepareAsync();
        mPlayer.setOnPreparedListener(new OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();

            }
        });
    }
}

的Manifest.xml

Manifest.xml

注意:清单文件我也尝试添加到指定的服务是它的意图。 (不知道它的工作原理是这样只)

Note: in manifest file I've also tried adding to specify which service is for which intent. (Not sure if it works like this only)

<service
    android:name=".MusicService"
    android:enabled="true" >
    <intent-filter>
        <action android:name="com.example.musicplayer.MusicPlayerActivity" />
    </intent-filter>
</service>

<service
    android:name="com.example.streamplayer.StreamService"
    android:enabled="true" >
    <intent-filter>
        <action android:name="com.example.streamplayer.StreamService" />
    </intent-filter>
</service>

请建议如何确保错误的服务应该不会被调用?

Please suggest how to make sure that the wrong service should not get called?

prequel这个问题:这里

Prequel to this problem: here

感谢您

推荐答案

您清单只显示一个服务,即使你已经定义了两个,你把你定义的服务的动作是没有意义的(它不 ŧ因为你同时发送意图是明确的匹配您在code定义),将被忽略的任何行动。

Your manifest shows only one service, even though you've defined two, and the action you put in for the service you defined makes no sense (it doesn't match any action you've defined in code) and would be ignored since both Intents you're sending are explicit.

你有没有在你的清单中定义两种服务?

Do you have both services defined in your manifest?

这篇关于安卓活动也要求应用程序的其他服务,这不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆