Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.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 - Fatal编程技术网

在android中关闭并退出我的应用程序

在android中关闭并退出我的应用程序,android,Android,我有一个应用程序,在主页上我有一些按钮,用于导航应用程序 在该页面上,我有一个按钮“退出”,单击该按钮时,用户将进入手机的主屏幕,应用程序图标位于该屏幕上 但它让我进入主屏幕,但收音机仍然工作 我该怎么做 我的代码id:- //method for exit. public void exitradio() { Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HO

我有一个应用程序,在主页上我有一些按钮,用于导航应用程序

在该页面上,我有一个按钮“退出”,单击该按钮时,用户将进入手机的主屏幕,应用程序图标位于该屏幕上

但它让我进入主屏幕,但收音机仍然工作

我该怎么做

我的代码id:-

//method for exit.
public void exitradio() {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}
编辑:-

我试试这个

@Override
public void onClick(View v){
            if(v == PlayBtn){
                 startradio();
              }
             else if(v == PauseBtn){
                 pauseradio();
              }
             else if(v == ExitBtn){
                 exitradio();
              }
             else if(v == RefreshBtn){
                 try {
                    refreshradio();
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (SecurityException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
              }
            }
以及
exitradio()代码是:-

//method for exit.
public void exitradio() {
    //finish();
    System.exit(0);
}
仍然关闭应用程序,但收音机仍然工作


我的主要活动课程是:-

public class MainActivity extends Activity implements OnClickListener{
        // Define ..............................................................
        private static ProgressDialog progressDialog;
        public MediaPlayer mp;
        boolean isPrepared = false;
        Button PlayBtn , ExitBtn , PauseBtn , RefreshBtn;
        String MEDIA_PATH;

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressDialog = ProgressDialog.show(MainActivity.this, "", "Buffering Radio...",true);
        progressDialog.setCancelable(false);
        // Declare ..............................................................
        mp = new MediaPlayer();
        PlayBtn = (Button)findViewById(R.id.btnPlay);
        PlayBtn .setOnClickListener(this);
        PauseBtn = (Button)findViewById(R.id.btnPause);
        PauseBtn .setOnClickListener(this);
        RefreshBtn = (Button)findViewById(R.id.btnRefresh);
        RefreshBtn .setOnClickListener(this);
        ExitBtn = (Button)findViewById(R.id.btnExit);
        ExitBtn .setOnClickListener(this);
        MEDIA_PATH = "http://radio.arabhosters.com:8015/"; 

        // Volume Control ..............................................................
        final AudioManager leftAm = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        int maxVolume = leftAm.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        int curVolume = leftAm.getStreamVolume(AudioManager.STREAM_MUSIC);
        SeekBar volControl = (SeekBar)findViewById(R.id.volumebar);
        volControl.setMax(maxVolume);
        volControl.setProgress(curVolume);
        volControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar arg0) {
             // TODO Auto-generated method stub
            }

            @Override
            public void onStartTrackingTouch(SeekBar arg0) {
             // TODO Auto-generated method stub
            }

            @Override
            public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
             // TODO Auto-generated method stub
             leftAm.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0);
            }
           });  
    }


@Override
public void onClick(View v){
            if(v == PlayBtn){
                 startradio();
              }
             else if(v == PauseBtn){
                 pauseradio();
              }
             else if(v == ExitBtn){
                 exitradio();
              }
             else if(v == RefreshBtn){
                 try {
                    refreshradio();
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (SecurityException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
              }
            }

public void onCompletion(MediaPlayer mediaPlayer) {
         synchronized(this){
            isPrepared = false;
            }
            }

protected void onResume (){
        super.onResume();

        try {
            mp.setDataSource(MEDIA_PATH);
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            mp.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } //also consider mp.prepareAsync().
        // defult start stream when start App.  
        mp.start();
        mp.setVolume(100, 100);
        progressDialog.dismiss();        
        }

        // method for play stream after stop it.
public void startradio() {
            try{
                if(mp.isPlaying()){
                    return;
                }  
                   mp.start();
                   progressDialog.dismiss();
            } catch(IllegalStateException ex){
                ex.printStackTrace();
            } 
        }

// method for Refresh stream.
public void refreshradio() throws IllegalArgumentException, SecurityException, IOException {
        try{
            if(mp.isPlaying()){
                return;
            }
               mp.reset();
               mp.setDataSource(MEDIA_PATH);
               mp.prepare(); 
               mp.start();
               progressDialog.dismiss(); 
        } catch(IllegalStateException ex){
            ex.printStackTrace();
        } 
    }

// method for pause stream. 
public void pauseradio() {
        mp.pause();
    }

// method for check is radio paly or not stream
public boolean isPlaying() {
        return mp.isPlaying();
    }

// method for Looping audio if your record it - Soon :)
public boolean isLooping() {
        return mp.isLooping();
    }

// method for Looping audio if your record it - Soon :)
public void setLooping(boolean isLooping) {
        mp.setLooping(isLooping);
    }

// method for volume
public void setVolume(float volumeLeft, float volumeRight) {
        mp.setVolume(volumeLeft, volumeRight);
    }

// method for stop stream.
public void stopradio() {
        if(mp.isPlaying()){
            mp.stop();
        }
        mp.release();
    }

//method for exit.
public void exitradio() {
    //finish();
    System.exit(0);
}

//method for back to main menu "Home".
public void backtomenu() {
  finish();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


}
和StartPoint类,如:-

public class StartPoint extends Activity{

ProgressBar progressBar;
private int progressBarStatus = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    progressBar = (ProgressBar)findViewById(R.id.progressBar1);


    Thread timer = new Thread(){
        public void run(){
            try{
                sleep(5000);
                while(progressBarStatus < 5000){
                    StartPoint.this.runOnUiThread(new Runnable(){
                        public void run()
                        {
                            progressBar.setProgress(progressBarStatus);
                            progressBarStatus += 1000;
                        }
                    });

                }
            }catch(InterruptedException e){
                e.printStackTrace();
            }finally{
                Intent openMainList = new Intent(StartPoint.this, com.example.kam.MainActivity.class);
                startActivity(openMainList);
            }
        }
    };
    timer.start();
}

}
公共类StartPoint扩展活动{
ProgressBar ProgressBar;
private int progressBarStatus=0;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
progressBar=(progressBar)findViewById(R.id.progressBar1);
线程计时器=新线程(){
公开募捐{
试一试{
睡眠(5000);
同时(状态<5000){
StartPoint.this.rununuithread(新的Runnable()命令){
公开募捐
{
progressBar.setProgress(progressBar状态);
状态+=1000;
}
});
}
}捕捉(中断异常e){
e、 printStackTrace();
}最后{
Intent openMainList=newintent(StartPoint.this、com.example.kam.MainActivity.class);
startActivity(openMainList);
}
}
};
timer.start();
}
}

尝试在退出按钮上设置侦听器(

在我的示例中是:btn_exit

) 您的应用将完全退出:)

**

还要确保第一个活动已完成(onStop()==>make) finish())


**

你在说什么收音机?另外,为什么要引入令人困惑的UI元素?Android有一个非常好的“主页”按钮。你给了他们一个额外的主页按钮,这对任何人都没有好处。你只是把你的UI弄得乱七八糟。@Dr.Dredel我怎样才能把我的UI弄得乱七八糟谢谢你,我看不出你也可以确保第一个活动已经完成(onStop()===>make finish())现在工作正常了谢谢你能来这里吗:)
    public class testprj extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button btn_exit = (Button) findViewById(R.id.btn1);
    btn_exit .setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
            System.exit(0);
        }
    });
    }
 }