警报不起作用

时间:2012-11-21 08:33:16

标签: java android alarmmanager

当我点击按钮(onClick属性设置为startAlarm())时,我正试图发出警报。这个按钮工作正常。我的startAlarm()方法中的toast显示但没有其他内容,而不是我的toast或其他两个文件中的日志(AlarmReceiver和AlarmActivity)。任何帮助将不胜感激。

这是我的AlarmActivity

import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

public class AlarmActivity extends Activity {
    private MediaPlayer mp;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Toast.makeText(getApplicationContext(), "Its done!", Toast.LENGTH_SHORT).show();
        super.onCreate(savedInstanceState);
        Log.d("alarm","activity");
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_alarm);

        Button stopAlarm = (Button) findViewById(R.id.stop_alarm_button);
        stopAlarm.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View arg0, MotionEvent arg1) {
            mp.stop();
            finish();
            return false;
        }
    });

    playSound(this, getAlarmUri());
}

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;
}

private void playSound(Context context, Uri alert) {
    mp = new MediaPlayer();
    try {
        mp.setDataSource(context, alert);
        final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
            mp.setAudioStreamType(AudioManager.STREAM_ALARM);
            mp.prepare();
            mp.start();
        }
    } catch (IOException e) {
        System.out.println("OOPS");
    }
}
}

这是我的MainActivity中的方法

public void startAlarm(View v) {
    Intent myIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, myIntent, 0);
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 1);
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    Toast.makeText(MainActivity.this, "Start Alarm", Toast.LENGTH_LONG).show();
}

这是我的AlarmReceiver

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class AlarmReceiver extends BroadcastReceiver { 
    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            Intent newIntent = new Intent(context, AlarmActivity.class);
        Log.d("alarm", "receive");
        newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(newIntent);
        Toast.makeText(context, "received", Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        e.printStackTrace();
    }
    }   
}

1 个答案:

答案 0 :(得分:1)

我认为您的代码的问题在于您使用广播接收器来获取警报事件,但您正试图通过服务接收它。

您应该使用PendingIntent.getBroadcast(getApplicationContext(), 0, myIntent, 0);代替PendingIntent.getService(getApplicationContext(), 0, myIntent, 0);