運(yùn)行有問(wèn)題或需要全部資源請(qǐng)點(diǎn)贊關(guān)注收藏后評(píng)論區(qū)留言~~~
一、通知推送Ntification
在APP的運(yùn)行過(guò)程中,為了讓用戶(hù)及時(shí)收到某些消息,有必要由App主動(dòng)向用戶(hù)推送消息通知,以免錯(cuò)過(guò)有價(jià)值的信息。
在手機(jī)屏幕的頂端下拉會(huì)彈出通知欄,里面存放的便是App主動(dòng)推給用戶(hù)的提醒消息,消息通知的組成內(nèi)容由Notification類(lèi)所描述,每條消息通知都有消息圖標(biāo),消息標(biāo)題,消息內(nèi)容等基本元素,時(shí)不時(shí)還有附加文本,進(jìn)度條,計(jì)時(shí)器等額外元素。
并且Notification僅僅描述了消息通知的組成內(nèi)容,實(shí)際推送工作還需要由通知管理器NotificationManager執(zhí)行。
下面以發(fā)送簡(jiǎn)單消息為例 效果如下,如果你連接了真機(jī)則手機(jī)上會(huì)彈出所輸入信息的彈框
?
代碼如下
Java類(lèi)?
package com.example.chapter11;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.chapter11.util.ViewUtil;
public class NotifySimpleActivity extends AppCompatActivity implements View.OnClickListener {
private EditText et_title;
private EditText et_message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notify_simple);
et_title = findViewById(R.id.et_title);
et_message = findViewById(R.id.et_message);
findViewById(R.id.btn_send_simple).setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_send_simple) {
ViewUtil.hideOneInputMethod(this, et_message); // 隱藏輸入法軟鍵盤(pán)
if (TextUtils.isEmpty(et_title.getText())) {
Toast.makeText(this, "請(qǐng)?zhí)顚?xiě)消息標(biāo)題", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(et_message.getText())) {
Toast.makeText(this, "請(qǐng)?zhí)顚?xiě)消息內(nèi)容", Toast.LENGTH_SHORT).show();
return;
}
String title = et_title.getText().toString();
String message = et_message.getText().toString();
sendSimpleNotify(title, message); // 發(fā)送簡(jiǎn)單的通知消息
}
}
// 發(fā)送簡(jiǎn)單的通知消息(包括消息標(biāo)題和消息內(nèi)容)
private void sendSimpleNotify(String title, String message) {
// 發(fā)送消息之前要先創(chuàng)建通知渠道,創(chuàng)建代碼見(jiàn)MainApplication.java
// 創(chuàng)建一個(gè)跳轉(zhuǎn)到活動(dòng)頁(yè)面的意圖
Intent clickIntent = new Intent(this, MainActivity.class);
// 創(chuàng)建一個(gè)用于頁(yè)面跳轉(zhuǎn)的延遲意圖
PendingIntent contentIntent = PendingIntent.getActivity(this,
R.string.app_name, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// 創(chuàng)建一個(gè)通知消息的建造器
Notification.Builder builder = new Notification.Builder(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Android 8.0開(kāi)始必須給每個(gè)通知分配對(duì)應(yīng)的渠道
builder = new Notification.Builder(this, getString(R.string.app_name));
}
builder.setContentIntent(contentIntent) // 設(shè)置內(nèi)容的點(diǎn)擊意圖
.setAutoCancel(true) // 點(diǎn)擊通知欄后是否自動(dòng)清除該通知
.setSmallIcon(R.mipmap.ic_launcher) // 設(shè)置應(yīng)用名稱(chēng)左邊的小圖標(biāo)
.setSubText("這里是副本") // 設(shè)置通知欄里面的附加說(shuō)明文本
// 設(shè)置通知欄右邊的大圖標(biāo)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_app))
.setContentTitle(title) // 設(shè)置通知欄里面的標(biāo)題文本
.setContentText(message); // 設(shè)置通知欄里面的內(nèi)容文本
Notification notify = builder.build(); // 根據(jù)通知建造器構(gòu)建一個(gè)通知對(duì)象
// 從系統(tǒng)服務(wù)中獲取通知管理器
NotificationManager notifyMgr = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
// 使用通知管理器推送通知,然后在手機(jī)的通知欄就會(huì)看到該消息
notifyMgr.notify(R.string.app_name, notify);
}
}
XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="消息標(biāo)題:"
android:textColor="@color/black"
android:textSize="17sp" />
<EditText
android:id="@+id/et_title"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="5dp"
android:background="@drawable/editext_selector"
android:hint="請(qǐng)?zhí)顚?xiě)消息標(biāo)題"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="消息內(nèi)容:"
android:textColor="@color/black"
android:textSize="17sp" />
<EditText
android:id="@+id/et_message"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="top"
android:layout_margin="5dp"
android:background="@drawable/editext_selector"
android:hint="請(qǐng)?zhí)顚?xiě)消息內(nèi)容"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
<Button
android:id="@+id/btn_send_simple"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="發(fā)送簡(jiǎn)單消息"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
二、包含計(jì)時(shí)器與進(jìn)度條
如果消息通知包含計(jì)時(shí)器與進(jìn)度條,則需要調(diào)用消息建造器的setUsesChronometer與setProgress方法,效果如下
包含進(jìn)度條不僅更加美觀而且讓用戶(hù)能實(shí)時(shí)看見(jiàn)App的進(jìn)展情況
?代碼如下文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-403501.html
Java類(lèi)
package com.example.chapter11;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.chapter11.util.ViewUtil;
public class NotifyCounterActivity extends AppCompatActivity implements View.OnClickListener {
private EditText et_title;
private EditText et_message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notify_counter);
et_title = findViewById(R.id.et_title);
et_message = findViewById(R.id.et_message);
findViewById(R.id.btn_send_counter).setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_send_counter) {
ViewUtil.hideOneInputMethod(this, et_message); // 隱藏輸入法軟鍵盤(pán)
if (TextUtils.isEmpty(et_title.getText())) {
Toast.makeText(this, "請(qǐng)?zhí)顚?xiě)消息標(biāo)題", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(et_message.getText())) {
Toast.makeText(this, "請(qǐng)?zhí)顚?xiě)消息內(nèi)容", Toast.LENGTH_SHORT).show();
return;
}
String title = et_title.getText().toString();
String message = et_message.getText().toString();
sendCounterNotify(title, message); // 發(fā)送計(jì)時(shí)的通知消息
}
}
// 發(fā)送計(jì)時(shí)的通知消息
private void sendCounterNotify(String title, String message) {
// 發(fā)送消息之前要先創(chuàng)建通知渠道,創(chuàng)建代碼見(jiàn)MainApplication.java
// 創(chuàng)建一個(gè)跳轉(zhuǎn)到活動(dòng)頁(yè)面的意圖
Intent cancelIntent = new Intent(this, MainActivity.class);
// 創(chuàng)建一個(gè)用于頁(yè)面跳轉(zhuǎn)的延遲意圖
PendingIntent deleteIntent = PendingIntent.getActivity(this,
R.string.app_name, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// 創(chuàng)建一個(gè)通知消息的建造器
Notification.Builder builder = new Notification.Builder(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Android 8.0開(kāi)始必須給每個(gè)通知分配對(duì)應(yīng)的渠道
builder = new Notification.Builder(this, getString(R.string.app_name));
}
builder.setDeleteIntent(deleteIntent) // 設(shè)置內(nèi)容的清除意圖
.setSmallIcon(R.mipmap.ic_launcher) // 設(shè)置應(yīng)用名稱(chēng)左邊的小圖標(biāo)
// 設(shè)置通知欄右邊的大圖標(biāo)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_app))
.setProgress(100, 60, false) // 設(shè)置進(jìn)度條及其具體進(jìn)度
.setUsesChronometer(true) // 設(shè)置是否顯示計(jì)時(shí)器
.setContentTitle(title) // 設(shè)置通知欄里面的標(biāo)題文本
.setContentText(message); // 設(shè)置通知欄里面的內(nèi)容文本
Notification notify = builder.build(); // 根據(jù)通知建造器構(gòu)建一個(gè)通知對(duì)象
// 從系統(tǒng)服務(wù)中獲取通知管理器
NotificationManager notifyMgr = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
// 使用通知管理器推送通知,然后在手機(jī)的通知欄就會(huì)看到該消息
notifyMgr.notify(R.string.app_name, notify);
}
}
XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="消息標(biāo)題:"
android:textColor="@color/black"
android:textSize="17sp" />
<EditText
android:id="@+id/et_title"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="5dp"
android:background="@drawable/editext_selector"
android:hint="請(qǐng)?zhí)顚?xiě)消息標(biāo)題"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="消息內(nèi)容:"
android:textColor="@color/black"
android:textSize="17sp" />
<EditText
android:id="@+id/et_message"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="top"
android:layout_margin="5dp"
android:background="@drawable/editext_selector"
android:hint="請(qǐng)?zhí)顚?xiě)消息內(nèi)容"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
<Button
android:id="@+id/btn_send_counter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="發(fā)送計(jì)時(shí)消息"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
創(chuàng)作不易 覺(jué)得有幫助請(qǐng)點(diǎn)贊關(guān)注收藏~~~文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-403501.html
到了這里,關(guān)于Android Studio App開(kāi)發(fā)之通知推送Notification的講解及實(shí)戰(zhàn)(給用戶(hù)推送信息實(shí)戰(zhàn))的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!