从Service启动的活动过早地调用onPause

时间:2011-12-22 06:45:19

标签: android service android-activity

我正在开始一项从服务中扮演MediaPlayer的活动,它开始正常,但立即调用onPause并离开。但是,如果我没有在Activity中放置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();
        }
}

1 个答案:

答案 0 :(得分:0)

onPause()不是由活动调用,而是由android本身调用,表示活动已经失去焦点,而其他活动现在已经有了。前面有什么活动呢?