Android 알림 알림 분석

4211 단어 AndroidNotification
Notification 은 핸드폰 상태 표시 줄 에 표 시 된 알림 입 니 다.Notification 알림 은 전역 적 인 알림 입 니 다.보통 NotificationManager 를 통 해 관리 합 니 다.
일반적으로 Notification 을 사용 하 는 절 차 는 다음 과 같다.
  • 1.getSysytemService(NOTIFICATION 호출SERVICE)시스템 의 NotificationManager 를 가 져 와 Notification 의 발송 과 회수
  • 구조 기 를 통 해 알림 을 만 듭 니 다.
  • 3.Notification set 의 각종 속성 을 위 한 다음 builder()를 만 듭 니 다.
  • 4.NotificationManager 를 통 해 알림 을 발송 합 니 다다음은 위의 용법 을 실례 를 통 해 보 여 드 리 겠 습 니 다.먼저 효과 도 를 보 겠 습 니 다.

    시스템 알림 관리자 가 져 오기
    
    private NotificationManager nm;
    
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //         
        nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
      }
    
    
    2.주요 레이아웃 의 두 단 추 를 누 르 면 감청 이 벤트 를 추가 하고 시작 알림 을 설정 하 며 각종 속성 과 취소 알림 을 설정 합 니 다.
    각종 속성 코드 에 소 개 된 것 은 매우 상세 하 며,구체 적 으로 API 를 참고 할 수 있다.
    시작 알림
    
    public void send(View view){
        //           Activity
        Intent intent = new Intent(MainActivity.this,OtherActivity.class);
        //      
        PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
        //    
        Notification notify = new Notification.Builder(this)
            //       ,      
            .setAutoCancel(true)
            //               
            .setTicker("   ")
            //       
            .setSmallIcon(R.mipmap.ic_launcher)
            //         
            .setContentTitle("     ")
            //      
            .setContentText("          ")
            //           ,   led 
            .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS)
            //ALL          ,  ,  ,   ,        
    //        .setDefaults(ALL)
            //       
            //setSound(Uri.parse())
            //        
            .setContentIntent(pi)
            //   build     
            .build();
        //      ,  NotificationManager   
        nm.notify(1,notify);
      }
    
    여기 서 사용 하 는 Other Activity 는 알림 을 통 해 시작 하 는 또 다른 Activity 입 니 다.시작 하기 위해 서 는 목록 파일 에 이 Activity 를 추가 해 야 합 니 다.또한 플래시 와 진동기 가 사용 되 기 때문에 해당 권한 을 추가 해 야 합 니 다.
    
    <activity android:name=".OtherActivity"> </activity>
      <uses-permission android:name="android.permission.FLASHLIGHT"/>
      <uses-permission android:name="android.permission.VIBRATE"/>
    
    취소 알림
    
    //    
      public void closed(View view){
        nm.cancel(1);
      }
    
    사용 하기에 상당히 편리 합 니 다.마지막 으로 메 인 인터페이스 레이아웃 을 첨부 합 니 다.
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
      android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
      android:orientation="horizontal"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
    
    
      <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="    "
        android:onClick="send"
        android:id="@+id/btnstartnotification"
         />
    
      <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="    "
        android:onClick="closed"
        android:id="@+id/btnstopnotification"
         />
    </LinearLayout>
    
    이상 은 Android Notification 알림 에 대한 상세 한 내용 이 므 로 여러분 의 학습 에 도움 이 되 기 를 바 랍 니 다.

    좋은 웹페이지 즐겨찾기