从Service启动的Activity过早调用onPause



我正在启动一个活动,从一个服务中播放MediaPlayer,它开始很好,但立即调用onPause并离开。但是如果我不把onPause()方法在活动…Mediaplayer播放正常,视图正确。下面是我的代码:

服务:

import android.content.Intent;
import android.util.Log;
public class ReminderService extends WakeReminderIntentService {
    public ReminderService() {
        super("ReminderService");
            }
    @Override
    void doReminderWork(Intent intent) {
        Log.d("ReminderService", "Doing work.");
        Intent dialogIntent = new Intent(getBaseContext(), TestRec.class);
        dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getApplication().startActivity(dialogIntent);

    }
}

TestRec.class:

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.view.Window;
import android.view.WindowManager;
public class TestRec extends Activity {
    MediaPlayer mp;
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(android.view.Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
         getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                    | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                 );
         mp = MediaPlayer.create(this, R.raw.music);
            mp.start();
    }

    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        mp.stop();
                finish();
        }
}

onPause()不是由activity调用,而是由android本身调用,以表明activity已经失去焦点,而其他activity现在拥有它。排在前面的是什么活动?

最新更新