国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Android Studio App開(kāi)發(fā)之通知推送Notification的講解及實(shí)戰(zhàn)(給用戶(hù)推送信息實(shí)戰(zhàn))

這篇具有很好參考價(jià)值的文章主要介紹了Android Studio App開(kāi)發(fā)之通知推送Notification的講解及實(shí)戰(zhàn)(給用戶(hù)推送信息實(shí)戰(zhàn))。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

運(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ì)彈出所輸入信息的彈框

Android Studio App開(kāi)發(fā)之通知推送Notification的講解及實(shí)戰(zhàn)(給用戶(hù)推送信息實(shí)戰(zhàn))

?Android Studio App開(kāi)發(fā)之通知推送Notification的講解及實(shí)戰(zhàn)(給用戶(hù)推送信息實(shí)戰(zhàn))

代碼如下

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)展情況

Android Studio App開(kāi)發(fā)之通知推送Notification的講解及實(shí)戰(zhàn)(給用戶(hù)推送信息實(shí)戰(zhàn))

?代碼如下

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)!

本文來(lái)自互聯(lián)網(wǎng)用戶(hù)投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 【android開(kāi)發(fā)-20】android中notification的用法講解

    【android開(kāi)發(fā)-20】android中notification的用法講解

    1, notification的基本用法 在Android中,通知(Notification)是一種在狀態(tài)欄上顯示的消息提示,用戶(hù)點(diǎn)擊通知后可以展開(kāi)更多詳細(xì)信息。以下是基本的通知用法: 1,創(chuàng)建通知 創(chuàng)建通知需要使用Notification類(lèi),可以通過(guò)以下代碼創(chuàng)建一個(gè)簡(jiǎn)單的通知: 2,創(chuàng)建通知通道 從Android 8.0開(kāi)

    2024年02月04日
    瀏覽(34)
  • Android Studio App開(kāi)發(fā)之通知渠道NotificationChannel及給華為、小米手機(jī)桌面應(yīng)用添加消息數(shù)量角標(biāo)實(shí)戰(zhàn)(包括消息重要級(jí)別的設(shè)置 附源碼)

    Android Studio App開(kāi)發(fā)之通知渠道NotificationChannel及給華為、小米手機(jī)桌面應(yīng)用添加消息數(shù)量角標(biāo)實(shí)戰(zhàn)(包括消息重要級(jí)別的設(shè)置 附源碼)

    需要全部源碼或運(yùn)行有問(wèn)題請(qǐng)點(diǎn)贊關(guān)注收藏后評(píng)論區(qū)留言~~~ 為了分清消息通知的輕重緩急,Android8.0新增了通知渠道,并且必須指定通知渠道才能正常推送消息,一個(gè)應(yīng)用允許擁有多個(gè)通知渠道,每個(gè)渠道的重要性各不相同,有的渠道消息在通知欄被折疊成小行,有的渠道消

    2024年02月16日
    瀏覽(25)
  • Android 之 Notification (狀態(tài)欄通知)詳解

    Android 之 Notification (狀態(tài)欄通知)詳解

    本節(jié)帶來(lái)的是Android中用于在狀態(tài)欄顯示通知信息的控件:Notification,相信大部分 學(xué)Android都對(duì)他都很熟悉,而網(wǎng)上很多關(guān)于Notification的使用教程都是基于2.x的,而 現(xiàn)在普遍的Android設(shè)備基本都在4.x以上,甚至是5.0以上的都有;他們各自的Notification 都是不一樣的!而本節(jié)給大家

    2024年02月15日
    瀏覽(14)
  • Android Studio App開(kāi)發(fā)之網(wǎng)絡(luò)通信中使用GET方式調(diào)用HTTP接口的講解及實(shí)戰(zhàn)(附源碼 超詳細(xì)必看)

    Android Studio App開(kāi)發(fā)之網(wǎng)絡(luò)通信中使用GET方式調(diào)用HTTP接口的講解及實(shí)戰(zhàn)(附源碼 超詳細(xì)必看)

    運(yùn)行有問(wèn)題或需要源碼請(qǐng)點(diǎn)贊關(guān)注收藏后評(píng)論區(qū)留言~~~ Android開(kāi)發(fā)采用Java作為編程語(yǔ)言,也就沿用了Java的HTTP連接工具HttpURLConnection,不管是訪(fǎng)問(wèn)HTTP接口還是上傳或下載文件都是用它來(lái)實(shí)現(xiàn)。它有幾個(gè)關(guān)鍵點(diǎn) 1:HttpURLConnection默認(rèn)采取國(guó)際通行的UTF-8編碼,中文用GBK編碼 2:多數(shù)

    2024年02月05日
    瀏覽(22)
  • flutter開(kāi)發(fā)實(shí)戰(zhàn)-實(shí)現(xiàn)推送功能Push Notification

    flutter開(kāi)發(fā)實(shí)戰(zhàn)-實(shí)現(xiàn)推送功能Push Notification

    flutter開(kāi)發(fā)實(shí)戰(zhàn)-實(shí)現(xiàn)推送功能Push Notification 推送服務(wù)現(xiàn)在可以說(shuō)是所有 App 的標(biāo)配了,最近在Flutter工程項(xiàng)目上實(shí)現(xiàn)推送功能。flutter上實(shí)現(xiàn)推送功能需要依賴(lài)原生的功能,需要插件實(shí)現(xiàn),這里使用的是極光推送的服務(wù)。 效果圖如下 在使用極光推送功能時(shí),需要使用的是極光提

    2024年02月16日
    瀏覽(15)
  • 使用Android Studio 利用極光推送SDK 制作手機(jī) APP 實(shí)現(xiàn)遠(yuǎn)程測(cè)試技術(shù) (第一部)

    總參考文章:https://blog.csdn.net/qq_38436214/article/details/105073213 Android Studio 安裝配置教程 - Windows(詳細(xì)版) 1.JDK 安裝與環(huán)境變量配置(Win10詳細(xì)版) 《jdk-8u371-windows-i586.exe》 https://blog.csdn.net/qq_38436214/article/details/105071088 此時(shí)會(huì)讓登錄賬號(hào)密碼: https://login.oracle.com/mysso/signon.jsp 賬號(hào):

    2024年02月03日
    瀏覽(24)
  • uni-app Android studio 本地打包 【圖文講解】

    uni-app Android studio 本地打包 【圖文講解】

    需要修改文件列表 appsrcmainresvaluesstrings.xml 修改app名稱(chēng) appsrcmainresvaluesAndroidManifest.xml 修改 包名 以及 uni-app 開(kāi)發(fā)者后臺(tái)生成的 離線(xiàn)打包可key simpleDemo 目錄下的 準(zhǔn)備安卓開(kāi)發(fā)環(huán)境 (這里忽略,之前有文章講解) 安卓開(kāi)發(fā)環(huán)境 https://nativesupport.dcloud.net.cn/AppDocs/download/an

    2024年02月03日
    瀏覽(24)
  • android.app.RemoteServiceException: Bad notification for startForeground

    android.app.RemoteServiceException: Bad notification for startForeground

    異常如下: 解決方法是 在 Android 8.0上 創(chuàng)建一個(gè) NotificationChannel,代碼如下 2 Android 中的 Service Service 是一種可在后臺(tái)執(zhí)行長(zhǎng)時(shí)間運(yùn)行操作而不提供界面的應(yīng)用組件。 服務(wù)可由其他應(yīng)用組件啟動(dòng),而且即使用戶(hù)切換到其他應(yīng)用,服務(wù)仍將在后臺(tái)繼續(xù)運(yùn)行。 此外,組件可通過(guò)綁

    2024年02月12日
    瀏覽(14)
  • Android Studio App入門(mén)之列表視圖ListView的講解及實(shí)戰(zhàn)(附源碼 超詳細(xì)必看)

    Android Studio App入門(mén)之列表視圖ListView的講解及實(shí)戰(zhàn)(附源碼 超詳細(xì)必看)

    需要圖片集或全部源碼請(qǐng)點(diǎn)贊關(guān)注收藏后評(píng)論區(qū)留言~~ 如果想在頁(yè)面上直接顯示全部列表信息,就要引入新的列表視圖ListView,列表視圖允許在頁(yè)面上分行展示相似的數(shù)據(jù)列表。 列表視圖新增的屬性與方法如下 divider 指定分割線(xiàn)的圖形 dividerHeight 指定分割線(xiàn)的高度 listSelecto

    2023年04月22日
    瀏覽(24)
  • android studio開(kāi)發(fā)app實(shí)例

    以下是一個(gè)簡(jiǎn)單的Android Studio開(kāi)發(fā)App的實(shí)例: 1. 打開(kāi)Android Studio,并創(chuàng)建一個(gè)新項(xiàng)目。 2. 選擇一個(gè)適當(dāng)?shù)膽?yīng)用程序名稱(chēng)和包名稱(chēng),然后選擇目標(biāo)API級(jí)別和默認(rèn)Activity的模板。 3. 在MainActivity.java文件中,添加以下代碼以配置MainActivity: ``` import android.os.Bundle; import androidx.appcompa

    2024年02月16日
    瀏覽(26)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包