Android 多媒体(一)——使用通知
生活随笔
收集整理的這篇文章主要介紹了
Android 多媒体(一)——使用通知
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
通知可以在活動(dòng)、廣播接收器,服務(wù)里創(chuàng)建
一、通知基本用法
新建一個(gè)空項(xiàng)目 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="發(fā)起通知"/></LinearLayout>主活動(dòng):
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("這是標(biāo)題").setAutoCancel(true).setContentText("這是文本").setContentInfo("這是內(nèi)容").setSubText("這是小字").setTicker("滾動(dòng)消息......").setWhen(System.currentTimeMillis()) // 出現(xiàn)時(shí)間.setSmallIcon(R.mipmap.ic_launcher) //小圖標(biāo).setContentIntent(pi) //大圖標(biāo).setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)); // 大圖標(biāo)Notification notification = builder.build();manager.notify(1, notification);break;default:break;}} }三、通知的響應(yīng)
通知響應(yīng)依賴于PendingIntent,分別用于活動(dòng),廣播,和服務(wù),其對(duì)應(yīng)靜態(tài)方法:getActivity(),getBroadcast(),getService()
- 第一個(gè)參數(shù):Context
- 第二個(gè)參數(shù):通常直接寫0
- 第三個(gè)參數(shù):Intent
- 第四個(gè)參數(shù):可以選擇四個(gè):FLAG_ONE_SHOT,FLAG_NO_CREATE,FLAG_CANCEL_CURRENT,FLAG_UPDATE_CURRENT。但通常寫0
新建一個(gè)空活動(dòng) NotificationActivity,對(duì)應(yīng)布局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="這一頁(yè)是通知布局"/></RelativeLayout>主活動(dòng)給通知加入點(diǎn)擊功能:
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("這是標(biāo)題").setContentText("這是文本").setContentInfo("這是內(nèi)容").setSubText("這是小字").setTicker("滾動(dòng)消息......").setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher).setContentIntent(pi).setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));。。。。。。運(yùn)行:
讓通知消失的兩種方法:
這里的 1,是通知的id,之前設(shè)置好的。
四、通知的進(jìn)階技巧
設(shè)置通知的音效:
builder.setSound(Uri.fromFile(new File("鈴聲路徑")));設(shè)置振動(dòng)【偶數(shù)表示振動(dòng)的毫秒數(shù),奇數(shù)表示接著靜止的毫秒數(shù)】:
builder.setVibrate(new long[]{0, 1000, 1000, 1000}); // 8.0 以上要添加這個(gè): channel.setVibrationPattern(pattern);不過(guò)設(shè)置振動(dòng)需要先添加權(quán)限:
<uses-permission android:name="android.permission.VIBRATE"/>顯示長(zhǎng)文字:
builder.setStyle(new NotificationCompat.BigTextStyle().bigText("唧唧復(fù)唧唧,木蘭當(dāng)戶織。不聞機(jī)杼聲,惟聞女嘆息。\n" +"問(wèn)女何所思,問(wèn)女何所憶。女亦無(wú)所思,女亦無(wú)所憶。昨夜見(jiàn)軍帖,可汗大點(diǎn)兵,軍書十二卷,卷卷有爺名。阿爺無(wú)大兒,木蘭無(wú)長(zhǎng)兄,愿為市鞍馬,從此替爺征。\n" +"東市買駿馬,西市買鞍韉,南市買轡頭,北市買長(zhǎng)鞭。旦辭爺娘去,暮宿黃河邊,不聞爺娘喚女聲,但聞黃河流水鳴濺濺。旦辭黃河去,暮至黑山頭,不聞爺娘喚女聲,但聞燕山胡騎鳴啾啾。\n" +"萬(wàn)里赴戎機(jī),關(guān)山度若飛。朔氣傳金柝,寒光照鐵衣。將軍百戰(zhàn)死,壯士十年歸。\n" +"歸來(lái)見(jiàn)天子,天子坐明堂。策勛十二轉(zhuǎn),賞賜百千強(qiáng)。可汗問(wèn)所欲,木蘭不用尚書郎,愿馳千里足,送兒還故鄉(xiāng)。\n" +"爺娘聞女來(lái),出郭相扶將;阿姊聞妹來(lái),當(dāng)戶理紅妝;小弟聞姊來(lái),磨刀霍霍向豬羊。開我東閣門,坐我西閣床,脫我戰(zhàn)時(shí)袍,著我舊時(shí)裳。當(dāng)窗理云鬢,對(duì)鏡帖花黃。出門看火伴,火伴皆驚忙:同行十二年,不知木蘭是女郎。\n" +"雄兔腳撲朔,雌兔眼迷離;雙兔傍地走,安能辨我是雄雌?"))
顯示圖片:
設(shè)置重要程度:
總結(jié)
以上是生活随笔為你收集整理的Android 多媒体(一)——使用通知的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 如何用Postman测试整套接口?测试流
- 下一篇: java模拟时钟源代码_Android实