我正在尝试为Android创建自己的MusicPlayer.我遇到问题的地方是在后台运行一些东西.主要活动管理GUI,到目前为止所有的歌曲都在播放.我想分开GUI和音乐播放课程.我想把音乐管理部分放在服务中,并留下现在的其他东西.
我的问题是我无法组织活动和服务之间的沟通,因为它们之间正在进行大量的沟通,包括双向移动物体.我尝试了很多技术,我在这里搜索Stack Overflow但每次遇到问题时都会这样做.我需要Service才能将对象发送到Activity,反之亦然.当我添加小部件时,我也希望它能够与服务进行通信.
任何提示都很受欢迎,如果你需要源代码发表评论,但现在在这个转变中它变得混乱.
是否有更高级的教程,而不是调用一个从服务返回随机数的方法?:P
编辑:可能的解决方案是使用RoboGuice库并注入移动对象
我使用的是最新的Firebase API(3.2.1),我使用此代码检查用户是否已登录:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if(self.navigationController != nil){
self.navigationController!.setNavigationBarHidden(true, animated: true)
}
if(FIRAuth.auth() != nil){
self.performSegueWithIdentifier("loginSuccessSegue", sender: self)
}
}
Run Code Online (Sandbox Code Playgroud)
换句话说,如果存在auth对象,我将切换到其他控制器.在那个控制器上,我有一个退出按钮,它正在这样做:
do{
try FIRAuth.auth()?.signOut()
self.performSegueWithIdentifier("logoutSegue", sender: self)
}catch{
print("Error while signing out!")
}
Run Code Online (Sandbox Code Playgroud)
我没有得到这个操作的错误,但当我切换到登录控制器时,这个auth对象存在,我再次切换回带有数据的控制器.我还尝试检查auth中的当前用户对象,它是否存在且有效.
任何人都知道我如何正确退出?
我有这种方法:
void showAllSongsMenu() {
if (rebuilding) {
Toast.makeText(MusicPlayerActivity.this,
"Database rebuild in progress, please wait!",
Toast.LENGTH_SHORT).show();
return;
}
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
final View popupView = layoutInflater.inflate(R.layout.allsongs_list,
null);
allSongs = new PopupWindow(popupView, LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT, true);
Button allSongsMenu = (Button) allSongs.getContentView().findViewById(
R.id.close_all_songs_menu);
allSongs.setBackgroundDrawable(MusicPlayerActivity.this.getResources()
.getDrawable(R.drawable.unknown_artist));
allSongs.setFocusable(true);
allSongsMenu.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
allSongs.dismiss();
}
});
ListView lv = (ListView) allSongs.getContentView().findViewById(
R.id.all_songs_list);
//registerForContextMenu(lv);
lv.setOnCreateContextMenuListener(new OnCreateContextMenuListener(){
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Song options");
menu.add(0, v.getId(), …Run Code Online (Sandbox Code Playgroud) 资源:
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
String s = "MusicPlayer";
if (MusicPlayerActivity.currentActivity != null) {
s = MusicPlayerActivity.currentActivity.getCurrentTrackName();
} else {
Log.d(MusicPlayerActivity.TAG,"STARTING ACTIVITY FROM WIDGET");
Intent intent1 = new Intent(context, MusicPlayerActivity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
}
// I WANT TO UPDATE TEXT VIEW HERE
if (MusicPlayerActivity.currentActivity != null)
if (intent.getAction().equals(ACTION_WIDGET_NEXT)) {
MusicPlayerActivity.currentActivity.playNext();
} else if (intent.getAction().equals(ACTION_WIDGET_PLAY)) {
MusicPlayerActivity.currentActivity.playPause();
} else if (intent.getAction().equals(ACTION_WIDGET_PREV)) {
MusicPlayerActivity.currentActivity.playPrevious();
}
}
Run Code Online (Sandbox Code Playgroud)
我用这段代码试了一下:
Log.d(MusicPlayerActivity.TAG,s);
remoteViews.setTextViewText(R.id.w_trackInfo, s);
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.w_trackInfo);
Run Code Online (Sandbox Code Playgroud)
但如果我把它放在评论位置,这不起作用.任何帮助赞赏.