服务自动启动

时间:2015-07-09 10:18:32

标签: android service

我有播放背景音乐的服务。我的活动有一个2.5秒倒数计时器,当活动开始也计时器启动时,我已经将服务开始于OnFinish方法的计时器,意味着音乐应该在活动开始后2.5秒播放,但我的问题是音乐同时开始播放活动开始时。 谢谢你的回答。 这是我的计时器:

if (first.getString("f", "0").equals("0")) {

    t1.setVisibility(View.INVISIBLE);
    t2.setVisibility(View.INVISIBLE);
    m.setVisibility(View.INVISIBLE);
    p2.setVisibility(View.INVISIBLE);
    p.setVisibility(View.INVISIBLE);
    h.setVisibility(View.INVISIBLE);
    t3.setVisibility(View.INVISIBLE);

    final CountDownTimer Count = new CountDownTimer(2500, 1000) {
        final ImageView im = (ImageView) findViewById(R.id.imageView2);

        public void onTick(long millisUntilFinished) {
        }

        public void onFinish() {
            t1.setVisibility(View.VISIBLE);
            t2.setVisibility(View.VISIBLE);
            m.setVisibility(View.VISIBLE);
            p2.setVisibility(View.VISIBLE);
            p.setVisibility(View.VISIBLE);
            h.setVisibility(View.VISIBLE);
            im.setVisibility(View.INVISIBLE);
            m.setImageResource(R.drawable.music);
            m.setTag(R.drawable.music);
            **startService(new Intent(main.this, music.class));**
            medit.putString("m", "0");
            medit.commit();
        }
    };
        Count.start();
        editor.putString("f", "1");
        editor.commit();
}
    else {
        final ImageView im = (ImageView) findViewById(R.id.imageView2);
        im.setVisibility(View.INVISIBLE);
        t3.setVisibility(View.INVISIBLE);
    }

这是我的服务:

public class music extends Service {
public music() {
}

MediaPlayer mp;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    mp= MediaPlayer.create(music.this, R.raw.theme);
    mp.setLooping(true);
}

@Override
public void onStart(Intent intent, int startId) {
    mp.start();
}

@Override
public void onDestroy() {
    super.onDestroy();
    mp.stop();
}

}

0 个答案:

没有答案