0

I have two Activity and one Service.I have created a service and add actions to it in manifest. two actions belong to firstActivity and one action belongs to SecondActivity. Two action belongs to firstActivity calls but when i call the same Service from SecondActivity. It doesn't even create.

Why is it so?

Calling from first Activity

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

equalizerService.setAction(MyEqualizerService.ACTION_PLAY);

startService(equalizerService);

calling from Second Activity

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

    equalizerService.setAction(MyEqualizerService.ACTION_INIT_EQ);

    startService(equalizerService);

but it doesn't startService. I don't know why.

I have properly added intent-filter like

 <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>

And 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; 
    }
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
  • Simply create your intent this way: Intent equalizerService = new Intent("com.zare.action.PLAY"); – Christopher Mar 02 '15 at 13:53
  • How do you know it's not started? Maybe the `Service` is still running from the first invocation? `onCreate()` wouldn't be called in this case, `onStartCommand()` should though. Override `onStartCommand` in your `Service` and add some logging to see if it is called. – ci_ Mar 02 '15 at 13:53
  • I see Log . When i call it from FirstActivity. its onCreate,onStartCommand method call. but when i start the same Service from another activity. Even onStartCommand doesn't call. – Zar E Ahmer Mar 02 '15 at 13:58
  • does the constants used as Intent ACTION are correct – Vishal Santharam Mar 02 '15 at 14:12
  • post your constants, at least `MyEqualizerService.ACTION_INIT_EQ` – ci_ Mar 02 '15 at 14:14
  • public static final String ACTION_INITIALIZE_EQ = "com.zare.INITIALIZE_EQ"; , i have checked they are same @ci_ – Zar E Ahmer Mar 02 '15 at 14:35
  • @Nepster: Your string-comparison is wrong: if(intent.getAction() == ACTION_PLAY) When comparing strings use if (ACTION_PLAY.equals(intent.getAction()) – Christopher Mar 02 '15 at 15:27
  • but ACTION_PLAY is working fine. @Christopher. ACTION_INITIALIZE_EQ is not Working – Zar E Ahmer Mar 02 '15 at 16:41
  • intent.getAction() == ACTION_PLAY is definitely wrong! http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java What happens if your start your service this way: Intent intent = new Intent("com.zare.action.PLAY"); startService(intent); – Christopher Mar 03 '15 at 07:10
  • they why other action like next previous working on the same check . I mean =. – Zar E Ahmer Mar 03 '15 at 07:18
  • what does `play(cMusic.getUrl())` do? Does this method return quickly or does it actually play an entire song and then return? – David Wasser Mar 03 '15 at 20:54
  • @DavidWasser it just mediaPlayer.setDataSource(getApplicationContext(), Uri.parse(path)); mediaPlayer.prepare(); mediaPlayer.start(); – Zar E Ahmer Mar 04 '15 at 06:52
  • Please add logging just before the line `return START_STICKY;`. See if you get to that line after starting the media player. Also, are you absolutely sure that the second Activity is calling `startService()`? – David Wasser Mar 04 '15 at 17:44
  • Also, your code shows you using the constant `MyEqualizerService.ACTION_INIT_EQ`, but in your comments you've written `public static final String ACTION_INITIALIZE_EQ = "com.zare.INITIALIZE_EQ";` – David Wasser Mar 04 '15 at 17:47

0 Answers0