Android的Service服务实际应用

来源:互联网 发布:模型分析软件 编辑:程序博客网 时间:2024/05/21 17:45

       作为Android四大组件之一的Service在Android系统中占据着重要的分量,因为它可是在后台调用的,不以图像的形式呈现在我们的眼前。但正是因为如此,才可以使我们的操作系统变得更加实用。

       今天,我要给大家分享一个简单的图片音乐播放器,来让我们更好的认识一下这个组件。

1.下面这段代码,是使用了Github上的图片的动画格式,以及回调函数操作图片。

<span style="font-size:18px;">public class PicAndMusActivity extends AppCompatActivity                  implements PhotoMoveService.Music{    private ImageView show;    private boolean run = true,click = false;    private Effectstype[] types={Effectstype.Fadein,Effectstype.Fall,Effectstype.Fliph,Effectstype.Flipv,            Effectstype.Newspager,Effectstype.RotateBottom,Effectstype.RotateLeft,Effectstype.Shake,            Effectstype.Sidefill,Effectstype.SlideBottom,Effectstype.Slideright,Effectstype.Slidetop,Effectstype.Slideleft,            Effectstype.Slit};    private int[] imgs={R.mipmap.n1,R.mipmap.n2,R.mipmap.n3,R.mipmap.n4,R.mipmap.n5            ,R.mipmap.car2,R.mipmap.car3,R.mipmap.car4,R.mipmap.mouth};    private Handler handler = new Handler(){        @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);            start(msg.arg1,5000,msg.what);        }    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_pic_and_mus);        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);        show = (ImageView) findViewById(R.id.iv);        show.setClickable(true);        show.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                if (click){                    return;                }                click =true;                Intent intent = new Intent(getBaseContext(),PhotoMoveService.class);                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                startService(intent);                start(6,5000,6);                PhotoMoveService.setMusicStop(PicAndMusActivity.this);                new Thread(new ImgNext()).start();            }        });    }    private void start(int which,int mDuration,int img) {        Log.d("===whice>:",which+"");        show.setImageResource(imgs[img]);        BaseEffects animator = types[which].getAnimator();        if (mDuration != -1) {            animator.setDuration(Math.abs(mDuration));        }        animator.start(show);    }    @Override    public void music() {        run = false;    }    public class ImgNext implements Runnable {        @Override        public void run() {          int i= 0,which= 0;            while (i < imgs.length && run){                if (which == types.length-1){                    which = 0;                }                if (i == imgs.length-1){                    i = 0;                }                try {                    Thread.sleep(5000);                    Message message = new Message();                    message.what =i++;                    message.arg1 = which++;                    handler.sendMessage(message);                } catch (InterruptedException e) {                    e.printStackTrace();                }            }            if (!run){                Message message = new Message();                message.what =imgs.length-1;                message.arg1 = types.length-1;                handler.sendMessage(message);            }        }    }    @Override    protected void onDestroy() {        super.onDestroy();        Intent intent = new Intent(getBaseContext(),PhotoMoveService.class);        stopService(intent);    }}</span>
2.音乐播放的Services
<span style="font-size:18px;">public class PhotoMoveService extends Service {    private static Music musicStop;    private MediaPlayer player;    public PhotoMoveService() {    }    @Override    public void onCreate() {        super.onCreate();        player=MediaPlayer.create(this,R.raw.nba);        stopMusic();    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        player.start();        return super.onStartCommand(intent, flags, startId);    }    @Override    public IBinder onBind(Intent intent) {        // TODO: Return the communication channel to the service.        throw new UnsupportedOperationException("Not yet implemented");    }    public static void setMusicStop(Context context){        musicStop = (Music) context;    }    public interface Music{        public void music();    }    public void stopMusic(){        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {            @Override            public void onCompletion(MediaPlayer mediaPlayer) {                musicStop.music();            }        });    }}</span>
3.完成以上步骤就可以看到带有图片滚动的简单播放器了。


0 0