待定意图不在服务内部工作

时间:2014-06-02 22:16:48

标签: android android-intent android-service alarmmanager

我有一个应用程序,在启动时,设置服务。该服务应在120秒后响铃。但是,虽然服务启动,但它不会等待120秒。相反,它会立即响起警报。

在我尝试实施该服务之前,应用程序运行正常,即在启动应用程序时,延迟了120秒,然后警报响起。

代码:

启动服务的主要活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    Intent intent = new Intent(this, ServiceTest.class);
    startService(intent);
}

服务:

public class ServiceTest extends IntentService {

    public ServiceTest() {
        super("ServiceTest");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
        return super.onStartCommand(intent,flags,startId);
    }

    @Override
    protected void onHandleIntent (Intent intent) {

        Calendar cal = Calendar.getInstance();  //Create a calendar
        cal.add(Calendar.SECOND, 120);          //Add 120 seconds to the current time

        Intent dialogIntent = new Intent(this, AlarmReceiverActivity.class);
        dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getApplication().startActivity(dialogIntent);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 1234, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        AlarmManager am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

    }
}

并且,活动响起警报并且过早地被服务调用:

public class AlarmReceiverActivity extends Activity {

    private MediaPlayer mMediaPlayer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
                WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        setContentView(R.layout.activity_alarm_receiver);

        Button stopAlarm = (Button) findViewById(R.id.stopAlarm);
        stopAlarm.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                mMediaPlayer.stop();
                finish();
            }
        });

        playSound(this, getAlarmUri());
    }

    private void playSound(Context context, Uri alert) {
        mMediaPlayer = new MediaPlayer();
        try {
            mMediaPlayer.setDataSource(context, alert);
            final AudioManager audioManager = (AudioManager) context
                    .getSystemService(Context.AUDIO_SERVICE);
            if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
                mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);

                try {
                    mMediaPlayer.prepare();
                    mMediaPlayer.start();
                } catch (IOException e) {
                    System.out.println("Oops");
                }

            }
        } catch (IOException e1) {
            System.out.println("OOPS");
        }
    }

    //Get an alarm sound. Try for an alarm. If none set, try notification,
    //Otherwise, ringtone.
    private Uri getAlarmUri() {
        Uri alert = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_ALARM);
        if (alert == null) {
            alert = RingtoneManager
                    .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            if (alert == null) {
                alert = RingtoneManager
                        .getDefaultUri(RingtoneManager.TYPE_RINGTONE);
            }
        }
        return alert;
    }

}

1 个答案:

答案 0 :(得分:1)

从服务中删除此行:

getApplication().startActivity(dialogIntent);

你告诉应用程序启动一个Activity,所以它确实如此。 :)

您可能希望此dialogIntent代之以创建PendingIntent