用户从inAppBrowser返回时检测到离子3

时间:2017-07-18 00:19:26

标签: angular ionic3 ionic-native

我有一个有角度的离子3应用程序,并使用inAppBrowser插件从我的应用程序中的离子按钮启动链接。

我想更改按钮的标题,但只有在用户重新返回应用程序时才会更改。

目前的代码如下:
HTML:

public class MusicService extends Service implements MediaPlayer.OnPreparedListener,
MediaPlayer.OnErrorListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnInfoListener,
AudioManager.OnAudioFocusChangeListener {

private MediaPlayer player;
private List<Song> songs;
private int songIndex;
private final IBinder musicBind = new MusicBinder();
private AudioManager audioManager;

public void onCreate() {
    // Create the service
    super.onCreate();
    songIndex = 0;
    player = new MediaPlayer();

    initMusicPlayer();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    String mediaFile = "";
    try {
        //An audio file is passed to the service through putExtra();
        mediaFile = songs.get(songIndex).uri;
    } catch (NullPointerException e) {
        stopSelf();
    }

    //Request audio focus
    if (requestAudioFocus() == false) {
        //Could not gain focus
        stopSelf();
    }

    if (mediaFile != null && mediaFile != "")
        initMusicPlayer();
    return super.onStartCommand(intent, flags, START_STICKY);
}

public void initMusicPlayer() {
    // Set player properties
    player.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
    player.setAudioStreamType(AudioManager.STREAM_MUSIC);
    player.setOnPreparedListener(this);
    player.setOnCompletionListener(this);
    player.setOnErrorListener(this);
}
public void setSong(int songIndex) {
    this.songIndex = songIndex;
}

public void setList(List<Song> songs) {
    this.songs = songs;
}

public class MusicBinder extends Binder {
    MusicService getService() {
        return MusicService.this;
    }
}

@Override
public boolean onInfo(MediaPlayer mediaPlayer, int i, int i1) {
    return false;
}

@Override
public void onAudioFocusChange(int focusState) {
    switch (focusState) {
        case AudioManager.AUDIOFOCUS_GAIN:
            // resume playback
            if (player == null) initMusicPlayer();
            else if (!player.isPlaying()) player.start();
            player.setVolume(1.0f, 1.0f);
            break;
        case AudioManager.AUDIOFOCUS_LOSS:
            // Lost focus for an unbounded amount of time: stop playback and release media player
            if (player.isPlaying()) player.stop();
            player.release();
            player = null;
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
            // Lost focus for a short time, but we have to stop
            // playback. We don't release the media player because playback
            // is likely to resume
            if (player.isPlaying()) player.pause();
            break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
            // Lost focus for a short time, but it's ok to keep playing
            // at an attenuated level
            if (player.isPlaying()) player.setVolume(0.1f, 0.1f);
            break;
    }
}

private boolean requestAudioFocus() {
    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        //Focus gained
        return true;
    }
    //Could not gain focus
    return false;
}

private boolean removeAudioFocus() {
    return AudioManager.AUDIOFOCUS_REQUEST_GRANTED ==
            audioManager.abandonAudioFocus(this);
}

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

@Override
public  boolean onUnbind(Intent intent) {
    player.stop();
    player.release();
    return false;
}

@Override
public void onCompletion(MediaPlayer mediaPlayer) {

}



@Override
public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
    return false;
}

@Override
public void onPrepared(MediaPlayer mediaPlayer) {
    mediaPlayer.start();
}

public void playSong() {
    player.reset();
    Song playSong = songs.get(songIndex);
    Uri trackUri = Uri.parse(playSong.uri);

    try {
        player.setDataSource(getApplicationContext(), trackUri);
        player.prepareAsync();
    } catch (Exception ex) {
        Log.e("Music Service", "Error setting data source", ex);
    }
}
}

JS:

<ion-col>
    <button ion-button clear small color="danger" icon-left (click)="openUrl(item, item.buttonModel.url)">
        <ion-icon name='{{item.buttonModel.icon}}'></ion-icon>
        <h2>{{item.buttonModel.title}}</h2>
    </button>
</ion-col>

具体来说,我想在从浏览器返回后才翻转showActions的值。

1 个答案:

答案 0 :(得分:0)

好吧,当'iab'关闭时,退出事件似乎会触发 似乎您需要附加另一个听众。
请参阅here 所以eventname是'退出'
 在该侦听器功能中,可以:

  • change item.buttonModel.title
  • 或者也许添加 <h2 #btn>@ViewChild('#btn', {read:ElementRef}) el:ElementRef
    然后,您可以设置el.innerText ...