Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在整个应用程序中管理循环搜索栏_Android_Android Mediaplayer - Fatal编程技术网

Android 在整个应用程序中管理循环搜索栏

Android 在整个应用程序中管理循环搜索栏,android,android-mediaplayer,Android,Android Mediaplayer,我正在开发一个用于在线播放歌曲的应用程序。我可以播放歌曲点击它 我的问题是,我在所有活动中都有一个CircularsekBar,我需要在所有屏幕上管理它。如果在任何活动中播放歌曲,则所有屏幕上都应显示Ekbar。我能做的是,当播放一首歌曲时,我能够在该特定活动中显示seekbar,但无法在任何其他活动中管理该seekbar 下面是我的一个xml文件的示例: <android.support.design.widget.CoordinatorLayout xmlns:android="htt

我正在开发一个用于在线播放歌曲的应用程序。我可以播放歌曲点击它

我的问题是,我在所有活动中都有一个CircularsekBar,我需要在所有屏幕上管理它。如果在任何活动中播放歌曲,则所有屏幕上都应显示Ekbar。我能做的是,当播放一首歌曲时,我能够在该特定活动中显示seekbar,但无法在任何其他活动中管理该seekbar

下面是我的一个xml文件的示例:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">
 </LinearLayout>

<include layout="@layout/seek_bar_layout"></include>
</android.support.design.widget.CoordinatorLayout>

请让我知道如何在所有活动中管理Seekbar


提前感谢。

当其他视图可见时,您无法管理活动视图

您可以通过任何
布尔
标志(例如:
isSongPlaying=false
)来解决该问题。开始播放歌曲时,将其设置为
true
。在onResume中的每个活动中,检查该标志并显示/隐藏您的
SeekBar

尽量避免使用
公共静态
,并使用任何其他方式


编辑: 根据您的评论,我创建了一个非常简单的示例,说明如何使用服务解决您的问题。记住,这段代码需要很多改进

歌曲服务

public class SongService extends Service {

    ActivityManager activityManager;
    IBinder iBinder;
    boolean playingSong = false;
    Worker worker;

    public SongService() {
        iBinder = new MyBinder();
    }

    private void playSong(String path) {
        if(worker != null) {
            worker.cancel(true);
        }
        worker = new Worker();
        worker.execute();
    }

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    private class Worker extends AsyncTask<Void, Integer, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            playingSong = true;
        }

        @Override
        protected Void doInBackground(Void... params) {
            for (int i = 100; i > -1; i--) {
                publishProgress(i);
                try {
                    Thread.sleep(100l);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            if (activityManager != null) {
                activityManager.publishProgress(values[0]);
            }
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            playingSong = false;
            activityManager.onSongFinish();
        }
    }

    public class MyBinder extends Binder {

        public SongService getSongService() {
            return SongService.this;
        }

        public void BindView(ActivityManager activityManager) {
            SongService.this.activityManager = activityManager;
        }

        public void playSong(String path) {
            SongService.this.playSong(path);
        }

        public boolean isPlayingSong() {
            return playingSong;
        }
    }

    public interface ActivityManager {
        void publishProgress(int progress);

        void onSongFinish();
    }
}
维护活动2

此活动类与MainActivity非常相似,但布局文件不同

main活动-布局:

public class MainActivity
        extends AppCompatActivity
        implements ServiceConnection, SongService.ActivityManager {

    TextView progress;
    Button play;
    Button next;
    SongService.MyBinder myBinder;

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

        progress = (TextView) findViewById(R.id.progress);
        play = (Button) findViewById(R.id.play);
        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(myBinder != null) {
                    //Your path to song
                    myBinder.playSong("");
                    play.setEnabled(false);
                }
            }
        });


        next = (Button) findViewById(R.id.next);
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                startActivity(intent);
            }
        });
    }

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

        Intent mIntent = new Intent(this, SongService.class);
        bindService(mIntent, this, BIND_AUTO_CREATE);
    }

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

        unbindService(this);
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        if(service instanceof SongService.MyBinder) {
            myBinder = (SongService.MyBinder) service;
            myBinder.BindView(this);

            if (myBinder.isPlayingSong()) {
                play.setEnabled(false);
            }
            else {
                play.setEnabled(true);
            }
        }
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        myBinder = null;
    }

    @Override
    public void publishProgress(int progress) {
        this.progress.setText("Progress: " + progress);
    }

    @Override
    public void onSongFinish() {
        play.setEnabled(true);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.aszymanski2.serviceanddelegatepatternexample.MainActivity">

    <TextView
        android:id="@+id/progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:text="Play"
        android:id="@+id/play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"/>

    <Button
        android:text="SecondActivity"
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.aszymanski2.serviceanddelegatepatternexample.MainActivity">

    <TextView
        android:id="@+id/progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:text="Play"
        android:id="@+id/play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"/>

    <Button
        android:text="Back"
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"/>
</RelativeLayout>

维护活动2-布局:

public class MainActivity
        extends AppCompatActivity
        implements ServiceConnection, SongService.ActivityManager {

    TextView progress;
    Button play;
    Button next;
    SongService.MyBinder myBinder;

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

        progress = (TextView) findViewById(R.id.progress);
        play = (Button) findViewById(R.id.play);
        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(myBinder != null) {
                    //Your path to song
                    myBinder.playSong("");
                    play.setEnabled(false);
                }
            }
        });


        next = (Button) findViewById(R.id.next);
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                startActivity(intent);
            }
        });
    }

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

        Intent mIntent = new Intent(this, SongService.class);
        bindService(mIntent, this, BIND_AUTO_CREATE);
    }

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

        unbindService(this);
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        if(service instanceof SongService.MyBinder) {
            myBinder = (SongService.MyBinder) service;
            myBinder.BindView(this);

            if (myBinder.isPlayingSong()) {
                play.setEnabled(false);
            }
            else {
                play.setEnabled(true);
            }
        }
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        myBinder = null;
    }

    @Override
    public void publishProgress(int progress) {
        this.progress.setText("Progress: " + progress);
    }

    @Override
    public void onSongFinish() {
        play.setEnabled(true);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.aszymanski2.serviceanddelegatepatternexample.MainActivity">

    <TextView
        android:id="@+id/progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:text="Play"
        android:id="@+id/play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"/>

    <Button
        android:text="SecondActivity"
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.aszymanski2.serviceanddelegatepatternexample.MainActivity">

    <TextView
        android:id="@+id/progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:text="Play"
        android:id="@+id/play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"/>

    <Button
        android:text="Back"
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"/>
</RelativeLayout>


谢谢Artur,我使用的是布尔值,但如何访问上一个活动的视图。我可以使seekbar可见,但seekbar进程没有发生,如果我在另一个活动中单击该seekbar,我将如何更改上一个活动中的图标。@我认为您必须更改您的逻辑。如果我理解正确,活动播放歌曲对吗?在这种情况下,将此功能移动到服务,并使用委托模式。每次启动其他活动时,请绑定此活动和您的服务。该解决方案使您能够通过服务管理当前连接的(可见)活动。是的,活动播放歌曲。但我从未使用过服务,如何使用服务管理它?我在活动“a”中播放了一首歌,现在我在活动“B”中。我可以从活动B控制活动A中的图标吗?@arbit好的,您必须阅读有关
服务
委托模式
的信息。目前我无法访问IDE,无法创建完整的示例。好的,没有问题……感谢您的帮助。只要您有可能创建示例。请做并分享。