Local Notification 구현 방법

2014. 7. 8. 10:23안드로이드 개발

* Local Notification 실행

<MainActivity.java>

new ProcessSetAlarmTask().execute(null,null,null);


// 스레드를 이용하여 알람 설정

private class ProcessSetAlarmTask extends AsyncTask<Void, Void, Void>{

     @Override

     protected Void doInBackground(Void... params) {

      boolean resultProc = setAlarm();

      Looper.prepare();

      if(resultProc == true){

       pHandler.sendEmptyMessage(0);

      }else{

       pHandler.sendEmptyMessage(1);

      }

      return null;

     }

    }


//알람 설정

private boolean setAlarm(){

     try{

      AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);

      //아래는 리시버에서 호출할 이름을 지정

      Intent intent = new Intent("com.example.eventalarm");

      PendingIntent pender = PendingIntent.getBroadcast(

        MainActivity.this, 0, intent, 0);

      

      Calendar calendar = Calendar.getInstance();

      calendar.set(Calendar.HOUR_OF_DAY, 10);

      calendar.set(Calendar.MINUTE, 30);

      calendar.set(Calendar.SECOND, 0);

      alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000*60*60*24, pender);

      return true;

     }catch(Exception e){

      e.printStackTrace();

     }

     return false;

    }


Handler pHandler = new Handler()

    {

       public void handleMessage(Message msg)

       {

        if(msg.what == 0)

        {

//알람 설정 성공      

        }

        else if(msg.what == 1)

        {

//알람 설정 실패

        }

       }

      };


<AndroidManifest.xml>

<!-- 알람용 리시버 -->

<receiver 

    android:label="alarmrecever"

    android:name=".AlarmReceive" >

<intent-filter>

<action android:name="com.example.eventalarm" />

</intent-filter>

</receiver>


<AlarmReceive.java>

public class AlarmReceive extends BroadcastReceiver {

 @Override

 public void onReceive(Context context, Intent intent) {

  // TODO Auto-generated method stub

  Toast.makeText(context, "Alarm Received!", Toast.LENGTH_LONG).show();

      

      NotificationManager notifier = (NotificationManager) context

          .getSystemService(Context.NOTIFICATION_SERVICE);

                      //노티피케이션바에 나타낼 아이콘이미지

  Notification notify = new Notification(R.drawable.icon"Local Notification!", System.currentTimeMillis());

  PendingIntent contentIntent = PendingIntent.getActivity(context, 0,

            new Intent(context, MainActivity.class)

                    .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

                    .putExtra("sendId", intent.getExtras().getString("name")),

            PendingIntent.FLAG_UPDATE_CURRENT);

 

  notify.setLatestEventInfo(context, "ALARM SAMPLE" "Local Notification 내용" contentIntent);

                //노티피케이션에서 선택하면 표시를 없앨지 말지 설정

  notify.flags |= Notification.FLAG_AUTO_CANCEL;

                //진동 설정

  notify.vibrate = new long[] { 200, 200, 500, 300 };

  notify.number++;

                //노티를 던진다!

  notifier.notify(1, notify);

    

      

  } catch(Exception e)

  {

      Log.e("recieve", e.toString());

  }

 }

}


* Local Notification 해제

AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);

Intent intent = new Intent("com.example.eventalarm");

PendingIntent pender = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);

alarm.cancel(pender);