我在这里制作了一个无线电流媒体应用程序代码:
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。可能的解决办法是什么?请帮帮忙?
答案 0 :(得分:0)
感谢“StinePike”使用新线程中的代码很简单它工作得很好!