启动服务抛出 IllegalAccessException

发布于 2024-12-18 04:43:34 字数 2224 浏览 1 评论 0原文

我有一个 Service ,它接收音频文件并使用 MediaPlayer 播放它。这就是我调用 Service 的方式:

private void playAudio(String url) throws Exception {
    Intent music = new Intent(this,MusicService.class);
    music.putExtra("paths", path);
    startService(music);
}

这是我的 Service 类:

class MusicService extends Service implements OnCompletionListener {
    MediaPlayer mediaPlayer;
    String musicFile;

    @Override
    public void onCreate() {
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setOnCompletionListener(this);
        Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_LONG).show();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Bundle e = intent.getExtras();
        musicFile= e.getString("paths"); 
        try {
            mediaPlayer.prepare(); 
            mediaPlayer.setDataSource(musicFile);
        } catch (IllegalArgumentException i) {
            // TODO Auto-generated catch block
            i.printStackTrace();
        } catch (IllegalStateException i) {
            // TODO Auto-generated catch block
            i.printStackTrace();
        } catch (IOException i) {
            // TODO Auto-generated catch block
            i.printStackTrace();
        }
        if (!mediaPlayer.isPlaying()) {             
            mediaPlayer.start();             
        } 
        return START_STICKY; 
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        // TODO Auto-generated method stub
    }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }
}

Service 永远不会被执行,Toast > 永远不会显示,并且 MediaPlayer 不会播放。

我在清单中声明它,如下所示:

<service android:name=".MusicService" android:enabled="true"></service>

我收到强制关闭错误,并且在我的日志中出现此 IllegalAccessException

java.lang.RuntimeException: Unable to instantiate service unjustentertainment.com.MusicService:
    java.lang.IllegalAccessException: access to class not allowed

I have a Service which takes in an audio file and plays it with MediaPlayer. This is how I call my Service:

private void playAudio(String url) throws Exception {
    Intent music = new Intent(this,MusicService.class);
    music.putExtra("paths", path);
    startService(music);
}

This is my Service class:

class MusicService extends Service implements OnCompletionListener {
    MediaPlayer mediaPlayer;
    String musicFile;

    @Override
    public void onCreate() {
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setOnCompletionListener(this);
        Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_LONG).show();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Bundle e = intent.getExtras();
        musicFile= e.getString("paths"); 
        try {
            mediaPlayer.prepare(); 
            mediaPlayer.setDataSource(musicFile);
        } catch (IllegalArgumentException i) {
            // TODO Auto-generated catch block
            i.printStackTrace();
        } catch (IllegalStateException i) {
            // TODO Auto-generated catch block
            i.printStackTrace();
        } catch (IOException i) {
            // TODO Auto-generated catch block
            i.printStackTrace();
        }
        if (!mediaPlayer.isPlaying()) {             
            mediaPlayer.start();             
        } 
        return START_STICKY; 
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        // TODO Auto-generated method stub
    }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }
}

The Service is never getting executed, the Toast is never shown, and the MediaPlayer does not play.

I declare my it in my manifest like this:

<service android:name=".MusicService" android:enabled="true"></service>

I get a force close error, and this IllegalAccessException in my logs:

java.lang.RuntimeException: Unable to instantiate service unjustentertainment.com.MusicService:
    java.lang.IllegalAccessException: access to class not allowed

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情场扛把子 2024-12-25 04:43:34

您得到的异常是因为系统无法初始化您的服务(调用其构造函数),因为它不可访问。

正如此处所述:

...确保该类是
已公开...

您发布的课程不是公开的,因此请将其公开。

The exception you get is because the system could not init your service (call its constructor) because its not accessible.

As it says here:

...Make sure the class is
declared public...

The class you posted is not public, so make it public.

往日情怀 2024-12-25 04:43:34

您需要公开您的课程。
IE

 public class MusicService extends Service implements OnCompletionListener {
          MediaPlayer mediaPlayer;
           String musicFile;

You need to make your class public.
i.e.

 public class MusicService extends Service implements OnCompletionListener {
          MediaPlayer mediaPlayer;
           String musicFile;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文