问题描述
- startservice之后onstartcommand却没有启动
-
mainactivity代码
package com.example.test_01;//import android.R;
import java.util.List;import com.example.service.PlayerService;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageButton;public class MainActivity extends ActionBarActivity
implements MainFragment.OnMainFragmentInteractionListener,
MyMusicFragment.OnMyMusicFragmentInteractionListener
{//记录当前播放器的状态 private boolean isPause; private FragmentManager fragmentManager; private FragmentTransaction fragmentTransaction; private MainFragment mainFragment; private MyMusicFragment myMusicFragment;
//查找歌曲的类
private FindSongs finder;
//歌曲列表public static List mp3Infos;
//音乐的位置
public static int music_position;
//播放按钮控件
private ImageButton play_button;
/*
这个方法是activity和fragment通信的一种方法
在MainFragment中调用这个方法,可以在activity中做出相应的反应
*/public void onMainFragmentInteraction(int msg){
/* 对其中的参数msg做出判断,如果为CHANGE_TO_MY_MUSIC_FRAGMENT 则执行跳转 */ if(msg == AppConstant.PlayerMsg.CHANGE_TO_MY_MUSIC_FRAGMENT){ /* 在这里并没有直接切换Fragment 而是调用了activity实现MyMusicFragment的那个接口 对后面的开发能带来一点便利之处 */ onMyMusicFragmentInteraction (AppConstant.PlayerMsg.CHANGE_TO_MY_MUSIC_FRAGMENT); } }
public void onMyMusicFragmentInteraction(int msg){
myMusicFragment = new MyMusicFragment(MainActivity.this); //创建了MyMusicFragment的实例 FragmentManager fragmentManager = getFragmentManager(); //得到FragmentManager FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); //得到fragmentTransaction if(msg == AppConstant.PlayerMsg.CHANGE_TO_MY_MUSIC_FRAGMENT){ fragmentTransaction.replace(R.id.fragment_layout, myMusicFragment); fragmentTransaction.addToBackStack(null); //这句话是将被替换的MainFragment加入到一个专门存放fragment的栈中,在回退的时候显示上一个Fragment fragmentTransaction.commit(); } if(msg == AppConstant.PlayerMsg.BACK_TO_MAIN_FRAGMENT){ fragmentTransaction.replace(R.id.fragment_layout, mainFragment); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); } }
public void onMyMusicFragmentInteraction(int msg,int position){
if(msg == AppConstant.PlayerMsg.LIST_CLICK){ if (mp3Infos != null) { // Log.i("dada", "-------------here------------"); isPause = false; initService(position); } }
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);music_position = 0; finder = new FindSongs(); mp3Infos = finder.getMp3Infos(getContentResolver()); play_button = (ImageButton)findViewById(R.id.play_button); //mainFragment = new MainFragment(); //创建了刚才定义的MainFragment实例 mainFragment = new MainFragment(MainActivity.this); fragmentManager = getFragmentManager(); //得到FragmentManager fragmentTransaction = fragmentManager.beginTransaction(); //得到fragmentTransaction,用于管理fragment的切换 fragmentTransaction.replace(R.id.fragment_layout, mainFragment).commit(); //将MainActivity里的布局模块fragment_layout替换为mainFragment } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } private void initService(int position) { music_position = position; Mp3Info mp3Info = mp3Infos.get(position); /* 这里新建了一个Intent 里面存放各种即将传给Service的数据 要启动自定义PlayerService类 还需要在AndroidManifest中加入如下代码 <service android:name="com.example.dada.myapplication.PlayerService" android:exported="false" > </service> */ //Intent intent = new Intent("com.example.service.PlayerService"); Intent intent = new Intent(MainActivity.this, PlayerService.class); play_button.setImageResource(R.drawable.play_pause); intent.putExtra("url", mp3Info.getUrl()); intent.putExtra("title", mp3Info.getTitle()); intent.putExtra("artist", mp3Info.getArtist()); intent.putExtra("album", mp3Info.getAlbum()); intent.putExtra("album_id", mp3Info.getAlbum_id()); intent.putExtra("MSG", AppConstant.PlayerMsg.PLAY_MSG); intent.setClass(MainActivity.this, PlayerService.class); startService(intent); Log.w("dada", "------------hahaha----------"); } //@Override
// public void onFragmentInteraction(int msg) {
// // TODO Auto-generated method stub
//// }
}service代码
package com.example.service;
import java.io.IOException;
import com.example.test_01.AppConstant;
import com.example.test_01.AppConstant.PlayerMsg;import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;public class PlayerService extends Service implements AppConstant {
public static MediaPlayer mediaPlayer = null; private int current_position; private String musicPath; private String music_artist; private String music_title; private String notification_msg; //private MediaPlayer mediaPlayer; @Override public IBinder onBind(Intent intent) { Log.w("dada", "-------------here------------"); return null; } /* * onStartCommand()方法就是刚启动service时调用的一个方法 * 里面第一个参数intent就是在activity中的那个intent * 因此里面包含着被点击的歌曲相关信息 */ @Override public int onStartCommand(Intent intent, int flags, int startId) { //MediaPlayer是android中自带的一个播放器类,直接实例化后使用即可 mediaPlayer = new MediaPlayer(); Log.w("dada", "-------------here------------"); try { int msg = intent.getIntExtra("MSG", 0); musicPath = intent.getStringExtra("url"); //从intent中拿出歌曲的路径 Log.w("dada", "-------------here------------"); if (msg == AppConstant.PlayerMsg.PLAY_MSG) { playMusic(0); } } catch (Exception e) { e.printStackTrace(); } return 0; } private void playMusic(int position) { Log.w("dada", "-------------here------------"); try { mediaPlayer.reset(); mediaPlayer.setDataSource(musicPath); mediaPlayer.prepare(); mediaPlayer.setOnPreparedListener(new MyPreparedListener(position)); } catch (IOException e) { e.printStackTrace(); } } private class MyPreparedListener implements MediaPlayer.OnPreparedListener { private int position; public MyPreparedListener(int position) { this.position = position; } public void onPrepared(MediaPlayer mp) { if (position > 0) Log.i("dada", "-------------here------------"); mediaPlayer.seekTo(position); mediaPlayer.start(); } } private void stopMusic() { if (mediaPlayer != null) { mediaPlayer.pause(); } } public void onDestory() { Log.e("dada", "-------------here------------"); if (mediaPlayer != null) { mediaPlayer.stop(); mediaPlayer.release(); } } }
startService(intent);后面的log显示了,但是service却没有反应
解决方案
按理startService应该是经过onStartCommand的,你override一下oncreate,里面加一下日志试试,看create了吗