Android的服务(QUOT; startService和放大器; stopService")和一个播放/停止按钮 [英] Android Service ("startService & stopService") and Play/Stop Button in One

查看:118
本文介绍了Android的服务(QUOT; startService和放大器; stopService")和一个播放/停止按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android应用程序,我需要做一个按钮,这将是一个播放/停止按键为Android服务演奏和停止。

I am developing an Android app and I need to make one button which will be a Play/Stop button for Android Service playing and stopping.


  • 播放按钮是 startActivity();

停止按钮是 stopActivity();

我如何做呢?

推荐答案

您只需要声明一个标志变量,并基于这样的标志值的onclick申报主体()。

You just need to declare a flag variable and declare body of onclick() based on flag value like this.

public class ServiceActivity extends Activity {
    Button play;
    int button_status=1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        play=(Button)findViewById(R.id.button1);

        play.setOnClickListener(new OnClickListener() {         
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if(button_status == 1)//play the service
                {
                button_status=0;
                Intent i=new Intent(ServiceActivity.this,Playing.class);
                startService(i);
                }
                else//stop the service
                {
                button_status=1;
                Intent i=new Intent(ServiceActivity.this,Playing.class);
                stopService(i); 
                } 
        });

    }  
}

您也可以使用切换按钮为您purose。

这篇关于Android的服务(QUOT; startService和放大器; stopService")和一个播放/停止按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