日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android开发笔记——Android 9发送通知

發布時間:2024/4/11 Android 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android开发笔记——Android 9发送通知 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Android 9 發送通知

  • 發送通知
    • 布局文件
    • 程序代碼
    • 運行截圖

發送通知

布局文件

<?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"><Buttonandroid:id="@+id/send_notify"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:text="send_notify" /> </LinearLayout>

程序代碼

package cn.fanchencloud.studyapplication;import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.content.Context; import android.os.Build; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button;import androidx.annotation.RequiresApi; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.NotificationCompat;import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter;public class MainActivity extends AppCompatActivity implements View.OnClickListener {/*** 通知<p>id</p>*/private static final int NOTIFY_ID = 0x123;private Button sendNoticeButton;// 通知相關// 渠道idprivate String channelId = "channelId1";private NotificationManager notificationManager;@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//新建通知管理器notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);addNotificationChannel();sendNoticeButton = findViewById(R.id.send_notify);sendNoticeButton.setOnClickListener(this);}@Overrideprotected void onDestroy() {super.onDestroy();}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.send_notify:Log.d("MainActivity", "點擊按鈕!");sendNotification("還不趕緊關注公眾號", "點擊查看詳情!");break;default:break;}}private void sendNotification(String title, String content) {Notification notification;if (Build.VERSION.SDK_INT >= 26) {//當sdk版本大于26notification = new Notification.Builder(MainActivity.this, channelId).setCategory(Notification.CATEGORY_MESSAGE).setSmallIcon(R.mipmap.ic_launcher_copy).setContentTitle(title).setContentText(content).setAutoCancel(true).build();} else {//當sdk版本小于26notification = new NotificationCompat.Builder(MainActivity.this, channelId).setContentTitle("This is content title").setContentText("This is content text").setSmallIcon(R.mipmap.ic_launcher_copy).build();}notificationManager.notify(NOTIFY_ID, notification);}public void addNotificationChannel() {//當sdk版本大于26if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//創建通知渠道CharSequence name = "fanchen_notify";String description = "fanchen的通知渠道";//重要性級別 這里用默認的int importance = NotificationManager.IMPORTANCE_DEFAULT;NotificationChannel mChannel = null;mChannel = new NotificationChannel(channelId, name, importance);//渠道描述mChannel.setDescription(description);//是否顯示通知指示燈mChannel.enableLights(true);//是否振動mChannel.enableVibration(true);//創建通知渠道notificationManager.createNotificationChannel(mChannel);}} }

運行截圖

總結

以上是生活随笔為你收集整理的Android开发笔记——Android 9发送通知的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。