Kotlin コースの学習 - 携帯電話通知 (携帯電話にソフトウェアをインストールする方法)

目次

1. 基礎知識

(1) 通知の作成

1.チャンネルID

2.チャンネル名

3. 通知の重要度

(2) 通知設定

(3) pendingItent と Itent の違い

意図

保留中の意図 

2. 準備作業

(1) 携帯電話の設定 (個人の携帯電話のモデルに応じて、オンラインにアクセスして開発者モードの設定を見つけます)

1.接続する

2. 設定を変更します(そうしないと電話機にインストールできません)

3. USB接続方法を以下のように変更します。

(2) パソコンの設定

3. プロジェクトの構築

(1) レイアウトの設定

(2) プロジェクトの準備

1. 認可通知

2. 接続を確立する

3. 現在のシステムのサービスを取得します -- 通知サービス

4. 通知を作成する

5. クリック後のポップアップ通知を実現するための監視を確立します。

(3) 完全なコード

AndroidManifest.xml

アクティビティ.xml

アクティビティ.kt

通知アクティビティ.kt


1. 基礎知識

(1) 通知の作成

通知チャネルを作成するには、チャネル ID、チャネル名、重要度レベルの少なくとも 3 つのパラメータを知っている必要があります。

1.チャンネルID

任意に定義できますが、グローバルに一意である必要があります。

2.チャンネル名

ユーザーが見てわかるように、チャンネルの目的を表現します。

3. 通知の重要度

主に重要度に応じて IMPORTANCE_HIGH、IMPORTANCE_DEFAUILT、IMPORTANCE_LOW、IMPORTANCE_MIN があり、重要度によって通知の動作が異なります。

(2) 通知設定

メソッド名 使用
.setContentTitle() 表題を加える
.setContentText() 表示内容を追加する
.setStyle(NotificationCompat.BigTextStyle().bigText()) 長いコンテンツを追加する (クリックして表示)
.setSmallIcon() シンプルな小さなアイコンを設定します
.setLargeIcon() 大きなアイコンを設定する
.setAutoCancel() クリック後に通知を消すかどうかを設定します(trueは消えます、falseは消えません)
.setContentIntent(pendingIntent) 設定した pendingIntent をロードし、通知バーの情報の後のジャンプをクリックします。

(3) pendingItent と Itent の違い

意図

 タイムリーに実行を開始できます。

ペンディンテント 

実行が遅延した Itent では、getActivit、getBroadcas、および getService を通じて保留中のインテントのインスタンスを取得する必要があります。クリックすると表示・実行できます。

2. 準備作業

(1) 携帯電話の設定 (個人の携帯電話のモデルに応じて、オンラインにアクセスして開発者モードの設定を見つけます)

1.接続する

コンピュータに接続するための携帯電話データケーブル

2. 設定を変更します(そうしないと電話機にインストールできません)

システムとアップデート~開発者向けオプション~デバッグ~USB デバッグ (有効)
次のような表示があります:

3. USB接続方法を以下のように変更します。

(2) パソコンの設定

上記の設定が成功したら、項目の下にある Locat をクリックし、プルダウンして携帯電話を選択すると、接続が成功します。

3. プロジェクトの構築

(1) レイアウトの設定

<Button
        android:id="@+id/buttton1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="notice"
        />

(2) プロジェクトの準備

1. 認可通知

AndroidManifest.xml に次の権限付与を追加します。

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

2. 接続を確立する

val sendNotice:Button=findViewById(R.id.buttton1)

3. 現在のシステムのサービスを取得します -- 通知サービス

val manager=getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

4. 通知を作成する

if (Build.VERSION.SDK_INT >= 26){//本项目是26,可写为Build.VERSION_CODES.O来获取
            //8.0引入channel
            //创建通道,第一个ID,第二个名称,第三个重要等级
            val channel = NotificationChannel("normal","warn",NotificationManager.IMPORTANCE_DEFAULT)
            //normal全局为1
            manager.createNotificationChannel(channel)//创建
        }

5. クリック後のポップアップ通知を実現するための監視を確立します。

sendNotice.setOnClickListener {
            val intent = Intent(this,NotificationActivity::class.java)//引用
            val pi = PendingIntent.getActivity(this,0,intent,0)
            val notification = NotificationCompat.Builder(this,"normal")
        //context是上下文指针,channelled与上文id匹配
                .setContentTitle("警告")
                .setContentText("前方的区域以后再来探索吧")
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setStyle(NotificationCompat.BigTextStyle().bigText("1 调试自己的手机,配置自己的手机为开发者模式\n" +
                        "\n" +
                        "2 运行安卓,在模拟器运行选择上,选择自己的手机\n" +
                        "\n" +
                        "3 创建3个通知,观察每个通知的详细内容\n"))
                .setAutoCancel(true)//点进通告,通知栏的通告会消失

                .setContentIntent(pi)//载入pi
                .build()
            manager.notify(1,notification)//第一个参数是通知标签
        }

(3) 完全なコード

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <!--    授权通知-->
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.124"
        tools:targetApi="31">
        <activity
            android:name=".NotificationActivity"
            android:exported="false" />
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

アクティビティ.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/buttton1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="notice"
        />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

アクティビティ.kt

package com.example.a124

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import androidx.core.app.NotificationCompat

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val sendNotice:Button=findViewById(R.id.buttton1)
        val manager=getSystemService(Context.NOTIFICATION_SERVICE) as //获取相应服务
                NotificationManager
        if (Build.VERSION.SDK_INT >= 26){
            //8.0引入channel
            //创建通道
            val channel = NotificationChannel("normal","warn",NotificationManager.IMPORTANCE_DEFAULT)
            //normal全局为1
            manager.createNotificationChannel(channel)//创建
        }
        sendNotice.setOnClickListener {
            val intent = Intent(this,NotificationActivity::class.java)//引用
            val pi = PendingIntent.getActivity(this,0,intent,0)
            val notification = NotificationCompat.Builder(this,"normal")
        //context是上下文指针,channelled与上文id匹配
                .setContentTitle("警告")
                .setContentText("前方的区域以后再来探索吧")
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setStyle(NotificationCompat.BigTextStyle().bigText("1 调试自己的手机,配置自己的手机为开发者模式\n" +
                        "\n" +
                        "2 运行安卓,在模拟器运行选择上,选择自己的手机\n" +
                        "\n" +
                        "3 创建3个通知,观察每个通知的详细内容\n"))
                .setAutoCancel(true)//点进通告,通知栏的通告会消失

                .setContentIntent(pi)//载入pi
                .build()
            manager.notify(1,notification)//第一个参数是通知标签
        }

    }
}

通知アクティビティ.kt

package com.example.a124

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class NotificationActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_notification)
    }
}

おすすめ

転載: blog.csdn.net/m0_61059796/article/details/130754638