我制作了一个用于广播流媒体的应用程序,代码如下:
public class streamService extends Service {
public static MediaPlayer stream;
public static String url;
public static int serviceAvailable;
private static final int HELLO_ID = 1;
public static NotificationManager mNotificationManager;
public static PowerManager.WakeLock wl;
public static Boolean bgPlay = false;
TelephonyManager tm;
ProgressDialog pDialog;
public static String Tag="";
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
pDialog=new ProgressDialog(RadioList.con);
pDialog.setTitle("Please wait!!");
pDialog.setMessage("Connecting");
pDialog.setCancelable(false);
pDialog.show();
}
public void onStart(Intent intent, int startid) {
// Restore preferences
Tag=RadioList.Tag;
//RadioStationThree.pDialog.show();
stream = new MediaPlayer();
try {
stream.setDataSource(RadioList.URL);
} catch (IllegalArgumentException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalStateException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
stream.setAudioStreamType(AudioManager.STREAM_MUSIC);
stream.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer stream) {
/* tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
tm.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
makeNotification();
*/ // Set the Wake Lock - CPU on, keyboard and screen off
/*PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK,
getString(R.string.app_name));
wl.acquire();*/
serviceAvailable = 1;
System.out.println("HERE CANCEL");
pDialog.cancel();
stream.start();
}
});
try {
stream.prepare();
} catch (IllegalStateException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
@Override
public void onDestroy() {
// kill the stream
serviceAvailable = 0;
stream.stop();
stream.release();
stream = null;
// kill the status notification
}
此服务在单击活动中的按钮时启动但是当我单击按钮时,应用程序会停止工作一段时间,然后继续正常运行(有时还会显示 ANR)。我也使用了 ProgressDialog 但它也停止旋转并显示 ANR。对此可能的解决方案是什么?请帮忙?