<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Android 中的通知 > 原文: [https://javatutorial.net/notifications-in-android](https://javatutorial.net/notifications-in-android) Android 應用程序中的通知非常有用。 本教程將通過示例說明如何在應用程序中設置通知。 ## 通知 通知是通知或通知某事。 Android 應用程序中的通知非常有用。 它提供有關重要信息的信息。 現在,一天的用戶希望易于使用,而又不想一次又一次地打開應用以獲取新的更新。 取而代之的是,它們啟用上推通知。 例如,沒有人希望在一天內一次又一次地檢查電子郵件,最好是在收到新郵件時調整通知。 為了滿足這種通知需求,本教程提供了一個簡單的示例來設置通知。 ## `NotificationCompat.Builder` `NotificationCompat`構建器類可幫助我們開發通知布局。 它有許多用于在應用程序中進行通知的方法。 以下是其中一些: * `addNotification(NotificationCompat.Action action)`:顧名思義,此方法用于向通知中添加操作。 * `addPerson(String uri)`:此方法用于將人添加到通知中。 例如,來自特定人的電子郵件。 * `getNotification()`:用于獲取通知。 * `build()`:用于構建通知。 * `setCategory(String category)`:此方法用于設置通知類別。 * `setColor(int argb)`:此方法用于設置通知顏色。 * `setContentTitle(CharSequence title)`:此方法用于設置通知欄的標題。 * `setContentText(CharSequence text)`:此方法用于設置通知欄中顯示的文本。 ## 通知管理器 通知管理器是一個類,用于告知用戶后臺發生了某些事情。 它通知事件。 ## Android 中的通知示例 讓我們開始創建一個簡單的示例,該示例演示如何在 Android 應用中使用通知。 打開您的 Android Studio 并創建主要活動。 在主要活動中,只有文本視圖,圖像視圖和按鈕。 當用戶單擊按鈕時,它將在狀態欄中顯示通知。 這是`activity_main.xml`的 XML 布局 ```java <?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context="MainActivity"> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="48dp" android:text="Java Tutorial " android:textColor="@android:color/holo_red_dark" android:textSize="30dp" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButton" android:src="@drawable/message" android:layout_below="@+id/textView2" android:layout_centerHorizontal="true" android:layout_marginTop="42dp" /> <Button android:id="@+id/button" android:layout_width="150dp" android:layout_height="wrap_content" android:layout_below="@+id/imageButton" android:layout_centerHorizontal="true" android:layout_marginTop="62dp" android:background="@android:color/holo_red_dark" android:text="Notification" android:onClick="notification" android:textColor="@android:color/background_light" /> </RelativeLayout> ``` 現在為通知創建另一個布局,這是`activity_notification_bar.xml`的代碼 ```java <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="400dp" android:text="You have a new unread message...." /> </LinearLayout> ``` 這是`NotificationBar.java` ```java package com.example.admin.androidnotifications; import android.os.Bundle; import android.app.Activity; public class NotificationBar extends Activity{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notification_bar); } } ``` 現在打開您的`MainActivity.java`并粘貼以下代碼 ```java package com.example.admin.androidnotifications; import android.os.Bundle; import android.app.Activity; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.support.v4.app.NotificationCompat; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void notification(View view) { addNotification(); } private void addNotification() { NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.message) .setContentTitle("Unread Message") //this is the title of notification .setColor(101) .setContentText("You have an unread message."); //this is the message showed in notification Intent intent = new Intent(this, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(contentIntent); // Add as notification NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(0, builder.build()); } } ``` 現在運行您的應用,這是輸出 ![notification example](https://img.kancloud.cn/fb/03/fb034bdee142d406165d31a035b575b7_370x654.jpg) 通知示例 單擊“通知”按鈕后,新的通知將出現在狀態欄中。 這是它的樣子 ![new notification](https://img.kancloud.cn/5e/f3/5ef346a6ec157adc08985613028450d2_367x654.jpg) 新通知 您可以通過單擊[鏈接](https://github.com/JavaTutorialNetwork/Tutorials/blob/master/AndroidNotifications.rar)下載此項目。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看