【发布时间】:2016-02-16 04:28:25
【问题描述】:
我开发了在线广播应用程序。我通过服务呼叫了广播电台服务器。当我调用服务时,所有 UI 控件都被冻结,因此我使用了 ProgressDialog 但仍然冻结并且没有显示进度对话框。我使用了 AsyncTask 但没有用。
HomeFragment.java
在按钮的onClick事件中,执行CallingStreamServer AsyncTask。
public class CallingStreamServer extends AsyncTask<Void, Void, Void>{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
progressDialog = new ProgressDialog(getActivity());
progressDialog.setTitle("Please Wait");
progressDialog.setMessage("Connecting Radio Station ...");
progressDialog.setIndeterminate(true);
progressDialog.setCancelable(false);
progressDialog.show();
//progressDialog = ProgressDialog.show(getActivity(), "Please Wait", "Connecting Radio Station ...");
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
getActivity().startService(new Intent(getActivity(), StreamService.class));
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
progressDialog.dismiss();
super.onPostExecute(result);
}
}
StreamService.java
StreamService 创建媒体播放器并连接广播电台服务器。
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.d(TAG, "onCreate");
try {
prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
sharedPreEditor = prefs.edit();
notiManager = (NotificationManager)getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
Context context = getApplicationContext();
String notiTitle = context.getResources().getString(R.string.app_name);
String notiMsg = "Connecting ... ";
Intent mainActivity = new Intent(context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, mainActivity, 0);
noti = new Notification.Builder(context)
.setContentTitle(notiTitle)
.setContentText(notiMsg)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.build();
notiManager.notify(notiID, noti);
String url = prefs.getString("streamURL", "");
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(url);
mediaPlayer.prepare();
} catch (Exception e) {
// TODO: handle exception
Log.e(TAG, "onCreate Method : " + e.getMessage());
}
}
@Override
@Deprecated
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Log.d(TAG, "onStart");
try {
mediaPlayer.start();
sharedPreEditor.putBoolean("isPlaying", true);
sharedPreEditor.commit();
Context context = getApplicationContext();
String notiTitle = context.getResources().getString(R.string.app_name);
String notiMsg = prefs.getString("displayStation", "") + " radio is now playing.";
Intent _intent = new Intent(context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, _intent, 0);
noti = new Notification.Builder(context)
.setContentTitle(notiTitle)
.setContentText(notiMsg)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.build();
noti.flags = Notification.FLAG_NO_CLEAR;
notiManager.notify(notiID, noti);
} catch (Exception e) {
Log.e(TAG, "onStart Method : " + e.getMessage());
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.d(TAG, "onDestroy");
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
sharedPreEditor.putBoolean("isPlaying", false);
sharedPreEditor.commit();
notiManager.cancel(notiID);
stopSelf();
}
【问题讨论】:
-
不要从 AsyncTask 创建对话框。使用上下文在 Service 本身中创建您的对话框。这里的问题可能是您正在创建对话框然后启动您的服务,但是您如何确保您的服务已完成任务并将其捕获在 asyncTask 的 onPostExecute 中。
-
@Rakesh 我的主要问题是我调用服务时 UI 冻结。
-
是的,我了解您的问题。从服务中为任何繁重的调用创建线程或异步任务。如果您需要自定义,请尝试使用接口实现观察者模式,并在繁重的处理/校准后回调以传递数据
标签: android android-asynctask android-service android-mediaplayer shoutcast