
我目前有一个列表视图,当您点击某个项目时,它会使用媒体播放器运行服务.如果我单击列表视图中的另一个项目,则运行的服务应该停止并运行新服务.我使用boolean isRunning设置为false,并在创建服务时返回true.然后在ListvIEw中,我在if语句中调用该标志.但是,它并不完全正常.我想我可能做错了.有任何想法吗?
这可能听起来让我感到困惑,因为这是我的ListvIEw和我的服务的代码.我只在案例3中对此进行测试(因此,我按此项目启动服务,然后单击案例2以查看是否会停止它).
ListvIEw类:
public class PlayListActivity extends ListActivity{ private static final String TAG = PlayListActivity.class.getSimplename(); // Data to put in the listadapter private String[] sdrPlayListnames = new String[] { "Best of June 2011", "Best of May 2011", "dubstep", "House", "Other"}; private listadapter sdrlistadapter; Intent playbackServiceIntentBOJ, playbackServiceIntentBOM, playbackServiceIntentdub; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.playLists_layout); //fill the screen with the List adapter playListFillData(); playbackServiceIntentdub = new Intent(this, dubAudioService.class); Log.d(TAG, "Made dub Service Intent"); } public voID playListFillData() { //create and set up the Array adapter for the List vIEw ArrayAdapter sdrlistadapter = new ArrayAdapter(this, R.layout.List_item, sdrPlayListnames); setlistadapter(sdrlistadapter); } //set up the on List item Click @OverrIDe protected voID onListItemClick(ListVIEw l, VIEw v, int position, long ID) { super.onListItemClick(l, v, position, ID); //create a switch so that each List item is a different playList switch(position){ case 0: Intent BOJintent = new Intent(this, BOJAudioActivity.class); // Create the vIEw using PlayListGroup's LocalActivityManager VIEw vIEw = PlayListGroup.group.getLocalActivityManager() .startActivity("show_city", BOJintent .addFlags(Intent.FLAG_ACTIVITY_CLEAR_top)) .getDecorVIEw(); // Again, replace the vIEw PlayListGroup.group.replaceVIEw(vIEw); // playbackServiceIntentBOJ = new Intent(this, BOJAudioService.class); Log.d(TAG, "Made BOJ Intent"); // startService(playbackServiceIntentBOJ); Log.d(TAG, "started BOJ Service"); break; case 1: Intent BOMintent = new Intent(this, BOMAudioActivity.class); // Create the vIEw using PlayListGroup's LocalActivityManager VIEw vIEw2 = PlayListGroup.group.getLocalActivityManager() .startActivity("show_city", BOMintent .addFlags(Intent.FLAG_ACTIVITY_CLEAR_top)) .getDecorVIEw(); // Again, replace the vIEw PlayListGroup.group.replaceVIEw(vIEw2); Log.d(TAG, "Replace vIEw"); //getApplicationContext().stopService(playbackServiceIntentBOJ); //playbackServiceIntentBOM = new Intent(this, BOJAudioService.class); Log.d(TAG, "Made BOM Service Intent"); // startService(playbackServiceIntentBOM); Log.d(TAG, "started BOM Service"); if(dubAudioActivity.isRunningdub = true){ stopService(playbackServiceIntentdub); Log.d(TAG, "stop service isRunningdub"); }// break; case 2: Intent dubIntent = new Intent (this, dubAudioActivity.class); VIEw vIEw3 = PlayListGroup.group.getLocalActivityManager() .startActivity("show_city", dubIntent .addFlags(Intent.FLAG_ACTIVITY_CLEAR_top)) .getDecorVIEw(); PlayListGroup.group.replaceVIEw(vIEw3); Log.d(TAG, "Replace vIEw"); startService(playbackServiceIntentdub); Log.d(TAG, "started dub service"); break; } }}服务类别:
public class dubAudioService extends Service implements OnPreparedListener, OnCompletionListener{Toast loadingMessage;private static final String TAG = dubAudioService.class.getSimplename(); public static boolean isRunningdub = false; //to keep track of the playList item Vector<PlayListfile> playListItems; MediaPlayer mediaPlayer; String baseURL = ""; //keep track of which item from the vector we are on int currentPlayListltemNumber = 0; public class dubBackgroundAudioServiceBinder extends Binder { dubAudioService getService() { return dubAudioService.this; } } private final IBinder basBinderdub = new dubBackgroundAudioServiceBinder(); @OverrIDe public IBinder onBind(Intent intent) { return basBinderdub; } @OverrIDe public voID onCreate() { Log.v("PLAYERSERVICE", "onCreate"); mediaPlayer = new MediaPlayer(); new MusicAsync().execute(); Log.d(TAG, "execute'd async"); mediaPlayer.setonPreparedListener(this); Log.d(TAG, "set on prepared Listener"); mediaPlayer.setonCompletionListener(this); Log.d(TAG, "set on completion Listener"); isRunningdub = true; Log.d(TAG, "isRunningRUB = true"); } @OverrIDe public int onStartCommand(Intent intent, int flags, int startID) { //if (!mediaPlayer.isPlaying()) { // mediaPlayer.start(); //} return START_STICKY; } class MusicAsync extends AsyncTask<VoID,VoID,VoID>{ @OverrIDe protected voID onPreExecute(){ } @OverrIDe protected VoID doInBackground(VoID... arg0) { // Todo auto-generated method stub //create empty vector playListItems = new Vector<PlayListfile>(); //http clIEnt library httpClIEnt httpClIEnt = new DefaulthttpClIEnt(); httpGet getRequest = new httpGet ("http://dl.dropBox.com/u/24535120/m3u%20playList/dubstepPlayList.m3u"); //i think you Could add the m3u thing in here Log.v("URI",getRequest.getURI().toString()); try { httpResponse httpResponse = httpClIEnt.execute(getRequest); if (httpResponse.getStatusline().getStatusCode() != httpStatus.SC_OK) { // ERROR MESSAGE Log.v("http ERROR",httpResponse.getStatusline().getReasonPhrase()); } else { inputStream inputStream = httpResponse.getEntity().getContent(); BufferedReader bufferedReader = new BufferedReader(new inputStreamReader(inputStream)); String line; while ((line = bufferedReader.readline()) != null) { Log.v("PLAYListliNE","ORIG: " + line); if (line.startsWith("#")) { //Metadata //Could do more with this but not fo Now } else if (line.length() > 0) { String filePath = ""; if (line.startsWith("http://")) { // Assume its a full URL filePath = line; } else { //Assume it’s relative filePath = getRequest.getURI().resolve(line).toString(); } PlayListfile playListfile = new PlayListfile(filePath); playListItems.add (playListfile); } } inputStream.close(); } } catch (ClIEntProtocolException e) { e.printstacktrace(); } catch (IOException e) { e. printstacktrace(); } currentPlayListltemNumber = 0; if (playListItems.size() > 0) { String path = ((PlayListfile)playListItems.get(currentPlayListltemNumber)).getfilePath(); try { mediaPlayer.setDataSource(path); mediaPlayer.prepareAsync();} catch (IllegalArgumentException e) { e.printstacktrace(); }catch (IllegalStateException e) { e.printstacktrace(); }catch (IOException e) { e.printstacktrace();} } return null; } // protected voID onPostExecute(VoID result){ //playbutton. setEnabled (false); } } @OverrIDe public voID onStart(Intent intent, int startID) { super.onStart(intent, startID); } public voID onDestroy() { if (mediaPlayer.isPlaying()) { mediaPlayer.stop(); Log.d(TAG, "music stopp'd"); } //mediaPlayer.release(); Log.d(TAG, "onDestroy"); } @OverrIDe public voID onPrepared(MediaPlayer mediaPlayer) { // Todo auto-generated method stub\ Log.d(TAG, "music is prepared and will start"); mediaPlayer.start(); } public voID onCompletion(MediaPlayer _mediaPlayer) { Log.d(TAG, "Song completed, next song"); mediaPlayer.stop(); mediaPlayer.reset(); if (playListItems.size() > currentPlayListltemNumber + 1) { currentPlayListltemNumber++; String path = ((PlayListfile)playListItems.get(currentPlayListltemNumber)).getfilePath(); try { mediaPlayer.setDataSource(path); mediaPlayer.prepareAsync(); } catch (IllegalArgumentException e) { e. printstacktrace(); } catch (IllegalStateException e) { e.printstacktrace(); } catch (IOException e) { e.printstacktrace(); } } } class PlayListfile { String filePath; public PlayListfile(String _filePath) { filePath = _filePath; } public voID setfilePath(String _filePath) { filePath = _filePath; } public String getfilePath() { return filePath; } } public voID playSong(){ Log.d(TAG, "start'd"); mediaPlayer.start(); } public voID pauseSong(){ Log.d(TAG, "pause'd"); mediaPlayer.pause(); }}解决方法:
这变得非常复杂,但我使用以下内容来查看我的服务是否正在运行:
private boolean isMyServiceRunning() {ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { if ("com.example.MyService".equals(service.service.getClassname())) { return true; }}return false;}我在ListvIEw类中添加了这个,并在每种情况下放置if语句以查看它是否正在运行,如果是,则会停止服务.
我还将我的所有绑定conenctions公开,以便ListvIEw类可以访问它们并在单击时启动它们.
如果有人想进一步了解等,请给我留言.
总结以上是内存溢出为你收集整理的Android列表视图,服务媒体播放器和布尔标志全部内容,希望文章能够帮你解决Android列表视图,服务媒体播放器和布尔标志所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)