无法停止服务?

时间:2016-12-23 03:27:28

标签: android service

我正在使用媒体播放器播放声音。当从第二个活动按下自定义工具栏图像时,音频服务不会停止。

 public class ClassMain extends AppCompatActivity {

        LinearLayout linearclass1;
        Toolbar toolbar;
        TextView customtexttoolbartitle;
        MediaPlayer mediaPlayer;
        Handler handler = new Handler();
        final static int Delay = 1000;


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

            toolbar = (Toolbar) findViewById(R.id.custom_toolbar);
            customtexttoolbartitle = (TextView) findViewById(R.id.custom_toolbar_title);
            customtexttoolbartitle.setText("Select Your Grade");

            playAudioWithDelay();
            linearclass1 = (LinearLayout) findViewById(R.id.linear_class_1);
            linearclass1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(ClassMain.this, ClassOneSubjectList.class);
                    startActivity(intent);
                    mediaPlayer.stop();
                    startService(v);

                }
            });
        }

        public void startService(View view) {

            startService(new Intent(this, UseBackgroundSoundService.class));

        }


        public void playAudioWithDelay() {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    sound();
                }

            }, Delay);

        }


        void sound() {


            mediaPlayer = new MediaPlayer();
            mediaPlayer = MediaPlayer.create(this, R.raw.got);
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.setLooping(false);
            mediaPlayer.start();
            mediaPlayer.setVolume(100, 100);


        }


        @Override
        protected void onPause() {
            // TODO Auto-generated method stub
            super.onPause();
            mediaPlayer.stop();

        }


        @Override
        protected void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
            mediaPlayer.stop();

        }


    }

这是我的第二项活动

公共类ClassOneSubjectList扩展了AppCompatActivity {

    Toolbar toolbar;
    TextView customtexttoolbartitle;
    ImageView customtoolbarimage;
    private FeatureCoverFlow mCoverFlow;
    private CoverFlowAdapter mAdapter;
    private ArrayList<BookEntity> mData = new ArrayList<>(0);
    private TextSwitcher mTitle;

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


        customtexttoolbartitle = (TextView) findViewById(R.id.custom_toolbar_title);
        customtexttoolbartitle.setText("Select Subject");
        customtoolbarimage = (ImageView) findViewById(R.id.custom_toolbar_image);
        customtoolbarimage.setVisibility(View.VISIBLE);
        toolbar = (Toolbar) findViewById(R.id.custom_toolbar);
        customtoolbarimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(ClassOneSubjectList.this, ClassMain.class);
                startActivity(intent);
                stopService(new Intent(ClassOneSubjectList.this, UseBackgroundSoundService.class));

            }
        });


        mData.add(new BookEntity(R.mipmap.social, R.string.title1));
        mData.add(new BookEntity(R.mipmap.maths, R.string.title2));
        mData.add(new BookEntity(R.mipmap.science, R.string.title3));
        mData.add(new BookEntity(R.mipmap.nepali, R.string.title4));

        mTitle = (TextSwitcher) findViewById(R.id.title);
        mTitle.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                LayoutInflater inflater = LayoutInflater.from(ClassOneSubjectList.this);
                TextView textView = (TextView) inflater.inflate(R.layout.activity_item_subject_book_title, null);
                return textView;
            }
        });
        Animation in = AnimationUtils.loadAnimation(this, R.anim.slide_in_top);
        Animation out = AnimationUtils.loadAnimation(this, R.anim.slide_out_bottom);
        mTitle.setInAnimation(in);
        mTitle.setOutAnimation(out);

        mAdapter = new CoverFlowAdapter(this);
        mAdapter.setData(mData);
        mCoverFlow = (FeatureCoverFlow) findViewById(R.id.coverflow);
        mCoverFlow.setAdapter(mAdapter);

        mCoverFlow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                switch (position) {
                    case 0:
                        Intent intent = new Intent();
                        startActivity(intent);
                        break;
                    case 1:
                        break;
                    case 2:
                        stopService(new Intent(ClassOneSubjectList.this, UseBackgroundSoundService.class));
                        Intent intent2 = new Intent(ClassOneSubjectList.this, ChooseExerciseSyllabus.class);
                        startActivity(intent2);
                        break;
                }


            }
        });

        mCoverFlow.setOnScrollPositionListener(new FeatureCoverFlow.OnScrollPositionListener() {
            @Override
            public void onScrolledToPosition(int position) {
                mTitle.setText(getResources().getString(mData.get(position).titleResId));

            }

            @Override
            public void onScrolling() {
                mTitle.setText("");
            }
        });


    }

    @Override
    public boolean stopService(Intent name) {
        return super.stopService(name);
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
        stopService(new Intent(ClassOneSubjectList.this, UseBackgroundSoundService.class));
    }


}

我的后台服务活动

public class UseBackgroundSoundService extends Service {
    private static final String Tag = null;
    MediaPlayer player;


    Handler handler = new Handler();
    final static int Delay = 1000;


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


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        return super.onStartCommand(intent, flags, startId);

    }

    @Override
    public void onCreate() {

        player = MediaPlayer.create(this, R.raw.got);
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        player.setLooping(false);
        player.setVolume(100, 100);
        playAudioWithDelaysound();
    }


    @Override
    public void onDestroy() {
        player.stop();
        player.release();


    }

    public void playAudioWithDelaysound() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(UseBackgroundSoundService.this, UseBackgroundSoundService.class);
                startService(intent);
                player.start();


            }

        }, Delay);

    }


}

服务正在停止反击,但它没有停止从第二个活动的CustomToolbar Image(Back)意图到First Activity?请帮助

0 个答案:

没有答案