安卓第三次作业
关键代码:
//启动服务器 Intent mintent=new Intent(); mintent.setClass(MainActivity.this, MyService.class); startService(mintent); zhuce(); list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub isplay=true; playm.setImageResource(R.drawable.dainji_20); showgeci.setText("歌词"); Intent intent=new Intent(); intent.setAction("ACTION_INDEX"); intent.putExtra("index", arg2); sendBroadcast(intent); } }); // se.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub if(fromUser==true) { Intent intent=new Intent(); intent.setAction("ACTION_SEEKBAR"); intent.putExtra("seekbar", progress); sendBroadcast(intent); } } }); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.shang: nextmusic(0); lastm.setImageResource(R.drawable.dianji_22); break; case R.id.xia: nextmusic(1); nextm.setImageResource(R.drawable.dianji_25); break; case R.id.geci: isgeci=!isgeci; if(isgeci==true){ geci.setTextColor(Color.rgb(255, 255, 0)); showgeci.setVisibility(View.VISIBLE); }else{ geci.setTextColor(Color.rgb(255, 255, 255)); showgeci.setVisibility(View.GONE); } break; case R.id.line: check++; if(check>3) check=1; if(check==1){ style.setImageResource(R.drawable.dianji_28); stylemusic(1); } if(check==2){ style.setImageResource(R.drawable.dianji1_28); stylemusic(2); } if(check==3){ style.setImageResource(R.drawable.pic2_28); stylemusic(3); } break; case R.id.play: isplay=!isplay; if(isplay==true) { playm.setImageResource(R.drawable.dainji_20); }else{ playm.setImageResource(R.drawable.dianji_19); } musicplay(isplay); break; default: break; } } //发送,播放,暂停广播 public void musicplay(boolean isplay){ Intent intent=new Intent(); intent.setAction("ACTION_ISPLAY"); intent.putExtra("isplay",isplay); sendBroadcast(intent); } //上一曲,下一曲,0上一曲,1下一曲 public void nextmusic(int num){ showgeci.setText("歌词"); Intent intent=new Intent(); intent.setAction("ACTION_NEXT"); intent.putExtra("next",num); sendBroadcast(intent); } public void stylemusic(int num){ Intent intent=new Intent(); intent.setAction("ACTION_STYLE"); intent.putExtra("check",num); sendBroadcast(intent); } class Mybroad extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub if(intent.getAction().equals("ACTION_MAXTIME")){ int maxtime=intent.getIntExtra("maxtime", 0); se.setMax(maxtime); time2.setText(""+settime(maxtime)); } if(intent.getAction().equals("ACTION_NOWTIME")){ int nowtime=intent.getIntExtra("nowtime", 0); se.setProgress(nowtime); time.setText(""+settime(nowtime)); } if(intent.getAction().equals("ACTION_LRC")){ String lrc=intent.getStringExtra("geci"); showgeci.setText(""+lrc); } if(intent.getAction().equals("ACTION_NOWMUSIC")){ String nowmusic1=intent.getStringExtra("nowmusic"); nowmusic.setText("正在播放:"+nowmusic1); } } }
界面:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/pi2" android:orientation="vertical"> <RelativeLayout android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="3" android:background="#7C6C48"> <ImageView android:id="@+id/img_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="18dp" android:src="@drawable/pic_03" /> <ImageView android:id="@+id/img_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="18dp" android:src="@drawable/pic_05" /> <TextView android:id="@+id/nowmusic" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:maxLines="1" android:text="播放" android:textColor="#F8FFFF" /> </RelativeLayout> <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="16"></ListView> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/showgeci" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:maxLines="1" android:text="歌词" android:textColor="#FFFF21" android:textSize="20dp" /> </RelativeLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="2" android:background="#988867"> <TextView android:id="@+id/sec" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:text="00:00" android:textColor="#FFFFFF" /> <TextView android:id="@+id/min" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:text="00:00" android:textColor="#FFFFFF" /> <SeekBar android:id="@+id/se" android:layout_width="fill_parent" android:layout_height="13dp" android:layout_centerVertical="true" android:layout_toLeftOf="@id/min" android:layout_toRightOf="@id/sec" android:maxHeight="2dp" android:thumb="@drawable/pic_14" android:thumbOffset="13dp" /> </RelativeLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="4" android:background="#7C6C48"> <ImageView android:id="@+id/play" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:src="@drawable/dianji_19" /> <ImageView android:id="@+id/shang" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginRight="18dp" android:layout_toLeftOf="@id/play" android:src="@drawable/dianji_22" /> <ImageView android:id="@+id/xia" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="18dp" android:layout_toRightOf="@id/play" android:src="@drawable/dianji_25" /> <TextView android:id="@+id/geci" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:text="歌词" android:textColor="#FFFFFF" android:textSize="18sp" /> <ImageView android:id="@+id/line" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="8dp" android:layout_toRightOf="@id/xia" android:src="@drawable/pic_28" /> </RelativeLayout> </LinearLayout>