暂停时警报停止

时间:2015-04-24 07:04:48

标签: android android-activity android-alarms

这是警报活动,在警报设置上两个CAncel和Snooze,默认暂停保持为4秒,这在我们保持警报时工作正常,但是当点击暂停时,警报在暂停时间不幸停止。

public class AlarmAlertActivity extends Activity implements OnClickListener        {

private Calendar alarmTime = Calendar.getInstance();
final Context context = this;
private Alarm alarm;
private MediaPlayer mediaPlayer;
private Vibrator vibrator;
private boolean alarmActive;
private TextView msg_txt, alarmname_txt;
private ImageView img1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final Window window = getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
            | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
            | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

    setContentView(R.layout.simple);

    Bundle bundle = this.getIntent().getExtras();
    alarm = (Alarm) bundle.getSerializable("alarm");
    Button can1 = (Button) findViewById(R.id.can1);
    Button snoozebtn = (Button) findViewById(R.id.snoozebtn);

    Animation shake = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.shake);
    Animation shakeimage = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.shakeimage);
    msg_txt = (TextView) findViewById(R.id.msg_txt);
    alarmname_txt = (TextView) findViewById(R.id.alarmname_txt);
    img1 = (ImageView) findViewById(R.id.img1);
    img1.setImageResource(R.drawable.smileyblack);

    Calendar c = Calendar.getInstance(); 
    hr1 = c.get(Calendar.HOUR_OF_DAY); 
    startAlarm();

    can1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
        }
    });

    snoozebtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {               
    snoozeAlarm(4, ((AlarmManager) getSystemService(ALARM_SERVICE)), context);
            finish();
        }
    });
    }

private void startAlarm() {
    if (alarm.getAlarmTonePath() != "") {
        mediaPlayer = new MediaPlayer();
        if (alarm.getVibrate()) {
            vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
            long[] pattern = { 1000, 200, 200, 200 };
            vibrator.vibrate(pattern, 0);
        }
        try {
            mediaPlayer.setVolume(1.0f, 1.0f);
            mediaPlayer.setDataSource(this,
                    Uri.parse(alarm.getAlarmTonePath()));
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
            mediaPlayer.setLooping(true);
            mediaPlayer.prepare();
            mediaPlayer.start();

        } catch (Exception e) {
            mediaPlayer.release();
            alarmActive = false;
        }
    }

}

public static final void snoozeAlarm(int seconds, AlarmManager alarmManager,
        Context context) {
    Intent intent = new Intent(context, AlarmAlertActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + seconds * 1000, pendingIntent);
    Toast.makeText(context, "Snoozed to " + seconds + " seconds.",
            Toast.LENGTH_SHORT).show();
} }

/ *这是警报类

  public class Alarm implements Serializable {
public String toString() {
        switch(this.ordinal()){
            case 0:
                return "Sunday";
            case 1:
                return "Monday";
            case 2:
                return "Tuesday";
            case 3:
                return "Wednesday";
            case 4:
                return "Thursday";
            case 5:
                return "Friay";
            case 6:
                return "Saturday";
        }
        return super.toString();
    }

}
private int id;
private Boolean alarmActive = true;
private Calendar alarmTime = Calendar.getInstance();
private Day[] days = {Day.MONDAY,Day.TUESDAY,Day.WEDNESDAY,Day.THURSDAY,Day.FRIDAY,Day.SATURDAY,Day.SUNDAY};    
private String alarmTonePath = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM).toString();
private Boolean vibrate = true;
private String alarmName = "Alarm";

public Alarm() {

}


public Boolean getAlarmActive() {
    return alarmActive;
}

public void setAlarmActive(Boolean alarmActive) {
    this.alarmActive = alarmActive;
}

public Calendar getAlarmTime() {
    if (alarmTime.before(Calendar.getInstance()))
        alarmTime.add(Calendar.DAY_OF_MONTH, 1);
    while(!Arrays.asList(getDays()).contains(Day.values()[alarmTime.get(Calendar.DAY_OF_WEEK)-1])){
        alarmTime.add(Calendar.DAY_OF_MONTH, 1);            
    }
    return alarmTime;
}

public String getAlarmTimeString() {
    String time = "";
    if (alarmTime.get(Calendar.HOUR_OF_DAY) <= 9)
        time += "0";
    time += String.valueOf(alarmTime.get(Calendar.HOUR_OF_DAY));
    time += ":";

    if (alarmTime.get(Calendar.MINUTE) <= 9)
        time += "0";
    time += String.valueOf(alarmTime.get(Calendar.MINUTE));

    return time;
}

public void setAlarmTime(Calendar alarmTime) {
    this.alarmTime = alarmTime;
}

