-1

Hello i have a Problem with my Code.I want to play the Music in the Background and this will function with the IntentService of Android. But my SDk give me a Error if I implement the line.

Radio.java

public class Radio extends Activity implements OnCompletionListener,
OnPreparedListener, OnErrorListener, OnBufferingUpdateListener, MusicFocusable, ViewFactory {

....

play.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View btn) {
            if (!playState) {
                play.setImageResource(R.drawable.stop);
                handler.postDelayed(handlePlayRequest, 300);

                 startService(new Intent(this, MyService.class));
                "Here is Error 1"

            }
            else {
                play.setImageResource(R.drawable.play);
                status.setText("Drücke Play!");
                handler.postDelayed(handlePlayRequest, 300);

                stopService(new Intent(this, MyService.class));
                 "Here is Error 2"

            }
        }

and

MyService.java

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {
private static final String TAG = "MyService";
MediaPlayer player;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onCreate");

}

@Override
public void onDestroy() {
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onDestroy");
    player.stop();
}

@Override
public void onStart(Intent intent, int startid) {
    Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onStart");
    player.start();
}
}

if you need more code just say it!

Ravi
  • 34,851
  • 21
  • 122
  • 183
etf3k
  • 1

1 Answers1

6

The error is because you are using this in an onClickListener, and you have to use ClassName.this in there. According to this answer, it is because:

You are creating an anonymous inner class when you use this part of your code: new OnClickListener() {

Which means that the class you are referencing is no longer this, so to access/identify the class you have to use the class name. So you should use this:

play.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View btn) {
        if (!playState) {
            play.setImageResource(R.drawable.stop);
            handler.postDelayed(handlePlayRequest, 300);

             startService(new Intent(Radio.this, MyService.class));
        }
        else {
            play.setImageResource(R.drawable.play);
            status.setText("Drücke Play!");
            handler.postDelayed(handlePlayRequest, 300);

            stopService(new Intent(Radio.this, MyService.class));
        }
    }
}

Also, you could use btn.getContext() instead of the Classname.this to make the code easier to copy to other classes. That would look like this:

startService(new Intent(btn.getContext(), MyService.class));
Community
  • 1
  • 1
hichris123
  • 10,145
  • 15
  • 56
  • 70