我一直在創建自己的音樂播放器。當我點擊列表項時,我的視圖凍結1到2秒,我如何防止我的視圖凍結。Android查看點擊凍結並且不響應1到2秒
我已經試過asynctak,但它掛起,反正ActivityUtil.goPlayHome_Ser(getActivity(), finalI, gridType);
只是調用服務只。
我幾乎完成了我的音樂播放器應用程序,但在這一個擊中,需要一些幫助。謝謝。
功能
public static void goPlayHome_Ser(Context context, int position, String gridtype) {
Intent intent = new Intent(context, MediaService.class);
intent.setAction(MediaService.ACTION_PLAY_HOMEMAP + "");
intent.putExtra(Common.BUNDLE_SONG_POSITION, position);
intent.putExtra(Common.BUNDLE_MUSIC_LIST_TYPE, gridtype);
context.startService(intent);
}
MyRecycler適配器
private class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> {
private HashMap<String, List<Music>> map;
private Context mContext;
private List<String> headerList;
public HomeAdapter(Context context, HashMap<String, List<Music>> musicList) {
this.map = musicList;
this.mContext = context;
headerList = new ArrayList<>();
for (Map.Entry<String, List<Music>> entry : map.entrySet()) {
headerList.add(entry.getKey());
}
/*Collections.sort(headerList, new Comparator<String>() {
@Override
public int compare(String lhs, String rhs) {
return lhs.compareTo(rhs);
}
});*/
if (headerList.remove(Common.HOME_LIST_PLAYLIST)) {
headerList.add(Common.HOME_LIST_PLAYLIST);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_home, null);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final List<Music> musics = map.get(headerList.get(position));
holder.title.setText(headerList.get(position));
addGrids(mContext, holder, musics, headerList.get(position));
holder.gridMore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (headerList.get(position)) {
case Common.HOME_LIST_ALBUM:
PopupUtil.homeAlbumsPopup(getActivity(), view, musics);
break;
case Common.HOME_LIST_LATEST:
//PopupUtil.homeLatestPopup(getActivity(), view, musics);
PopupUtil.homeSongsPopup(getActivity(), view, Common.HOME_LIST_LATEST, musics);
break;
case Common.HOME_LIST_ALL_SONGS:
//PopupUtil.homeAllSongsPopup(getActivity(), view, musics);
PopupUtil.homeSongsPopup(getActivity(), view, Common.HOME_LIST_ALL_SONGS, musics);
break;
case Common.HOME_LIST_MOST_PLAYED:
PopupUtil.homeMostPlayedPopup(getActivity(), view, musics);
break;
case Common.HOME_LIST_PLAYLIST:
PopupUtil.homePlayListPopup(getActivity(), view, musics);
break;
}
}
});
}
@Override
public int getItemCount() {
return headerList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
private TextView title;
private ImageView gridMore;
private LinearLayout grids;
public ViewHolder(View view) {
super(view);
this.title = (TextView) view.findViewById(R.id.adapter_home_grid_title);
this.gridMore = (ImageView) view.findViewById(R.id.adapter_home_grid_more);
this.grids = (LinearLayout) view.findViewById(R.id.adapter_home_grid);
}
}
private void addGrids(final Context context, ViewHolder holder, final List<Music> musics, final String gridType) {
holder.grids.removeAllViews();
int maxViewCount = musics.size() < 10 ? musics.size() : 10;
for (int i = 0; i < maxViewCount; i++) {
final int position = i;
View view = LayoutInflater.from(context).inflate(R.layout.view_custom_home_grid, null);
TextView gridTitle, gridArtist;
ImageView gridThumb;
gridTitle = (TextView) view.findViewById(R.id.home_grid_name);
gridArtist = (TextView) view.findViewById(R.id.home_grid_artist);
gridThumb = (ImageView) view.findViewById(R.id.home_grid_thumb);
switch (gridType) {
case Common.HOME_LIST_ALBUM:
gridTitle.setText(musics.get(i).getAlbum());
gridArtist.setText(musics.get(i).getArtist());
break;
case Common.HOME_LIST_ALL_SONGS:
gridTitle.setText(musics.get(i).getName());
gridArtist.setText(musics.get(i).getArtist());
break;
case Common.HOME_LIST_LATEST:
gridTitle.setText(musics.get(i).getName());
gridArtist.setText(musics.get(i).getArtist());
break;
case Common.HOME_LIST_MOST_PLAYED:
gridTitle.setText(musics.get(i).getName());
gridArtist.setText(musics.get(i).getArtist());
break;
case Common.HOME_LIST_PLAYLIST:
gridTitle.setText(musics.get(i).getPlaylist_name());
gridArtist.setText(musics.get(i).getArtist());
break;
}
final int finalI = i;
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ActivityUtil.goPlayHome_Ser(getActivity(), finalI, gridType);
}
});
ImageLoader.getInstance().displayImage(Util.getContentUri(musics.get(i).getAlbum_id()), gridThumb);
holder.grids.addView(view);
}
}
}
MediaService
public class MediaService extends Service implements MediaPlayer.OnErrorListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnBufferingUpdateListener, MediaPlayer.OnCompletionListener, AudioManager.OnAudioFocusChangeListener {
private static final String TAG = "MediaService";
public static final String BROADCAST_ACTION = "_broadcast_action";
public static final String BROADCAST_TYPE = "_broadcast_type";
public static final int ACTION_INITIALIZE = 1000;
public static final int ACTION_PLAY = 1001;
public static final int ACTION_PAUSE = 1002;
public static final int ACTION_TOGGLE = 1003;
public static final int ACTION_SEEK = 1004;
public static final int ACTION_PREV = 1006;
public static final int ACTION_NEXT = 1007;
public static final int ACTION_NOIFY_SHOW = 1008;
public static final int ACTION_NOIFY_HIDE = 1009;
public static final int ACTION_QUEUE = 1010;
public static final int ACTION_PLAY_NEXT = 1011;
public static final int ACTION_QUEUE_REMOVE_CURRENT_SONG = 1012;
public static final int ACTION_QUEUE_CHANGE_SONG = 1013;
public static final int ACTION_STORAGE_REMOVE_CURRENT_SONG = 1014;
public static final int ACTION_INIIALIZE_HOMEMAP = 1015;
public static final int ACTION_PLAY_HOMEMAP = 1016;
public static final int MUSIC_INITIALIZE_REPLY = 2000;
public static final int MUSIC_PLAY_REPLY = 2001;
public static final int MUSIC_PAUSE_REPLY = 2002;
public static final int MUSIC_SEEK_REPLY = 2003;
public static final int MUSIC_ERROR_REPLY = 2004;
public static final int MUSIC_QUEUELIST_REPLY = 2005;
public static final int MUSIC_HOMEFRAG_REPLY = 2006;
public static final int FOREGROUND_NOTIFICATION_ID = 3000;
private MediaPlayer mediaPlayer;
private AudioManager audioManager;
private Handler handler;
private Mediaobj mediaobj;
private NotificationCompat.Builder builder;
private NotificationManager notificationManager;
private HashMap<String, List<Music>> mHomeMap;
@Override
public void onCreate() {
super.onCreate();
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
handler = new Handler();
Log.d(TAG, "onCreate called");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int action = Integer.parseInt(intent.getAction());
Log.d(TAG, "onStartCommand -> " + String.valueOf(action));
switch (action) {
case ACTION_INITIALIZE:
if (mediaPlayer == null) {
Log.d(TAG, "Mediaplayer Initializing");
initializeMediaPlayer();
initializeMediaObj();
} else {
Log.d(TAG, "Mediaplayer already initialized");
initalizeReply();
}
break;
case ACTION_PLAY:
mediaobj.queueList.clear();
Bundle bundle = intent.getBundleExtra(Common.BUNDLE_INTENT);
List<Music> queues = (List<Music>) bundle.getSerializable(Common.BUNDLE_MUSIC_LIST_DATA);
if (PrefUtil.getShuffleIcon(getApplicationContext()) == R.drawable.ic_action_shuffle) {
Collections.shuffle(queues);
}
mediaobj.queueList = queues;
mediaobj.songPosition = 0;
mediaobj.music = mediaobj.queueList.get(mediaobj.songPosition);
play();
Dbhelper.getInstance(getApplicationContext()).removeQueues();
Dbhelper.getInstance(getApplicationContext()).addQueues(mediaobj.queueList);
break;
case ACTION_PAUSE:
pause();
break;
case ACTION_TOGGLE:
togglePlay();
break;
case ACTION_SEEK:
int seekPos = intent.getExtras().getInt(Common.BUNDLE_MUSIC_SEEK_POS);
mediaPlayer.seekTo(seekPos);
PrefUtil.saveMusicResumePosition(getApplicationContext(), mediaPlayer.getCurrentPosition());
seekReply();
break;
case ACTION_PREV:
if (mediaobj.hasPrev) {
mediaobj.songPosition--;
Dbhelper.getInstance(getApplicationContext()).updateQueueSongFlag(mediaobj.music.get_id(), false);
mediaobj.music = mediaobj.queueList.get(mediaobj.songPosition);
}
play();
break;
case ACTION_NEXT:
if (mediaobj.hasNext) {
mediaobj.songPosition++;
Dbhelper.getInstance(getApplicationContext()).updateQueueSongFlag(mediaobj.music.get_id(), false);
mediaobj.music = mediaobj.queueList.get(mediaobj.songPosition);
/*if (mediaPlayer.isPlaying()) {
play();
} else {
play();
pause();
}*/
play();
}
break;
case ACTION_NOIFY_SHOW:
createRemoteNotification(true);
break;
case ACTION_NOIFY_HIDE:
createRemoteNotification(false);
break;
case ACTION_QUEUE:
if (intent.getSerializableExtra(Common.BUNDLE_MUSIC_LIST_DATA) != null) {
List<Music> musics = (List<Music>) intent.getSerializableExtra(Common.BUNDLE_MUSIC_LIST_DATA);
if (mediaobj != null && mediaobj.queueList != null && musics != null) {
mediaobj.queueList.addAll(musics);
}
}
Dbhelper.getInstance(getApplicationContext()).removeQueues();
Dbhelper.getInstance(getApplicationContext()).addQueues(mediaobj.queueList);
queueReply();
break;
case ACTION_PLAY_NEXT:
if (intent.getSerializableExtra(Common.BUNDLE_MUSIC_LIST_DATA) != null) {
List<Music> musics = (List<Music>) intent.getSerializableExtra(Common.BUNDLE_MUSIC_LIST_DATA);
if (mediaobj != null && mediaobj.queueList != null && musics != null) {
int songPos = mediaobj.songPosition;
for (Music music : musics) {
songPos++;
mediaobj.queueList.add(mediaobj.songPosition + 1, music);
}
}
queueReply();
}
Dbhelper.getInstance(getApplicationContext()).removeQueues();
Dbhelper.getInstance(getApplicationContext()).addQueues(mediaobj.queueList);
break;
case ACTION_QUEUE_REMOVE_CURRENT_SONG:
Dbhelper.getInstance(getApplicationContext()).removeQueue(String.valueOf(mediaobj.music.get_id()));
mediaobj.queueList.remove(mediaobj.songPosition);
mediaobj.music = mediaobj.queueList.get(mediaobj.songPosition);
if (mediaPlayer.isPlaying()) {
play();
} else {
pause();
}
Dbhelper.getInstance(getApplicationContext()).updateQueueSongFlag(mediaobj.music.get_id(), true);
break;
case ACTION_QUEUE_CHANGE_SONG:
int songPosition = intent.getIntExtra(Common.BUNDLE_SONG_POSITION, mediaobj.songPosition);
if (songPosition == mediaobj.songPosition) {
togglePlay();
} else {
mediaobj.songPosition = songPosition;
mediaobj.music = mediaobj.queueList.get(mediaobj.songPosition);
play();
}
break;
case ACTION_STORAGE_REMOVE_CURRENT_SONG:
String path = mediaobj.music.getPath();
Dbhelper.getInstance(getApplicationContext()).removeQueue(String.valueOf(mediaobj.music.get_id()));
mediaobj.queueList.remove(mediaobj.songPosition);
mediaobj.music = mediaobj.queueList.get(mediaobj.songPosition);
if (mediaPlayer.isPlaying()) {
play();
} else {
pause();
}
Dbhelper.getInstance(getApplicationContext()).updateQueueSongFlag(mediaobj.music.get_id(), true);
Util.removeMp3FromStorage(path);
queueReply();
break;
case ACTION_INIIALIZE_HOMEMAP:
if (mHomeMap == null) {
mHomeMap = Util.getHomeHash(getApplicationContext());
}
Intent homeMapIntent = new Intent(BROADCAST_ACTION);
homeMapIntent.putExtra(BROADCAST_TYPE, MUSIC_HOMEFRAG_REPLY);
homeMapIntent.putExtra(Common.BUNDLE_HOMEFRAG_MAP, mHomeMap);
sendBroadcastMsg(homeMapIntent);
break;
case ACTION_PLAY_HOMEMAP: {
if (mHomeMap != null) {
int position = intent.getIntExtra(Common.BUNDLE_SONG_POSITION, 0);
String gridType = intent.getStringExtra(Common.BUNDLE_MUSIC_LIST_TYPE);
if (!TextUtils.equals(gridType, Common.HOME_LIST_PLAYLIST) && !TextUtils.equals(gridType, Common.HOME_LIST_ALBUM)) {
List<Music> musicList = mHomeMap.get(gridType);
mediaobj.queueList.clear();
mediaobj.queueList.addAll(musicList.subList(position, musicList.size() - 1));
mediaobj.music = mediaobj.queueList.get(0);
mediaobj.songPosition = 0;
play();
Dbhelper.getInstance(getApplicationContext()).removeQueues();
Dbhelper.getInstance(getApplicationContext()).addQueues(mediaobj.queueList);
}
}
}
default: {
}
}
return Service.START_NOT_STICKY;
}}
這聽起來像你正在做的事情,像或許加載一個大文件或訪問網絡,在主(UI)線程時,它應該作爲的AsyncTask(或類似的東西來處理backgroundish)。這是一個常見問題,但是如果沒有看到你的代碼,那麼任何人都不可能提供幫助。 – kungphu
顯示一些代碼片段。 UI線程上的任何長時間運行操作都會導致此問題。嘗試在工作線程中執行您的操作。建議AsyncTask或你自己的線程實現 –