알람 예제 (AlarmManager Example)


 As of API 19, all repeating alarms are inexact.
Because this method has been available since API 3, your application can safely call it and be assured that it will get similar behavior on both current and older versions of Android.

https://developer.android.com/reference/android/app/AlarmManager.html#setRepeating(int, long, long, android.app.PendingIntent)


 공식적으로 API 19에서 모든 반복 알람은 정확하지 않다고 명시하고 있다. 실제로 setRepeating() 메서드를 사용해서 반복 알림을 설정하면 최소주기가 60초라는 것을 알 수 있다. 60초보다 빠른 동작이 필요하다면 다른 방법을 사용해야 한다.


activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.alarmexample.mystoryg.alarmexample.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alarm log" />

<TextView
android:id="@+id/log"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>


MainActivity.java

package com.alarmexample.mystoryg.alarmexample;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.widget.TextView;

public class MainActivity extends Activity {
public static TextView sTv;
private AlarmManager mAm;
private Intent mIntent;
private PendingIntent mSender;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sTv = (TextView) findViewById(R.id.log);
alarm();
}

private void alarm() {
mIntent = new Intent(MainActivity.this, executeAlarm.class);
mSender = PendingIntent.getBroadcast(MainActivity.this, 0, mIntent, 0);

long initialTime = SystemClock.elapsedRealtime();
initialTime += 2 * 1000;

mAm = (AlarmManager) getSystemService(ALARM_SERVICE);
mAm.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, initialTime, 2 * 1000, mSender);
}

@Override
protected void onPause() {
super.onPause();
mAm.cancel(mSender);
} } 


executeAlarm.java

package com.alarmexample.mystoryg.alarmexample;

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

import java.util.Calendar;

public class executeAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

final Calendar calendar = Calendar.getInstance();
int mHour = calendar.get(Calendar.HOUR_OF_DAY);
int mMinute = calendar.get(Calendar.MINUTE);
int mSecond = calendar.get(Calendar.SECOND);

MainActivity.sTv.append("Current Time : " + mHour + ":" + mMinute + ":" + mSecond + "\n");
Toast.makeText(context, "Current Time : " + mHour + ":" + mMinute + ":" + mSecond, Toast.LENGTH_LONG).show();

} } 


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alarmexample.mystoryg.alarmexample">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".executeAlarm" />

</application>
</manifest> 



'프로그래밍 > Android' 카테고리의 다른 글

Preference Example  (0) 2017.01.10
브로드캐스트 리시버 (BroadcastReceiver Example)  (0) 2016.12.20
알림 예제(Notification Example)  (0) 2016.12.20
Service Example  (0) 2016.12.17
명시적 인텐트(Explicit Intent)  (0) 2016.12.16
암시적 인텐트(Implicit Intent)  (0) 2016.12.15
WebView 줄바꿈 문제  (0) 2016.02.18

+ Recent posts