使用可以使用它来设置时间的媒体播放器将多长时间玩
new CountDownTimer(1000 * lengthOfTime, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
if(yourMediaPlayer.isPlaying()){
yourMediaPlayer.stop();
yourMediaPlayer.release();
}
}
}
}.start();
创建服务,并使用
Intent it = new Intent();
it.setAction("your.package.name.whatEverYourServiceName");
startService(it);
然后在你的清单中添加这个称呼它:
<service
android:name="your.package.name.whatEverYourServiceName"
android:label="Your Service Homie"
android:process=":my_process" >
<intent-filter>
<action android:name="your.package.name.whatEverYourServiceName" >
</action>
</intent-filter>
</service>
这里是服务样本:
public class whatEverYourServiceName extends Service{
@Override
public void onStart(Intent intent, int startId){
super.onStart(intent, startId);
//Add your code here mate
}
@Override
public void onDestroy(){
super.onDestroy();
//Add what do you want to do here when the service is stop like do a backflip
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
如果您使用的是eclipse,只需按ctrl + shift + o即可自动导入包。
我有它的工作设置时间,我的问题是,我需要声音从服务而不是活动和Idont知道如何做impliment服务 – user2869238
我编辑了我的答案。 – RussVirtuoso
在服务代码中发生了什么,或者在创建类后是否为空?并会帮助你,如果我发布我的主要活动Java类 – user2869238