使用一个服务处理两个活动

时间:2015-03-02 13:50:23

标签: android android-intent android-activity android-service

我有两个Activity和一个Service。我创建了一个service并在manifest中添加了操作。两个操作属于firstActivity,一个操作属于SecondActivity。两个action属于firstActivity次来电,但是当我从Service拨打相同的SecondActivity时。它甚至没有创造。

为什么会这样?

从第一个活动调用

Intent equalizerService = new Intent(context,
                        MyEqualizerService.class);

equalizerService.setAction(MyEqualizerService.ACTION_PLAY);

startService(equalizerService);

从第二个活动中调用

Intent equalizerService = new Intent(context,
                            MyEqualizerService.class);

    equalizerService.setAction(MyEqualizerService.ACTION_INIT_EQ);

    startService(equalizerService);

但它没有启动服务。我不知道为什么。

我已正确添加了像

这样的intent-filter
 <service
        android:name="com.zare.MyEqualizerService"
        android:enabled="true"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.zare.action.PLAY" />
            <action android:name="com.zare.action.PAUSE" />

            <action android:name="com.zare.action.NEXT" />
            <action android:name="com.zare.PREVIOUS" />
            <action android:name="com.zare.INITIALIZE_EQ"/>
        </intent-filter>
    </service>

和onStartCommand

 @Override
    public int onStartCommand(Intent intent, int flags, int startId) 
    {
        Log.v("meiService", "onStartCommand");

        if(intent != null && intent.getAction() != null) 
        {
            if(intent.getAction() == ACTION_PLAY)
            {
                if (null != mediaPlayer && !mediaPlayer.isPlaying()) 
                {
                    if(musiky!=null)
                    {
                        if(musiky.size() > 0 )
                        {
                            Music cMusic = musiky.get(currentSongIndex);

                            play(cMusic.getUrl());
                        }

                    }
                    else
                    {
                        Toast.makeText(getApplicationContext(), "No song to Play", Toast.LENGTH_LONG).show();
                    }
                }
            }

            else if(intent.getAction()==ACTION_INIT_EQ)
            {   
                if(isOurPlayerPlaying())
                {
                    Toast.makeText(getApplicationContext(), "I am  initialized", Toast.LENGTH_LONG).show();

                    initBassBoost(mediaPlayer.getAudioSessionId());

                    initEqualizer(mediaPlayer.getAudioSessionId());

                    initVirtualizer(mediaPlayer.getAudioSessionId());
                }
                else
                {
                    Toast.makeText(getApplicationContext(), "I am  initialized with 0 id", Toast.LENGTH_LONG).show();

                    initBassBoost(0);

                    initEqualizer(0);

                    initVirtualizer(0);
                }
            }
        }
        return START_STICKY; 
    }

0 个答案:

没有答案