📜  如何在 Android 中实现通知计数器?

📅  最后修改于: 2022-05-13 01:54:44.661000             🧑  作者: Mango

如何在 Android 中实现通知计数器?

通知计数器基本上会计算您通过应用程序收到的通知,并显示在应用程序图标的顶部,以便您无需打开应用程序或特定功能(例如 Instagram 中的消息按钮)即可了解收到新消息或任何新更新。通知计数器是当今几乎所有 Android 应用程序中都提供的一项功能,例如 Facebook、Whatsapp、Instagram、YouTube、Gmail 这些是我们在日常生活中使用的一些社交应用程序,因此通知计数器功能究竟是做什么的?

它用于各种目的,例如:

  1. 告诉用户您收到一条新消息
  2. 告诉他们有多少未读消息 
  3. 要通知用户上传了新功能或内容,例如每当您订阅的频道在 Youtube 上传视频时,您的 Youtube 通知计数就会增加。

我们将在本文中构建什么?

在本文中,我们将构建一个简单的应用程序,该应用程序借助两个按钮递增和递减来计算通知数量。每当用户点击递增按钮时,通知计数就会增加,每当用户点击递减按钮时,通知计数会随着这两个按钮而减少,我们还将实现或使用第三个按钮,它是一个重置按钮,直接设置通知计数为零。下面给出了一个示例 GIF,以了解我们将在本文中做什么。请注意,我们将使用Java语言来实现这个项目。

分步实施

第 1 步:创建一个新项目

要在 Android Studio 中创建新项目,请参阅如何在 Android Studio 中创建/启动新项目。请注意,选择Java作为编程语言。



第 2 步:按照以下步骤为通知铃创建新的 Vector 资产。右键单击可绘制 > 新建 > 矢量资产 > 搜索通知图标并选择 > 完成。我们将使用此图标在其上显示我们的通知计数。现在,我们在这一步中使用的矢量资产到底是什么。

Vector Asset: Vector Asset 是一款 Android Studio 工具,允许您添加材质图标并将可缩放矢量图形 (SVG) 和 Adobe Photoshop 文档 (PSD) 文件作为矢量可绘制资源导入到您的项目中。

图片供参考:

图 = 矢量资产

XML

    android:height="24dp" 
    android:viewportWidth="24"  
    android:viewportHeight="24" 
    android:tint="?attr/colorControlNormal">
  


XML


 
    


Java
import android.util.Log;
import android.view.View;
import android.widget.TextView;
 
public class notificationcount { 
     
    private TextView notificationNumber;
 
    // maximum count limit
    private final int MAX_NUMBER = 99;
 
    // minimum count limit
    private final int MIN_NUMBER = 0;
 
    // initial count
    private int notification_number_counter = 0;
 
    public notificationcount(View view) {
        // finding textview through id textview
        // in notification number variable
        notificationNumber = view.findViewById(R.id.textView);
    }
 
    // increment method
    public void increment() {
 
        // checking condition if notification_counter-number
        // is greater than max number or not
        if (notification_number_counter > MAX_NUMBER) {
            // printing message maximum number reached
            Log.d("Counter", "Maximum number reached");
        } else {
            // if condition fails then increment the count by 1
            notification_number_counter++;
            // returning increased value
            notificationNumber.setText(String.valueOf(notification_number_counter));
        }
    }
 
    // decrement method
    public void decrement() {
 
        // checking condition if notification_number_count
        // is less than min number or not
        if (notification_number_counter <= MIN_NUMBER) {
            // if true then message minimum number reached
            Log.d("Counter", "Minimum number reached");
        } else {
            // decrease if condition fails
            notification_number_counter--;
            // returning decrease count
            notificationNumber.setText(String.valueOf(notification_number_counter));
        }
    }
 
    // rest method
    public void reset() {
        // checking if notification_number_count
        // is already zero or not
        if (notification_number_counter == 0) {
            // if true message already zero
            Log.d("alert", " notification count is already 0 ");
        } else {
            // else setting count to zero
            notification_number_counter = 0;
            // returning updated value
            notificationNumber.setText(String.valueOf(notification_number_counter));
        }
    }
 
}


Java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
import androidx.appcompat.app.AppCompatActivity;
 
public class MainActivity extends AppCompatActivity {
   
    // initializing 3 button variables b1,b2,b3
    Button b1, b2, b3;
    // initializing textview variable number
    TextView number;
    // object of Notificationcount class
    notificationcount notificationcount; 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // finding increment button through id in b1
        b1 = findViewById(R.id.increment);
        // finding decrement button through id in b2
        b2 = findViewById(R.id.decrement);
        // finding reset button through id in b3
        b3 = findViewById(R.id.reset);
 
