Android开发笔记——Android 9发送通知
生活随笔
收集整理的這篇文章主要介紹了
Android开发笔记——Android 9发送通知
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Android 9 發(fā)送通知
- 發(fā)送通知
- 布局文件
- 程序代碼
- 運(yùn)行截圖
發(fā)送通知
布局文件
<?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;// 通知相關(guān)// 渠道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", "點(diǎn)擊按鈕!");sendNotification("還不趕緊關(guān)注公眾號(hào)", "點(diǎn)擊查看詳情!");break;default:break;}}private void sendNotification(String title, String content) {Notification notification;if (Build.VERSION.SDK_INT >= 26) {//當(dāng)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 {//當(dāng)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() {//當(dāng)sdk版本大于26if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//創(chuàng)建通知渠道CharSequence name = "fanchen_notify";String description = "fanchen的通知渠道";//重要性級(jí)別 這里用默認(rèn)的int importance = NotificationManager.IMPORTANCE_DEFAULT;NotificationChannel mChannel = null;mChannel = new NotificationChannel(channelId, name, importance);//渠道描述mChannel.setDescription(description);//是否顯示通知指示燈mChannel.enableLights(true);//是否振動(dòng)mChannel.enableVibration(true);//創(chuàng)建通知渠道notificationManager.createNotificationChannel(mChannel);}} }運(yùn)行截圖
總結(jié)
以上是生活随笔為你收集整理的Android开发笔记——Android 9发送通知的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: log4j2配置
- 下一篇: 剑指 Offer II 014. 字符串