【问题标题】:Error on the Android Intent ServiceAndroid Intent 服务上的错误
【发布时间】:2016-08-05 12:17:13
【问题描述】:

您好,我的代码有问题。我想在后台播放音乐,这将与 Android 的 IntentService 一起使用。 但是如果我实现该行,我的 SDk 会给我一个错误。

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"

            }
        }

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();
}
}

如果您需要更多代码,请直接说出来!

【问题讨论】:

  • 发布错误和相关的logcats以启动

标签: android service intentservice


【解决方案1】:

错误是因为您在onClickListener 中使用了this,而您必须在其中使用ClassName.this。根据this的回答,是因为:

当您使用时,您正在创建一个匿名内部类 这部分代码:new OnClickListener() {

这意味着你引用的类不再是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));
        }
    }
}

此外,您可以使用btn.getContext() 代替Classname.this 以使代码更容易复制到其他类。看起来像这样:

startService(new Intent(btn.getContext(), MyService.class));

【讨论】:

    猜你喜欢
    • 2015-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多