自定义适配器
public class LAdapter extends BaseAdapter{
//创建属性
private Context context;
//对象
private ArrayList<Music> list;
//有参构造
public LAdapter(Context context, ArrayList<Music> list) {
super();
this.context = context;
this.list = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//优化
ViewHolder holder=null;
if(convertView==null){
holder=new ViewHolder();
convertView = View.inflate(context, R.layout.list, null);
holder.image=(ImageView) convertView.findViewById(R.id.image1);
holder.name=(TextView) convertView.findViewById(R.id.name1);
holder.singer=(TextView) convertView.findViewById(R.id.singer1);
convertView.setTag(holder);
}else{
holder=(ViewHolder) convertView.getTag();
}
Music music = list.get(position);
holder.image.setImageResource(music.getImage1());
holder.name.setText(music.getName());
holder.singer.setText(music.getSinger());
return convertView;
}
//自定义viewholder类
class ViewHolder{
ImageView image;
TextView name,singer;
}
}
自定义Music类
public class Music implements Serializable{
private int image1;
private String name;
private String singer;
private File file;
public Music() {
super();
// TODO Auto-generated constructor stub
}
public Music(int image1, String name, String singer, File file) {
super();
this.image1 = image1;
this.name = name;
this.singer = singer;
this.file = file;
}
public int getImage1() {
return image1;
}
public void setImage1(int image1) {
this.image1 = image1;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSinger() {
return singer;
}
public void setSinger(String singer) {
this.singer = singer;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
@Override
public String toString() {
return "Music [image1=" + image1 + ", name=" + name + ", singer="
+ singer + ", file=" + file + "]";
}
}
自定义广播类
//开机自启
public class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
//进入方法
if("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())){
//创建intent
Intent data = new Intent(context,MainActivity.class);
data.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//跳转
context.startActivity(data);
}
}
}
Mainactivity
public class MainActivity extends Activity {
private ImageView image;
private TextView title1;
private Button tiao;
int t = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找控件
image = (ImageView) findViewById(R.id.image);
title1 = (TextView) findViewById(R.id.tit);
tiao = (Button) findViewById(R.id.tiao);
// 设置监听
tiao.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//过场动画
overridePendingTransition(R.anim.aanim, 1);
overridePendingTransition(android.R.anim.fade_in,
android.R.anim.fade_out);
overridePendingTransition(android.R.anim.slide_in_left,
android.R.anim.slide_out_right);
t = 0;
}
});
// 动画
Animation an = AnimationUtils.loadAnimation(MainActivity.this,
R.anim.aanim);
image.startAnimation(an);
handler.sendEmptyMessage(0);
}
// handler
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 0:
// 设置控件值
title1.setText(t + "s");
t--;
if (t <= 0) {
startActivity(new Intent(MainActivity.this,
TwoActivity.class));
return;
}
handler.sendEmptyMessageDelayed(0, 1000);
break;
default:
break;
}
};
};
}
TwoActivity
public class TwoActivity extends Activity {
private ListView listview;
private Music music;
private ArrayList<Music> list;
private LAdapter aa;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
// 找控件
listview = (ListView) findViewById(R.id.listview);
// 创建数据
list = new ArrayList<Music>();
for (int i = 0; i < 12; i++) {
File file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath(), "ge" + i + ".mp3");
if (i % 2 == 0) {
music = new Music(R.drawable.liuruoying, "后来", "歌手:刘若英", file);
} else {
music = new Music(R.drawable.xietingfeng, "因为爱所以爱", "歌手:谢霆锋",
file);
}
list.add(music);
}
// 适配器
aa = new LAdapter(TwoActivity.this, list);
// 添加适配器
listview.setAdapter(aa);
// 点击事件
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
//跳转
Intent intent = new Intent(TwoActivity.this,
ThreeActivity.class);
intent.putExtra("position", position);
intent.putExtra("list", list);
startActivityForResult(intent, 1);
}
});
// 长安事件
listview.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
final int position, long id) {
// TODO Auto-generated method stub
// 创建弹框
AlertDialog.Builder builder = new AlertDialog.Builder(
TwoActivity.this);
View shanchu = View.inflate(TwoActivity.this, R.layout.shanchu,
null);
final AlertDialog dialog = builder.setView(shanchu).create();
dialog.show();
// 找控件
Button queding = (Button) shanchu.findViewById(R.id.queding);
Button cancel = (Button) shanchu.findViewById(R.id.quxiao);
TextView bb = (TextView) shanchu.findViewById(R.id.bb);
TextView cc = (TextView) shanchu.findViewById(R.id.cc);
bb.setText(list.get(position).getName());
cc.setText(list.get(position).getSinger());
// 确定按钮
queding.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
list.remove(position);
aa.notifyDataSetChanged();
dialog.dismiss();
}
});
// 取消按钮
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
return true;
}
});
}
}
ThreeActivity
public class ThreeActivity extends Activity implements OnClickListener {
private ImageView hui;
private ImageView play;
private ImageView up;
private ImageView down;
private ImageView huitu;
private TextView listing;
private SeekBar bar;
private MediaPlayer player = new MediaPlayer();
private int position;
private ArrayList list2;
int a = 0;
private Music music;
boolean isplay = false;
boolean is = false;
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
player.stop();
handler.removeMessages(0);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_three);
// 找控件
// 进度条
bar = (SeekBar) findViewById(R.id.seekBar);
// 回传的图标
hui = (ImageView) findViewById(R.id.hui);
// 回传的背景图片
huitu = (ImageView) findViewById(R.id.huitu);
// 暂停
play = (ImageView) findViewById(R.id.play);
// 上一曲
up = (ImageView) findViewById(R.id.up);
// 下一曲
down = (ImageView) findViewById(R.id.down);
// 找到intent的对象
Intent intent = getIntent();
// 找到集合
list2 = (ArrayList<Music>) intent.getSerializableExtra("list");
// 索引
position = intent.getIntExtra("position", -1);
// 找到对象
music = list2.get(position);
// 附图片
listing = (TextView) findViewById(R.id.listing);
// 设置内容
listing.setText(music.getName());
// 设置监听
hui.setOnClickListener(this);
play.setOnClickListener(this);
up.setOnClickListener(this);
down.setOnClickListener(this);
bar.setOnClickListener(this);
// 监听事件
bar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
// 判断是否正在播放
if (player.isPlaying()) {
player.seekTo(bar.getProgress());
} else {
bar.getProgress();
}
}
@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
}
});
}
// handler处理机制
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 0:
// 设置player的进度值
bar.setProgress(player.getCurrentPosition());
// 判断是否最大
if (bar.getMax() == player.getCurrentPosition()) {
return;
}
// 发送延迟
handler.sendEmptyMessageDelayed(0, 1000);
break;
default:
break;
}
};
};
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 创建对象
Music music2 = null;
// boolean isplay = false;
switch (v.getId()) {
// 回转
case R.id.hui:
// 创建对象
Intent data = new Intent(ThreeActivity.this, TwoActivity.class);
// 找到控件
String nn = listing.getText().toString();
data.putExtra("nn", nn);
// 开始跳转
startActivity(data);
break;
// 正在播放
case R.id.play:
music2 = list2.get(position);
// 设置播放路径
// 是否正在播放
if (player.isPlaying()) {
play.setImageResource(R.drawable.play);
player.pause();
player.reset();
handler.removeMessages(0);
return;
}
try {
//设置路径
player.setDataSource(music2.getFile().getAbsolutePath());
if(!is){
//准备
player.prepare();
// 开始
player.start();
isplay=true;
}else{
// 开始
player.start();
}
// 传索引
a = position;
// 设置
Toast.makeText(ThreeActivity.this, "" + isplay, 0).show();
// 设置按钮的背景图片
play.setImageResource(R.drawable.pause_n);
// 设置背景图片
huitu.setImageResource(music.getImage1());
// 设置进度
bar.setProgress(0);
// 最大进度
bar.setMax(player.getDuration());
// 发送
handler.sendEmptyMessage(0);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
break;
// 下一曲
case R.id.down:
if (position == list2.size() - 1) {
Toast.makeText(ThreeActivity.this, "没有新专辑", 0).show();
return;
}
// 判断是否正在播放
if (player.isPlaying()) {
player.stop();
player.reset();
}
// 索引加1
a++;
try {
// 得到对象
music2 = list2.get(a);
// 播放 路径
player.setDataSource(music2.getFile().getAbsolutePath());
// 准备
player.prepare();
// 开始
player.start();
// 设置图片索引
huitu.setImageResource(music2.getImage1());
play.setImageResource(R.drawable.pause_n);
// 点击
listing.setText(music2.getName());
// 当前进度
bar.setProgress(0);
bar.setMax(player.getDuration());
handler.sendEmptyMessage(0);
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
// 上一曲
case R.id.up:
if (position == 0) {
Toast.makeText(ThreeActivity.this, "没有上一曲", 0).show();
return;
}
if (player.isPlaying()) {
// 停止
player.stop();
// 重置
player.reset();
}
// 索引减去1
a--;
try {
// 设置播放路径
// 得到对象
music2 = list2.get(a);
// 播放 路径
player.setDataSource(music2.getFile().getAbsolutePath());
// 准备
player.prepare();
// 开始
player.start();
// 设置背景图片
huitu.setImageResource(music2.getImage1());
play.setImageResource(R.drawable.pause_n);
listing.setText(music2.getName());
// 初始值
bar.setProgress(0);
// 最大值
bar.setMax(player.getDuration());
// 发送
handler.sendEmptyMessage(0);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
default:
break;
}
}
}
权限
注册广播
创建anim文件夹 创建anim.xml
<?xml version="1.0" encoding="utf-8"?>activity_main.xml
<Button
android:id="@+id/tiao"
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text="立即体验"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
/>
activity_two.xml
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical”
activity_three.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f00"
android:orientation="horizontal" >
<ImageView
android:id="@+id/hui"
android:layout_width="100dp"
android:layout_height="50dp"
android:src="@drawable/hui" />
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center"
android:text="播放界面" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginRight="20dp"
android:text="正在播放:" />
<TextView
android:id="@+id/listing"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="正在播放" />
</LinearLayout>
<ImageView
android:id="@+id/huitu"
android:layout_marginLeft="200dp"
android:layout_marginTop="100dp"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/liuruoying" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical" >
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="@+id/up"
android:layout_width="45dp"
android:layout_height="45dp"
android:src="@drawable/left" />
<ImageView
android:id="@+id/play"
android:layout_width="45dp"
android:layout_height="45dp"
android:src="@drawable/play" />
<ImageView
android:id="@+id/down"
android:layout_width="45dp"
android:layout_height="45dp"
android:src="@drawable/right" />
</LinearLayout>
</LinearLayout>
list.xml
<?xml version="1.0" encoding="utf-8"?><ImageView
android:id="@+id/image1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/liuruoying"
/>
<TextView
android:layout_marginTop="10dp"
android:id="@+id/name1"
android:text="后来"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:layout_marginTop="10dp"
android:id="@+id/singer1"
android:text="歌手:刘若英"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
shanchu.xml
<?xml version="1.0" encoding="utf-8"?><TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定要删除吗"
android:textColor="#00f"
android:textSize="25sp" />
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#0f0" />
<TextView
android:id="@+id/bb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="后来"
android:textColor="#00f"
android:textSize="25sp" />
<TextView
android:id="@+id/cc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="刘若英"
android:textColor="#00f"
android:textSize="25sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/quxiao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="取消" />
<Button
android:id="@+id/queding"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="确定" />
</LinearLayout>
title.xml
<?xml version="1.0" encoding="utf-8"?>