public void setAlarmTime(String alarmTime) {
    String[] timePieces = alarmTime.split(":");
    Calendar newAlarmTime = Calendar.getInstance();
    newAlarmTime.set(Calendar.HOUR_OF_DAY,
            Integer.parseInt(timePieces[0]));
    newAlarmTime.set(Calendar.MINUTE, Integer.parseInt(timePieces[1]));
    newAlarmTime.set(Calendar.SECOND, 0);
    setAlarmTime(newAlarmTime);     
}

public Day[] getDays() {
    return days;
}

public void setDays(Day[] days) {
    this.days = days;
}

public void addDay(Day day){
    boolean contains = false;


}

public void removeDay(Day day) {

    List<Day> result = new LinkedList<Day>();
    for(Day d : getDays())
        if(!d.equals(day))
            result.add(d);
    setDays(result.toArray(new Day[result.size()]));
}

public String getAlarmTonePath() {
    return alarmTonePath;
}

public void setAlarmTonePath(String alarmTonePath) {
    this.alarmTonePath = alarmTonePath;
}

public String getAlarmName() {
    return alarmName;
}

public void setAlarmName(String alarmName) {
    this.alarmName = alarmName;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}


public void schedule(Context context) {
    setAlarmActive(true);

    Intent myIntent = new Intent(context, AlarmAlertBroadcastReciever.class);
    myIntent.putExtra("alarm", this);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, myIntent,PendingIntent.FLAG_CANCEL_CURRENT);

    AlarmManager alarmManager = (AlarmManager)context.getSystemService(context.ALARM_SERVICE);

    alarmManager.set(AlarmManager.RTC_WAKEUP, getAlarmTime().getTimeInMillis(), pendingIntent);                 
}

public String getTimeUntilNextAlarmMessage(){

        }
    }
    return alert;
}

}

警报工作正常,但是当我设置延后4秒钟时,不幸的是在暂停时停止

这是LOGCAT中的错误

   04-24 13:42:09.893: E/AndroidRuntime(1403): FATAL EXCEPTION: main
    04-24 13:42:09.893: E/AndroidRuntime(1403): java.lang.RuntimeException: Unable to start activity   ComponentInfo{com.am.alarm/com.am.alarm.alert.AlarmAlertActivity}: java.lang.NullPointerException
   04-24 13:42:09.893: E/AndroidRuntime(1403):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
   04-24 13:42:09.893: E/AndroidRuntime(1403):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
   04-24 13:42:09.893: E/AndroidRuntime(1403):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
   04-24 13:42:09.893: E/AndroidRuntime(1403):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
   04-24 13:42:09.893: E/AndroidRuntime(1403):  at android.os.Handler.dispatchMessage(Handler.java:99)
   04-24 13:42:09.893: E/AndroidRuntime(1403):  at android.os.Looper.loop(Looper.java:137)
   04-24 13:42:09.893: E/AndroidRuntime(1403):  at android.app.ActivityThread.main(ActivityThread.java:4340)
   04-24 13:42:09.893: E/AndroidRuntime(1403):  at java.lang.reflect.Method.invokeNative(Native Method)
   04-24 13:42:09.893: E/AndroidRuntime(1403):  at java.lang.reflect.Method.invoke(Method.java:511)
   04-24 13:42:09.893: E/AndroidRuntime(1403):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
   04-24 13:42:09.893: E/AndroidRuntime(1403):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
   04-24 13:42:09.893: E/AndroidRuntime(1403):  at dalvik.system.NativeStart.main(Native Method)
   04-24 13:42:09.893: E/AndroidRuntime(1403): Caused by: java.lang.NullPointerException
   04-24 13:42:09.893: E/AndroidRuntime(1403):  at com.am.alarm.alert.AlarmAlertActivity.startAlarm(AlarmAlertActivity.java:178)
   04-24 13:42:09.893: E/AndroidRuntime(1403):  at com.am.alarm.alert.AlarmAlertActivity.onCreate(AlarmAlertActivity.java:92)
   04-24 13:42:09.893: E/AndroidRuntime(1403):  at android.app.Activity.performCreate(Activity.java:4465)
   04-24 13:42:09.893: E/AndroidRuntime(1403):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
   04-24 13:42:09.893: E/AndroidRuntime(1403):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)

2 个答案:

答案 0 :(得分:0)

由于您提供的结束()电话,

答案 1 :(得分:0)

我解决了这个问题,而不是直接从意图调用活动,在广播接收器中传递广播接收器然后活动。

   Intent intent = new Intent(context, BroadcastReciever.class);
    intent.putExtra("alarm", alarm);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1 , intent, PendingIntent.FLAG_UPDATE_CURRENT);