Kotlin course learning - mobile phone notification (how to install the software on the mobile phone)

Table of contents

1. Basic knowledge

(1) Notification creation

1.Channel ID

2.Channel name

3. Importance level of notification

(2) Notification settings

(3) The difference between pendingItent and Itent

Intent

PendingIntent 

2. Preparation work

(1) Mobile phone settings (according to your personal mobile phone model, go online to find the developer mode settings)

1.Connect

2. Change the settings (otherwise it cannot be installed on the phone)

3. Change the USB connection method as follows

(2) Computer settings

3. Project construction

(1) Layout settings

(2) Project preparation

1. Authorization notification

2. Establish a connection

3. Get the services of the current system--notification service

4. Create notifications

5. Establish monitoring to realize pop-up notifications after clicking

(3) Complete code

AndroidManifest.xml

Activity.xml

Activity.kt

NotificationActivity.kt


1. Basic knowledge

(1) Notification creation

To create a notification channel, you need to know at least three parameters: channel ID, channel name, and importance level.

1.Channel ID

It can be defined arbitrarily, but it needs to be globally unique.

2.Channel name

For users to see, express the purpose of the channel.

3. Importance level of notification

There are mainly IMPORTANCE_HIGH, IMPORTANCE_DEFAUILT, IMPORTANCE_LOW, and IMPORTANCE_MIN, corresponding to the importance from high to low. Different importance levels determine different behaviors of notifications.

(2) Notification settings

method name use
.setContentTitle() Add title
.setContentText() Add display content
.setStyle(NotificationCompat.BigTextStyle().bigText()) Add long content (click to display)
.setSmallIcon() Set simple small icons
.setLargeIcon() Set large icons
.setAutoCancel() Set whether the notification disappears after clicking (true will disappear, false will not disappear)
.setContentIntent(pendingIntent) Load the pendingIntent you set and click the jump after the information in the notification bar

(3) The difference between pendingItent and Itent

Intent

 Execution can be started in a timely manner.

PendingIntent 

An Itent that is delayed in execution requires obtaining an instance of pending intent through getActivit, getBroadcas, and getService. It can be displayed and executed after clicking.

2. Preparation work

(1) Mobile phone settings (according to your personal mobile phone model, go online to find the developer mode settings)

1.Connect

Mobile phone data cable to connect to computer

2. Change the settings (otherwise it cannot be installed on the phone)

System and updates~Developer options~Debugging~usb debugging (enabled)
has the following display:

3. Change the USB connection method as follows

(2) Computer settings

After the above settings are successfully set, click Locat below the item, pull down and select your mobile phone, and the connection will be successful.

3. Project construction

(1) Layout settings

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

(2) Project preparation

1. Authorization notification

Add the following permission grant in AndroidManifest.xml

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

2. Establish a connection

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

3. Get the services of the current system--notification service

val manager=getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

4. Create notifications

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. Establish monitoring to realize pop-up notifications after clicking

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) Complete code

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>

Activity.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>

Activity.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)//第一个参数是通知标签
        }

    }
}

NotificationActivity.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)
    }
}

Guess you like

Origin blog.csdn.net/m0_61059796/article/details/130754638