        // creating new object of notificationcount class
        notificationcount = new notificationcount(findViewById(R.id.textView));
 
        // increment button
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // calling increment method
                notificationcount.increment();
            }
        });
 
        // decrement button
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // calling decrement button
                notificationcount.decrement();
            }
        });
 
        // reset button
        b3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // calling reset button
                notificationcount.reset();
            }
        });
 
    }
}


步骤 3:使用 activity_main.xml 文件

在这一步中,我们将在第 2 步中创建的矢量资产的帮助下,在activity_main.xml文件中设计三个名为 Increment 、decrement 和 reset 的按钮以及我们的通知铃声,并显示我们将要显示的通知计数使用 textview 并将该 textview 设置在我们使用图像视图实现的钟形图标的顶部,我们还使用 ImageView 来显示 gfg 徽标的图像。

导航到app > res > layout > activity_main.xml并将以下代码添加到该文件中。下面是activity_main.xml文件的代码



XML



 
    

第四步:

在这一步中,我们将实现我们的方法来执行诸如递增、递减、重置等操作。为此,请按照以下步骤创建一个新的Java类。右键单击项目名称 > 新建 > 类 > 完成。以下是第N otificationcount的代码。 Java文件。

Java

import android.util.Log;
import android.view.View;
import android.widget.TextView;
 
public class notificationcount { 
     
    private TextView notificationNumber;
 
    // maximum count limit
    private final int MAX_NUMBER = 99;
 
    // minimum count limit
    private final int MIN_NUMBER = 0;
 
    // initial count
    private int notification_number_counter = 0;
 
    public notificationcount(View view) {
        // finding textview through id textview
        // in notification number variable
        notificationNumber = view.findViewById(R.id.textView);
    }
 
    // increment method
    public void increment() {
 
        // checking condition if notification_counter-number
        // is greater than max number or not
        if (notification_number_counter > MAX_NUMBER) {
            // printing message maximum number reached
            Log.d("Counter", "Maximum number reached");
        } else {
            // if condition fails then increment the count by 1
            notification_number_counter++;
            // returning increased value
            notificationNumber.setText(String.valueOf(notification_number_counter));
        }
    }
 
    // decrement method
    public void decrement() {
 
        // checking condition if notification_number_count
        // is less than min number or not
        if (notification_number_counter <= MIN_NUMBER) {
            // if true then message minimum number reached
            Log.d("Counter", "Minimum number reached");
        } else {
            // decrease if condition fails
            notification_number_counter--;
            // returning decrease count
            notificationNumber.setText(String.valueOf(notification_number_counter));
        }
    }
 
    // rest method
    public void reset() {
        // checking if notification_number_count
        // is already zero or not
        if (notification_number_counter == 0) {
            // if true message already zero
            Log.d("alert", " notification count is already 0 ");
        } else {
            // else setting count to zero
            notification_number_counter = 0;
            // returning updated value
            notificationNumber.setText(String.valueOf(notification_number_counter));
        }
    }
 
}



第 5 步: 主要活动。 Java文件

在这最后一步中,我们将在MainActivity 中为三个名为 increment、decrement 和 reset 的按钮实现 onclick 侦听器我们在上一步中创建的Java文件和调用函数递增、递减、重置。下面MainActivity的代码。 Java文件。代码中添加了注释以更详细地理解代码。

Java

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
import androidx.appcompat.app.AppCompatActivity;
 
public class MainActivity extends AppCompatActivity {
   
    // initializing 3 button variables b1,b2,b3
    Button b1, b2, b3;
    // initializing textview variable number
    TextView number;
    // object of Notificationcount class
    notificationcount notificationcount; 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // finding increment button through id in b1
        b1 = findViewById(R.id.increment);
        // finding decrement button through id in b2
        b2 = findViewById(R.id.decrement);
        // finding reset button through id in b3
        b3 = findViewById(R.id.reset);
 
        // creating new object of notificationcount class
        notificationcount = new notificationcount(findViewById(R.id.textView));
 
        // increment button
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // calling increment method
                notificationcount.increment();
            }
        });
 
        // decrement button
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // calling decrement button
                notificationcount.decrement();
            }
        });
 
        // reset button
        b3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // calling reset button
                notificationcount.reset();
            }
        });
 
    }
}

输出:

图 = 通知计数器

输出视频:

项目链接:点我