android 알림 알림 상세 설명 및 인 스 턴 스 코드

6415 단어 androidNotification
android 알림 실례 상세 설명
1.Builder 모드 로 만 들 기
2.smallIcon 을 설정 해 야 합 니 다.setTicker 도 설정 할 수 있 습 니 다.
3.setContentTitle,setContentInfo,setContentText,setWhen 설정 가능
4.setDefaults(플래시,사 운 드,진동)를 설정 할 수 있 고,Notification 을 통 해 flags(지 울 수 있 는 지 없 는 지)를 설정 할 수 있 습 니 다.
5.보 내 려 면 NotificationManager(getSystem Service 를 가 져 와 야 합 니 다)를 가 져 와 야 합 니 다.notify(int id,Notification)
6.manager.cancelAll 지우 기(모든 것 지우 기)cancal(int id);16 번 의 api 가 필요 하 다 면 v4 패 키 지 를 가 져 오고 notificationCompat 호 환 클래스 를 사용 해 야 합 니 다.
사용자 정의 알림
RemoteViews 를 사용 하여 레이아웃 을 만 든 다음 setContent()설정 을 사용 합 니 다.
이벤트 클릭 하여 PendingIntent 로 완료
다음은 하나의 사례 입 니 다.


MainActivity 클래스

public class MainActivity extends Activity {

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

  }

  public void clearNotification(View v) {
    //         NO_CLEAR
    //      manager
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    //                 
    manager.cancelAll();
  }

  public void sendNotification(View v) {
    //     v4     ,  16        
    // NotificationCompat
    //      
    Notification.Builder builder = new Notification.Builder(this);
    // NotificationCompat.Builder builder2 = new NotificationCompat.Builder(
    // this);
    //               
    builder.setSmallIcon(R.drawable.ic_launcher);
    //                
    builder.setTicker("        !!");

    //      
    builder.setContentTitle("   ");
    builder.setContentText("  ");
    builder.setWhen(System.currentTimeMillis());
    builder.setContentInfo("Info");
    builder.setDefaults(Notification.DEFAULT_ALL);
    //       Pending
    Intent intent = new Intent(this, MainActivity.class);
    PendingIntent pi = PendingIntent.getActivity(this, 1, intent,
        PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(pi);
    //      16API  ,         v4   notificationCompat
    Notification notify = builder.build();
    //       
    notify.flags = Notification.FLAG_NO_CLEAR;

    //          
    NotificationManager mananger = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    //       ,  id  ,         ,     
    mananger.notify((int) (Math.random() * 1000), notify);
  }

  public void diyNotification(View v) {
    //           
    RemoteViews views = new RemoteViews(getPackageName(), R.layout.layout);
    Notification notification = new Notification.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher).setTicker("      ")
        //   
        .setContent(views).build();
    //   RemoteViews       
    views.setTextColor(R.id.tv, Color.RED);
    Intent intent = new Intent(this, OneActivity.class);
    //        Service
    PendingIntent pi = PendingIntent.getActivity(this, 2, intent,
        PendingIntent.FLAG_UPDATE_CURRENT);
    views.setOnClickPendingIntent(R.id.tv, pi);

    Intent intent2 = new Intent(this, MainActivity.class);

    PendingIntent pi2 = PendingIntent.getActivity(this, 1, intent2,
        PendingIntent.FLAG_UPDATE_CURRENT);
    views.setOnClickPendingIntent(R.id.iv, pi2);
    //   
    NotificationManager notify = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notify.notify(1, notification);

  }

}

OneActivity 클래스

public class OneActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView tv = new TextView(this);
    tv.setText("    ");
    setContentView(tv);
  }
}

activity.main.xml

<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:orientation="vertical"
  tools:context="com.example.lesson8_notification.MainActivity" >

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="sendNotification"
    android:text="     " />

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="clearNotification"
    android:text="      " />

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="diyNotification"
    android:text="     " />

</LinearLayout>

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

  <TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

  <ImageView
    android:id="@+id/iv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />

</LinearLayout>

마지막 으로 액 티 비 티 등록 하 세 요.
읽 어 주 셔 서 감사합니다. 여러분 에 게 도움 이 되 기 를 바 랍 니 다.본 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!

좋은 웹페이지 즐겨찾기