package com.service;
import java.io.File;
import java.io.IOException;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.os.Vibrator;
import android.widget.RemoteViews;
import android.widget.Toast;
import com.data.Data;
import com.mymusictwo.MusicWidget;
import com.mymusictwo.R;
public class MusicService extends Service {
/**
* onBind 是 Service 的虚方法,因此我们不得不实现它。
* 返回 null,表示客服端不能建立到此服务的连接。
*/
MyReceiver receive = null;
private MediaPlayer myPlayer1 = null;
private Vibrator mVibrator01 =null;
private int countTime = 0;
private int virbrateTime =0;
@Override
public IBinder onBind(Intent intent) {
return null;
}
class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent arg1) {
// TODO Auto-generated method stub
if(arg1.getAction().equals("click")){
//Toast.makeText(context, "ha", Toast.LENGTH_SHORT).show();
new MusicMode(context).start();
}else if(arg1.getAction().equals("end")){
Toast.makeText(context, "end", Toast.LENGTH_SHORT).show();
}else if(arg1.getAction().equals(Intent.ACTION_TIME_TICK)){
//Toast.makeText(context, "timechange", Toast.LENGTH_SHORT).show();
if(Data.play){
countTime++;
int setTime = Integer.parseInt(Data.readShare(context, 0, Data.play_time));
if(countTime>=setTime){
countTime = 0;
try{
closeMusic(context);
}catch(Exception exception){
}
}
}else{
countTime = 0;
}
if(Data.virbrate){
virbrateTime++;
int setVirbate =Integer.parseInt(Data.readShare(context, 0, Data.clock_time));
if(virbrateTime>=setVirbate){
openVirbrate();
updateVirbrateIcon(context, true);
if(virbrateTime>=setVirbate+5){
virbrateTime=0;
closeVirbrate();
updateVirbrateIcon(context,false);
}
}
}else{
virbrateTime=0;
updateVirbrateIcon(context, false);
}
}else if(arg1.getAction().equals("notplay")){
Toast.makeText(context, "请确认歌曲是否存在?", Toast.LENGTH_SHORT).show();
}else if(arg1.getAction().equals("virbrate")){
//Toast.makeText(context, "virbrate", Toast.LENGTH_SHORT).show();
String str = Data.readShare(context, 0, Data.clock_time);
if(str.length()==0){
Toast.makeText(context, "请设置振动时间", Toast.LENGTH_SHORT).show();
}else{
if(mVibrator01 !=null){
if(Data.virbrate){
virbrateTime = 0;
closeVirbrate();
updateVirbrateIcon(context, false);
}else{
virbrateTime = 0;
Data.virbrate = true;
//openVirbrate();
updateVirbrateIcon(context, true);
}
}else{
mVibrator01 = ( Vibrator )getApplication().getSystemService( Service .VIBRATOR_SERVICE );
Data.virbrate = false;
updateVirbrateIcon(context, false);
}
}
}
}
}
@Override
public void onCreate() {
super.onCreate();
if(receive == null){
receive = new MyReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("click");
filter.addAction("virbrate");
filter.addAction("notplay");
filter.addAction("end");
filter.addAction(Intent.ACTION_TIME_TICK);
this.registerReceiver(receive, filter);
}
Data.virbrate = false;
if(myPlayer1 == null){
myPlayer1 = new MediaPlayer();
}
mVibrator01 = ( Vibrator )getApplication().getSystemService( Service .VIBRATOR_SERVICE );
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Intent it =new Intent("end");
this.sendBroadcast(it);
if(receive != null){
this.unregisterReceiver(receive);
}
}
private void updatePlayIcon(Context context,boolean play){
if(play){
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.music_widget);
views.setImageViewResource(R.id.imageButton1, R.drawable.stop_icon);
ComponentName thisWidget = new ComponentName(context,MusicWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(thisWidget, views);
}else{
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.music_widget);
views.setImageViewResource(R.id.imageButton1, R.drawable.play_icon);
ComponentName thisWidget = new ComponentName(context,MusicWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(thisWidget, views);
}
}
private void updateVirbrateIcon(Context context,boolean virbrate){
if(virbrate){
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.music_widget);
views.setImageViewResource(R.id.virbrate, R.drawable.virbrate_on);
ComponentName thisWidget = new ComponentName(context,MusicWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(thisWidget, views);
}else{
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.music_widget);
views.setImageViewResource(R.id.virbrate, R.drawable.virbrate_off);
ComponentName thisWidget = new ComponentName(context,MusicWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(thisWidget, views);
}
}
private void playMusic(Context context){
//歌曲名称、
String name = Data.readShare(context, 0, Data.play_name);
File fi = new File(name);
if(!fi.exists()){
Intent it =new Intent("notplay");
this.sendBroadcast(it);
}else{
if(myPlayer1.isPlaying()!=true){
try {
myPlayer1.setDataSource(name);
myPlayer1.prepare();
myPlayer1.start();
myPlayer1.setLooping(true);
updatePlayIcon(context,true);
Data.play = true;
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private void closeMusic(Context context){
if(myPlayer1.isPlaying()==true)
{
myPlayer1.reset();
updatePlayIcon(context,false);
countTime = 0;
Data.play = false;
}
}
private void openVirbrate(){
//Data.virbrate = true;
mVibrator01.vibrate( new long[]{660,660,310,500},0);
}
private void closeVirbrate(){
Data.virbrate = false;
virbrateTime = 0;
mVibrator01.cancel();
}
class MusicMode extends Thread{
Context context;
public MusicMode(Context context){
this.context = context;
}
public void run(){
if(myPlayer1 == null){
myPlayer1 = new MediaPlayer();
}else{
if(myPlayer1.isPlaying()){
closeMusic(context);
}else{
playMusic(context);
}
}
}
}
}
|
|