警报不玩Android

时间:2011-08-19 08:28:46

标签: android

我尝试在我的android应用程序中实现警报,这是我的代码,我不知道我在哪里做错了,请帮助,谢谢。

OnetimeAlarmReceiver.java          import android.content.BroadcastReceiver;

      import android.content.Context;
       import android.content.Intent;
        import android.widget.Toast;

       public class OnetimeAlarmReceiver extends BroadcastReceiver  {
    @Override 
     public void onReceive(Context context, Intent intent) {
      Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
      }

            }

NoteEdit.java

              private int alarm=1;
         public void setAlarm() { 
    if(alarm!=1){
        alarmTime.set(Calendar.YEAR, dateAndTime.get(Calendar.YEAR));
        alarmTime.set(Calendar.MONTH, dateAndTime.get(Calendar.MONTH));
        alarmTime.set(Calendar.DAY_OF_MONTH, dateAndTime.get(Calendar.DAY_OF_MONTH));
        alarmTime.set(Calendar.HOUR_OF_DAY, dateAndTime.get(Calendar.HOUR_OF_DAY));
        alarmTime.set(Calendar.MINUTE, dateAndTime.get(Calendar.MINUTE)-alarm); 
        alarmTime.set(Calendar.SECOND, 0);   
        alarmTime.set(Calendar.MILLISECOND, 0); 

    }

    Intent intent = new Intent(NoteEdit.this, OnetimeAlarmReceiver.class);
    PendingIntent sender = PendingIntent.getBroadcast(NoteEdit.this, 0, intent, PendingIntent.FLAG_ONE_SHOT); 
    //PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1234567, intent, 0);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime.getTimeInMillis(),  sender);
    Toast.makeText(NoteEdit.this, "Alarm set", Toast.LENGTH_LONG).show();
}
private void populateFields() {
    if (mRowId != null) {
        Cursor note = mDbHelper.fetchNote(mRowId);
        startManagingCursor(note);
        mTitleText.setText(note.getString(
                note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
        mBodyText.setText(note.getString(
                note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
    }
}


         public boolean onOptionsItemSelected(MenuItem item) {

            case PICK_ALARM:
        final CharSequence[] items1 = {"5 minutes before", "10 minutes before", "15 minutes before","20 minutes before", "30 minutes before", "1 hour before"};
        AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
        builder2.setTitle("Select your alarm");
        builder2.setItems(items1, new DialogInterface.OnClickListener() 
        {    

            public void onClick(DialogInterface dialog, int item) {   
                setAlarm(); 
                Toast.makeText(getApplicationContext(), items1[item], Toast.LENGTH_SHORT).show(); 
                if(items1[item]=="5 minutes before"){
                    alarm =5;
                }else if(items1[item]=="10 minutes before"){
                    alarm =10;
                }else if(items1[item]=="15 minutes before"){
                    alarm =15;
                }else if(items1[item]=="20 minutes before"){
                    alarm =20;
                }else if(items1[item]=="30 minutes before"){
                    alarm =30;
                }else if(items1[item]=="1 hour before"){
                    alarm =60;
                }
            }
            });
        AlertDialog alert1 = builder2.create();
        alert1.show();
        break;



        }

2 个答案:

答案 0 :(得分:0)

我认为问题在于你的分钟计算。假设当前以分钟为单位的时间为0且您的警报值为5,则设置闹钟时间,它将获得-5。无论您的逻辑是否正确,请再次检查您的代码

编辑:

if(alarm!=1){
    alarmTime.set(Calendar.YEAR, dateAndTime.get(Calendar.YEAR));
    alarmTime.set(Calendar.MONTH, dateAndTime.get(Calendar.MONTH));
    alarmTime.set(Calendar.DAY_OF_MONTH, dateAndTime.get(Calendar.DAY_OF_MONTH));
    alarmTime.set(Calendar.HOUR_OF_DAY, dateAndTime.get(Calendar.HOUR_OF_DAY));
    alarmTime.set(Calendar.MINUTE, dateAndTime.get(Calendar.MINUTE)); 
    alarmTime.set(Calendar.SECOND, 0);   
    alarmTime.set(Calendar.MILLISECOND, 0); 

}

设置闹钟时,请使用以下代码:

alarmTimeInMillis = alarm*60*1000;
     alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime.getTimeInMillis()-alarmTimeInMillis,  sender);

答案 1 :(得分:0)

如果您收到Toast消息Alarm worked.,要播放音频,您必须添加以下代码
raw中创建res文件夹并添加音频文件

MediaPlayer mpaudio=MediaPlayer.create(this,R.raw.audio_file);

mpaudio.start();

试试这个

Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.SECOND, 10);
        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);

此警报将在10秒后开始

Ins OnetimeAlarmReceiver.class试试此MyAlarmService.java

public class MyAlarmService extends Service {
MediaPlayer  mpaudio;
@Override
public void onCreate() {
    // TODO Auto-generated method stub
    Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show();
    }
@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show();
    return null;
    }
@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show();
    }
@Override
public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    super.onStart(intent, startId);
    Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();
    mpaudio=MediaPlayer.create(this,R.raw.maya);
    mpaudio.start();
    }
@Override
public boolean onUnbind(Intent intent) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show();
    return super.onUnbind(intent);
    }
}