gpt4 book ai didi

java - 方向改变时如何处理服务

转载 作者:行者123 更新时间:2023-12-02 03:30:26 27 4
gpt4 key购买 nike

我正在制作一个用于学习目的的音乐播放器应用程序,但我陷入了用户决定更改设备方向的位置,我的应用程序总是重新创建,这很糟糕,我已经设法保存了所有其他数据并且使用 onRestoreInstanceState 恢复,但我想不出使用 musicplayer 服务 的好方法,下面是我的代码的一部分,代码很多,所以我将重点关注问题

public class ManagerActivty extends AppCompatActivity{

//music player service object
private MusicService musicService;

//boolean to keep track of musicservice state
private boolean musicBound = Boolean.FALSE;

//Music service connection object
private ServiceConnection musicServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
if(!instanceSaved) {
MusicService.MusicBinder binder = (MusicService.MusicBinder) iBinder;
musicService = binder.getService();
musicService.setOnSongChangeListener(ManagerActivty.this);
musicService.setOnPlayerStateChangedListener(ManagerActivty.this);
new loadSongAsync().execute();
musicBound = Boolean.TRUE;
System.out.println("Music Service Connected");

}

}

@Override
public void onServiceDisconnected(ComponentName componentName) {
musicBound = Boolean.FALSE;
}
};



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manager_activty);
}

//method to bind music service with my activity
private void bindMusicService()
{
playMusicIntent = new Intent(this, MusicService.class);
bindService(playMusicIntent, musicServiceConnection, Context.BIND_AUTO_CREATE);
startService(playMusicIntent);
System.out.println("Binder Called");

}



@Override
protected void onStart() {
super.onStart();

//launch binder
bindMusicService();
}


@Override
protected void onDestroy() {
//stop service connection
if (playMusicIntent != null) {
stopGuiUpdates();
unbindService(musicServiceConnection);
stopService(playMusicIntent);
musicService = null;
}
super.onDestroy();
}

}




@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
System.out.println("Saved Instances");
outState.putBoolean(Constant.IS_MEDIA_PLAYING_PLAYING, musicService.isPlaying());
outState.putInt(Constant.MEDIA_PLAYER_POSITION, musicService.getMediaPlayer().getCurrentPosition());
outState.putBoolean(Constant.IS_MUSIC_REPLAY, musicService.isReplay());

}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
System.out.println("Retrieving Instances");

//if null try to re-bind service
if(musicService == null) {
bindMusicService();

}


}

当我在方向更改后尝试访问 musicservice 对象时,此代码给我一个空指针异常,任何以前处理过此问题的人请帮忙。

最佳答案

您可以将配置更改声明为 <activity> App Manifest.xml 文件中的标记

android:configChanges="orientation|keyboardHidden"

这是您可以在 <activity> 中设置的属性的完整列表。 Manifest.xml

中的标记
<activity android:allowEmbedded=["true" | "false"]
android:allowTaskReparenting=["true" | "false"]
android:alwaysRetainTaskState=["true" | "false"]
android:autoRemoveFromRecents=["true" | "false"]
android:banner="drawable resource"
android:clearTaskOnLaunch=["true" | "false"]
android:configChanges=["mcc", "mnc", "locale",
"touchscreen", "keyboard", "keyboardHidden",
"navigation", "screenLayout", "fontScale",
"uiMode", "orientation", "screenSize",
"smallestScreenSize"]
android:documentLaunchMode=["intoExisting" | "always" |
"none" | "never"]
android:enabled=["true" | "false"]
android:excludeFromRecents=["true" | "false"]
android:exported=["true" | "false"]
android:finishOnTaskLaunch=["true" | "false"]
android:hardwareAccelerated=["true" | "false"]
android:icon="drawable resource"
android:label="string resource"
android:launchMode=["multiple" | "singleTop" |
"singleTask" | "singleInstance"]
android:maxRecents="integer"
android:multiprocess=["true" | "false"]
android:name="string"
android:noHistory=["true" | "false"]
android:parentActivityName="string"
android:permission="string"
android:process="string"
android:relinquishTaskIdentity=["true" | "false"]
android:screenOrientation=["unspecified" | "behind" |
"landscape" | "portrait" |
"reverseLandscape" | "reversePortrait" |
"sensorLandscape" | "sensorPortrait" |
"userLandscape" | "userPortrait" |
"sensor" | "fullSensor" | "nosensor" |
"user" | "fullUser" | "locked"]
android:stateNotNeeded=["true" | "false"]
android:taskAffinity="string"
android:theme="resource or theme"
android:uiOptions=["none" | "splitActionBarWhenNarrow"]
android:windowSoftInputMode=["stateUnspecified",
"stateUnchanged", "stateHidden",
"stateAlwaysHidden", "stateVisible",
"stateAlwaysVisible", "adjustUnspecified",
"adjustResize", "adjustPan"] >
. . .
</activity>

关于java - 方向改变时如何处理服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38172265/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com