Error Android app when change targetSDKVersion from 19 to 26

344 Views Asked by At

can anyone help my problem?

My previous android application used targetSdkVersion: 19 then I changed to 26, but there was a problem like the following.

This LogCat

This is the snippet of the MusicManager.java code

  private static Intent musicIntent = new Intent("onet.BGMUSIC");

public void play() {
    if (isBgMisicEnabled()) {
        musicIntent.putExtra("bgmusic", bgMusicRes);
        ctx.startService(musicIntent);
    }
}

public void stop() {
    ctx.stopService(musicIntent);
}

This is the snippet of the BaseActivity.java code

    public static MusicManager musicMgr = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initMusic();
}

private void initMusic() {
    if (musicMgr == null) {
        musicMgr = new MusicManager(this);
    }
    playMusic();
}

protected void playMusic() {
    if (musicMgr != null) {
        musicMgr.play();
    }
}

protected void stopMusic() {
    if (musicMgr != null) {
        musicMgr.stop();
    }
}

This is the snippet of the WelcomeActivity.java code

    @Override
protected void playMusic() {
    if (musicMgr != null) {
        musicMgr.setBgMusicRes(R.raw.welcomebg);
        musicMgr.play();
    }
}


private void initMusicSetting() {
    ivMusic = (TextView) findViewById(R.id.music);
    setGameMusic();
    if (musicMgr != null) {
        musicMgr.setBgMisicEnabled(LevelCfg.globalCfg.isGameBgMusic());
    }
}

This is the snippet of the Manifest.xml code

        <service
        android:name="onet.sound.MusicService"
        android:exported="false" >
        <intent-filter>
            <action android:name="onet.BGMUSIC" />

            <category android:name="android.intent.category.default" />
        </intent-filter>
    </service>
2

There are 2 best solutions below

4
On

I believe you must set the package beforehand like :

if (isBgMisicEnabled()) {
    musicIntent.setPackage("onet")
    musicIntent.putExtra("bgmusic", bgMusicRes);
    ctx.startService(musicIntent);
}

or create the intent like:

Intent musicIntent= new Intent(ctx, MusicService.class);

as illustrated here: Service Intent must be explicit: Intent

0
On

Use

new Intent(this, BGMUSIC.class)

instead of

new Intent("onet.BGMUSIC")

Implicit intents are not preferred in the latest versions because of security risk. Hence you should be using class names

Intent serviceIntent = new Intent(context,MyService.class); context.startService(serviceIntent);