停止启动服务的意图

时间:2012-09-05 18:06:10

标签: android android-intent

我有一个Intent,它会启动MusicService连接到互联网并播放信息流:

final Intent i = new Intent(MusicService.ACTION_URL);
Uri uri = Uri.parse("http://31.3.242.244:8058");

i.setData(uri);
startService(i);

progressDialog = ProgressDialog.show(fmActivity.this, "", "connecting...");
progressDialog.setCancelable(true);

建立连接后,我有Handler关闭对话框:

static Handler closeHandler = new Handler() {
    public void handleMessage(Message msg) {
        if (progressDialog !=null) {
            progressDialog.dismiss();
        }
    }
};    
fmActivity.closeHandler.handleMessage(null); //from the MusicService

除非连接挂起或服务器连接速度慢,否则这一切都很有效。我需要能够取消Intent尝试使用后退按钮启动MusicService。检查互联网连接无济于事,因为始终存在连接...用户需要能够取消连接呼叫,因为如果与服务器的连接因任何原因而失败,则会阻止用户加载不同的流。我已经搜遍过,无法找到如何做到这一点。感谢。

1 个答案:

答案 0 :(得分:0)

您可以在取消取消ProgressDialog内的OnClickListener之前停止播放音乐服务。

例如:

    progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog, int which) {
                stopService(i); // Stop the music service here
                dialog.dismiss();
            }
});
相关问题