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

歡迎訪問 生活随笔!

生活随笔

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

Android

Android 多媒体(一)——使用通知

發布時間:2024/3/26 Android 52 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 多媒体(一)——使用通知 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

通知可以在活動、廣播接收器,服務里創建

一、通知基本用法

  • 用Context.getSystemService(Context.NOTIFICATION_SERVICE)獲取到NotificationManager對通知管理:
  • NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)
  • 用Builder構造器創建Notification對象,channelId用來表示消息通道的ID,自定義一個填上即可
  • NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "unique");
  • 基本設置
  • builder.setContentTitle("這是標題").setAutoCancel(true).setContentText("這是文本").setContentInfo("這是內容").setSubText("這是小字").setTicker("滾動消息......").setWhen(System.currentTimeMillis()) // 出現時間.setSmallIcon(R.mipmap.ic_launcher) //小圖標.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)); // 大圖標// 大于Android 8.0的版本適配 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {NotificationChannel notificationChannel = null;notificationChannel = new NotificationChannel("unique", "TEST", NotificationManager.IMPORTANCE_HIGH);notificationChannel.enableLights(true); //LED燈notificationChannel.setLightColor(Color.YELLOW);manager.createNotificationChannel(notificationChannel);}
  • 顯示通知,第一個參數是自定義的id,第二個參數是對象
  • Notification notification = builder.build(); manager.notify(1, notification);

    新建一個空項目 day14_NotificationTest

    主布局:

    <?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:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/send_notice"android:text="發起通知"/></LinearLayout>

    主活動:

    public class MainActivity extends AppCompatActivity implements View.OnClickListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button button_sendNotice = findViewById(R.id.send_notice);button_sendNotice.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.send_notice:Intent intent = new Intent(this, NotificationActivity.class);PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "unique");builder.setContentTitle("這是標題").setAutoCancel(true).setContentText("這是文本").setContentInfo("這是內容").setSubText("這是小字").setTicker("滾動消息......").setWhen(System.currentTimeMillis()) // 出現時間.setSmallIcon(R.mipmap.ic_launcher) //小圖標.setContentIntent(pi) //大圖標.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)); // 大圖標Notification notification = builder.build();manager.notify(1, notification);break;default:break;}} }

    三、通知的響應

    通知響應依賴于PendingIntent,分別用于活動,廣播,和服務,其對應靜態方法:getActivity(),getBroadcast(),getService()

    • 第一個參數:Context
    • 第二個參數:通常直接寫0
    • 第三個參數:Intent
    • 第四個參數:可以選擇四個:FLAG_ONE_SHOT,FLAG_NO_CREATE,FLAG_CANCEL_CURRENT,FLAG_UPDATE_CURRENT。但通常寫0

    新建一個空活動 NotificationActivity,對應布局notification_layout

    通知布局:

    <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:textSize="24sp"android:text="這一頁是通知布局"/></RelativeLayout>

    主活動給通知加入點擊功能:

    public void onClick(View v) {switch (v.getId()) {case R.id.send_notice:Intent intent = new Intent(this, NotificationActivity.class);PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "001");builder.setContentTitle("這是標題").setContentText("這是文本").setContentInfo("這是內容").setSubText("這是小字").setTicker("滾動消息......").setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher).setContentIntent(pi).setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));。。。。。。

    運行:

    讓通知消失的兩種方法:

  • 在NotificationCompat.Builder中連綴一個setAutoCancel(true)方法,通知被點擊后就會自動清除:
  • NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "001").setAutoCancel(true);
  • 顯示調用NotificationManger的cancel()將它取消:
  • public class NotificationActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.notification_layout);NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);manager.cancel(1);} }

    這里的 1,是通知的id,之前設置好的。

    四、通知的進階技巧

    設置通知的音效:

    builder.setSound(Uri.fromFile(new File("鈴聲路徑")));

    設置振動【偶數表示振動的毫秒數,奇數表示接著靜止的毫秒數】:

    builder.setVibrate(new long[]{0, 1000, 1000, 1000}); // 8.0 以上要添加這個: channel.setVibrationPattern(pattern);

    不過設置振動需要先添加權限:

    <uses-permission android:name="android.permission.VIBRATE"/>

    顯示長文字:

    builder.setStyle(new NotificationCompat.BigTextStyle().bigText("唧唧復唧唧,木蘭當戶織。不聞機杼聲,惟聞女嘆息。\n" +"問女何所思,問女何所憶。女亦無所思,女亦無所憶。昨夜見軍帖,可汗大點兵,軍書十二卷,卷卷有爺名。阿爺無大兒,木蘭無長兄,愿為市鞍馬,從此替爺征。\n" +"東市買駿馬,西市買鞍韉,南市買轡頭,北市買長鞭。旦辭爺娘去,暮宿黃河邊,不聞爺娘喚女聲,但聞黃河流水鳴濺濺。旦辭黃河去,暮至黑山頭,不聞爺娘喚女聲,但聞燕山胡騎鳴啾啾。\n" +"萬里赴戎機,關山度若飛。朔氣傳金柝,寒光照鐵衣。將軍百戰死,壯士十年歸。\n" +"歸來見天子,天子坐明堂。策勛十二轉,賞賜百千強。可汗問所欲,木蘭不用尚書郎,愿馳千里足,送兒還故鄉。\n" +"爺娘聞女來,出郭相扶將;阿姊聞妹來,當戶理紅妝;小弟聞姊來,磨刀霍霍向豬羊。開我東閣門,坐我西閣床,脫我戰時袍,著我舊時裳。當窗理云鬢,對鏡帖花黃。出門看火伴,火伴皆驚忙:同行十二年,不知木蘭是女郎。\n" +"雄兔腳撲朔,雌兔眼迷離;雙兔傍地走,安能辨我是雄雌?"))


    顯示圖片:

    builder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.cat)))


    設置重要程度:

    builder.setPriority(NotificationCompat.PRIORITY_MAX)

    總結

    以上是生活随笔為你收集整理的Android 多媒体(一)——使用通知的全部內容,希望文章能夠幫你解決所遇到的問題。

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