ANDROID 알람 관련

<알람설정시 매일 같은 시간에 나오게 하기>
try
{
// 내일 아침 8시 10분에 처음 시작해서, 24시간 마다 실행되게
Date tomorrow = new SimpleDateFormat(“yyyy-MM-dd hh:mm:ss”).parse(“2012-02-25 08:10:00”);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setInexactRepeating(AlarmManager.RTC, tomorrow.getTime(), 24 * 60 * 60 * 1000, sender);
} catch (ParseException e)
{
e.printStackTrace();
}

출처: http://susemi99.tistory.com/1127

 

<Alarm Manager Error NullPointerException>

-> 이 경우에 알람 끄는 기능을 추가하니까 해결. cancel.

case R.id.bt_stop:
// TODO Auto-generated method stub
Intent intent = new Intent(ThirdActivity.this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(ThirdActivity.this, 0, intent, 0);
am.cancel(pendingIntent); // 이런식으로 cancel 가능하게끔

 

<알람시 사운드 설정하는 방법>

알람에서 전달되는 activity에 oncreate안에 playSound(this, getAlarmUri()); 를 넣고.
(앞에 private MediaPlayer mMediaPlayer; 선언)

아래에 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);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
} catch (IOException e) {
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;
}
https://blog.mikesir87.io/2013/04/android-creating-an-alarm-with-alarmmanager/

한번만 울리고 멈추게 하기 위해서는
mp.start아래에

mp.start();
mMediaPlayer.seekTo(0);
또는

mp.start();
mp.setOnSeekCompleteListener(new OnSeekCompleteListener() {
public void onSeekComplete(MediaPlayer mp) {
mp.stop();
mp.release();
}
});
를 넣어주면 된다.

 

<알람을 assets에 넣고 원하는 mp3 나오게 하기>

https://stackoverflow.com/questions/16795600/can-you-play-a-mp3-file-from-the-assets-folder

MediaPlayer mediaPlayer = new MediaPlayer();
AssetFileDescriptor afd = context.getAssets().openFd(“your_file.mp3”);
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mediaPlayer.prepare();
mediaPlayer.start();

예제) 한번만 울리게 하기

private void playSound() {

   mMediaPlayer = new MediaPlayer();
         try {
            AssetFileDescriptor afd = getAssets().openFd("bell.mp3"); // cf : 파일을 여는 부분
            mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
            afd.close();
            mMediaPlayer.prepare();
            mMediaPlayer.start();

            // cf : 이때 setOnseekCompleteListener를 이용하여 알람이 한번만 울리고 멈추게 해 주었다.
            mMediaPlayer.setOnSeekCompleteListener(new OnSeekCompleteListener() {
               public void onSeekComplete(MediaPlayer mMediaPlayer) {
                  mMediaPlayer.stop();
                  mMediaPlayer.release();
               }
            });

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

}

< intent로 값을 전달하고 notification으로 받는 방법> – 사용안해봄
Intent AlarmIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
AlarmIntent.putExtra(“Ringtone”,
Uri.parse(“getResources().getResourceName(R.raw.shankh_final_mid)”));
PendingIntent Sender = PendingIntent.getBroadcast(this, 0, AlarmIntent, 0);
AlarmManager AlmMgr = (AlarmManager)getSystemService(ALARM_SERVICE);
AlmMgr.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() +
(60 * 1000), (24 * 60 * 60 * 1000), Sender);
https://stackoverflow.com/questions/11929929/android-playing-own-sound-on-alarm

public void onReceive(Context context, Intent intent) {

Intent in = new Intent(context, SnoozeEvent.class);
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent Sender = PendingIntent.getActivity(context, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);
manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notification = new Notification(R.drawable.icon, “Wake up alarm”, System.currentTimeMillis());
notification.setLatestEventInfo(context, “Hanuman Chalisa”, “Wake Up…”, Sender);
notification.flags = Notification.FLAG_INSISTENT;
notification.sound = (Uri)intent.getParcelableExtra(“Ringtone”);
manager.notify(1, notification);
}

<화면이 꺼져있을때 알람울리기>
private static PowerManager.WakeLock myWakeLock;

PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
myWakeLock= pm.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE, TAG);
myWakeLock.acquire(); //실행후 리소스 반환 필수

static void releaseCpuLock() {

Log.d(TAG,”Releasing cpu wake lock”);

if (myWakeLock!= null) {
myWakeLock.release();
myWakeLock= null;
}

}

<uses-permission android:name=”android.permission.WAKE_LOCK”/>
이게 반드시 필요
http://www.androidside.com/bbs/board.php?bo_table=B49&wr_id=160240
http://aroundck.tistory.com/48

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s