单击按钮(将onClick属性设置为startAlarm())时,我试图使警报消失。此按钮工作正常。我的startAlarm()方法中的烤面包没有显示,但是没有其他显示,没有我的烤面包或其他两个文件(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();
    }
    }
}

最佳答案

我认为您代码的问题是您正在使用广播接收器来获取警报事件,但是您正在尝试通过服务接收它。
您应该使用PendingIntent.getBroadcast(getApplicationContext(), 0, myIntent, 0);而不是PendingIntent.getService(getApplicationContext(), 0, myIntent, 0);

07-24 09:20