카테고리

  • 안드로이드
  • IOS
  • MFC
  • JAVA
  • AWS
  • LAMP
  • 여행&사진
  • 이런저런생활
  • 2015년 12월 1일 화요일

    [안드로이드]헤드업 알림(Heads Up Notification)

    안드로이드 롤리팝(5.0)버전부터 통지기능에 헤드업 알림이 추가됨.
    자세한 사항은 가서 직접 보시도록.


    헤드업 알림(Heads Up Notification)은 현재 실행중인 어플리케이션이 떠 있는 상태에서
    다른 어플리케이션의 정보를 상태바가 아닌 화면으로 보여주는 기능
    아래 그림을 참조.

    이런식으로 다른 무언가를 하고 있는 중에 전화가 왔을 시, 전화 화면으로 바뀌는 것이 아닌
    Notification처럼 상단 상태바위에서 알림창이 내려오도록 하는 기능임

    안드로이드 5.0 버전이상에서만 사용 가능함.

    사용방법은 하위 소스 참조
    public class MainActivity extends Activity {

        NotificationManager nm;
        Notification.Builder builder;
        Intent push;
        PendingIntent fullScreenPendingIntent;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            builder = new Notification.Builder(this);
            builder.setSmallIcon(R.mipmap.ic_launcher);
            builder.setTicker("Test1"); //** 이 부분은 확인 필요
            builder.setWhen(System.currentTimeMillis());
            builder.setContentTitle("Test2"); //** 큰 텍스트로 표시
            builder.setContentText("Test3"); //** 작은 텍스트로 표시
            builder.setAutoCancel(true);
            builder.setPriority(Notification.PRIORITY_MAX); //** MAX 나 HIGH로 줘야 가능함

            nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            //** Intent와 PendingIntent를 추가해 주는 것으로 헤드업 알림이 가능
            //** 없을 경우 이전 버전의 Notification과 동일
            push = new Intent();
            push.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            push.setClass(this, MainActivity.class);

            fullScreenPendingIntent = PendingIntent.getActivity(this, 0, push, PendingIntent.FLAG_CANCEL_CURRENT);
            builder.setFullScreenIntent(fullScreenPendingIntent, true);
            //** 여기까지 헤드업 알림을 사용하기 위한 필수 조건!

            Button bt_Test = (Button) findViewById(R.id.button);

            bt_Test.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //** 123456은 고유 ID값이므로 아무거나 들어가도 상관 없음
                    nm.notify(123456, builder.build());
                }
            });
        }
    }

    테스트 화면

     - 안드로이드 5.0 이전 버전의 Notification 기능에 대한 소스코드 정리가 잘 되 있음

     - 안드뢰드 5.0부터 새로 추가된 기능에 대한 설명(영문)

    댓글 없음 :

    댓글 쓰기