音乐播放器-MainFragment分析2
2015-10-13 20:34
423 查看
最近一直被签工作整的头晕脑胀,也许当你要毕业的时候也会有这种感慨,在此奉劝哪些有弟弟妹妹将来要报志愿的同学,千万要告诉弟弟妹妹们别去那些工作机会少的城市,到时候你要么老老实实的参加校园招聘(没有好的选择你也只能认命),要么就是奔波劳碌的到其他城市参加各种招聘,起码如果我有弟弟妹妹就一定会告诉他们不要 来偏远地区。最近聊天的时候听一个哥们说,他们校园招聘很少(一个只在历史上留过名的城市),原来找工作都这么难啊,还好,我们这只是好单位很少。
MainFragment里面有好多类,第一个首先要说的就是ServiceManager
通过startService的方法启动service很简单只需要传入一个intent,跟startActivity差不多,当然也可以通过隐式意图来启动,传入一个Action
startService1:Intent intent=new Intent(context,TargetClass);
startService(intent);
startService2:Intent intent=new Intent(ACTION);//需要在manifest里面设置filter,在filter里面填写该action
startService(intent);
android5.0(Lollipop棒棒糖)以后如果是在其他应用里面startService必须通过component 指定packagename,这里是在本应用里面
该例子中开启服务用的是bindService
Intent intent = new Intent("com.ldw.music.service.MediaService");
mContext.bindService(intent, mConn, Context.BIND_AUTO_CREATE);
mConn是ServiceConnection对象
通过
构造。在构造该方法的时候同时初始化了一个该manager的核心IMediaService 即mService
剩下的任务就是用该mService控制音乐的播放暂停切换快进等,
这里需要注意IMediaService是一个接口,mService通过在构造ServiceConnection的时候传入IBinder的时候通过
构造的,启动的服务是隐式服务MediaService,传入的Action在manifest里面注册过了
于是我们找到了该service,就是“.service.MediaService"
该Service由于是bind启动所以有onBind方法的返回值不为null
该mBinder为:
ServerStub为继承该IMideaService.Stub的局部类:
.
..
...
....
这里需要注意的是service是通过一个MusicControl实现的
比如上面的mMc.pause()就是例子,具体的就不贴了,关于音乐是如何播放的,任何一本android入门书籍里面都有(并且也千篇一律)
MainFragment里面有好多类,第一个首先要说的就是ServiceManager
通过startService的方法启动service很简单只需要传入一个intent,跟startActivity差不多,当然也可以通过隐式意图来启动,传入一个Action
startService1:Intent intent=new Intent(context,TargetClass);
startService(intent);
startService2:Intent intent=new Intent(ACTION);//需要在manifest里面设置filter,在filter里面填写该action
startService(intent);
android5.0(Lollipop棒棒糖)以后如果是在其他应用里面startService必须通过component 指定packagename,这里是在本应用里面
该例子中开启服务用的是bindService
Intent intent = new Intent("com.ldw.music.service.MediaService");
mContext.bindService(intent, mConn, Context.BIND_AUTO_CREATE);
mConn是ServiceConnection对象
通过
mConn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { } @Override public void onServiceConnected(ComponentName name, IBinder service) { mService = IMediaService.Stub.asInterface(service); if (mService != null) { mIOnServiceConnectComplete .onServiceConnectComplete(mService); } } };
构造。在构造该方法的时候同时初始化了一个该manager的核心IMediaService 即mService
剩下的任务就是用该mService控制音乐的播放暂停切换快进等,
这里需要注意IMediaService是一个接口,mService通过在构造ServiceConnection的时候传入IBinder的时候通过
mService = IMediaService.Stub.asInterface(service);
构造的,启动的服务是隐式服务MediaService,传入的Action在manifest里面注册过了
<service android:name=".service.MediaService" > <intent-filter> <action android:name="com.ldw.music.service.MediaService" /> </intent-filter> </service>
于是我们找到了该service,就是“.service.MediaService"
该Service由于是bind启动所以有onBind方法的返回值不为null
public IBinder onBind(Intent intent) { return mBinder; }
该mBinder为:
private final IBinder mBinder = new ServerStub();
ServerStub为继承该IMideaService.Stub的局部类:
private class ServerStub extends IMediaService.Stub { @Override public boolean pause() throws RemoteException { // MediaService.this.stopForeground(true); return mMc.pause(); }
.
..
...
....
这里需要注意的是service是通过一个MusicControl实现的
比如上面的mMc.pause()就是例子,具体的就不贴了,关于音乐是如何播放的,任何一本android入门书籍里面都有(并且也千篇一律)
相关文章推荐
- Codeforces Round #325 (Div. 2)D. Phillip and Trains BFS
- 【停课集训10.13】【#3 training】
- Gmail Api 的解读及例子
- hdu 4770 Lights Against Dudely(二进制枚举情况)
- Failed to install Intel HAXM.
- 彻底解决INSTALL_FAILED_UPDATE_INCOMPATIBLE的安装错误
- Exception in thread "main" org.apache.hadoop.security.AccessControlException: Permission denied: use
- Codeforces Round #325 (Div. 2) D. Phillip and Trains
- 正确使用Block避免Cycle Retain和Crash
- Xcode git commit失败 unable to auto-detect email address
- leetcode11 Container With Most Water
- HDU 3487 Play with Chain
- "may only be called from the main thread"
- Error:Error: Invalid file name: must contain only lowercase letters and digits ([a-z0-9_.])
- lintcode 容易题:Trailing Zeros 尾部的零
- 使用MAVEN创建项目报错Failed to execute goal org.appche.maven.plugings:maven-archetype-plgin:2.4
- "plugin container for firefox已停止工作"的解决办法
- poj 1411 Calling Extraterrestrial Intelligence Again 筛素数
- How-to: 安装 SharePoint 后,配置失败: Failed to register SharePoint services
- activity_main cannot be resolved or is not a field