Android 通知栏开发指南:完整步骤及代码示例
"使用 Android 编写一个通知栏", "可以按照以下步骤进行:", "1. 在 AndroidManifest.xml 文件中添加权限:", "\", \"<uses-permission android:name=\"android.permission.FOREGROUND_SERVICE\" />\", \"", "2. 创建一个 Service 类来处理通知栏的逻辑,例如 NotificationService.java:", "java\", \"import android.app.Notification;\", \"import android.app.NotificationChannel;\", \"import android.app.NotificationManager;\", \"import android.app.PendingIntent;\", \"import android.app.Service;\", \"import android.content.Intent;\", \"import android.os.Build;\", \"import android.os.IBinder;\", \"\", \"import androidx.core.app.NotificationCompat;\", \"\", \"public class NotificationService extends Service {\", \"\", \" private static final String CHANNEL_ID = \"NotificationChannel\";\", \" private static final int NOTIFICATION_ID = 1;\", \"\", \" @Override\", \" public void onCreate() {\", \" super.onCreate();\", \"\", \" createNotificationChannel();\", \"\", \" Intent notificationIntent = new Intent(this, MainActivity.class);\", \" PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);\", \"\", \" Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)\", \" .setContentTitle(\"通知栏标题\")\", \" .setContentText(\"通知栏内容\")\", \" .setSmallIcon(R.drawable.notification_icon)\", \" .setContentIntent(pendingIntent)\", \" .build();\", \"\", \" startForeground(NOTIFICATION_ID, notification);\", \" }\", \"\", \" @Override\", \" public IBinder onBind(Intent intent) {\", \" return null;\", \" }\", \"\", \" private void createNotificationChannel() {\", \" if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {\", \" CharSequence name = getString(R.string.channel_name);\", \" String description = getString(R.string.channel_description);\", \" int importance = NotificationManager.IMPORTANCE_DEFAULT;\", \" NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);\", \" channel.setDescription(description);\", \" NotificationManager notificationManager = getSystemService(NotificationManager.class);\", \" notificationManager.createNotificationChannel(channel);\", \" }\", \" }\", \"}\", \"", "3. 在 MainActivity.java 中启动 Service:", "java\", \"import android.content.Intent;\", \"import android.os.Bundle;\", \"import android.view.View;\", \"\", \"import androidx.appcompat.app.AppCompatActivity;\", \"\", \"public class MainActivity extends AppCompatActivity {\", \"\", \" @Override\", \" protected void onCreate(Bundle savedInstanceState) {\", \" super.onCreate(savedInstanceState);\", \" setContentView(R.layout.activity_main);\", \" }\", \"\", \" public void startService(View view) {\", \" startService(new Intent(this, NotificationService.class));\", \" }\", \"\", \" public void stopService(View view) {\", \" stopService(new Intent(this, NotificationService.class));\", \" }\", \"}\", \"", "4. 在布局文件 activity_main.xml 中添加两个按钮,用于启动和停止 Service:", "xml\", \"<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\", \" xmlns:tools=\"http://schemas.android.com/tools\"\", \" android:layout_width=\"match_parent\"\", \" android:layout_height=\"match_parent\"\", \" android:gravity=\"center\"\", \" android:orientation=\"vertical\"\", \" tools:context=\".MainActivity\">\", \"\", \" <Button\", \" android:layout_width=\"wrap_content\"\", \" android:layout_height=\"wrap_content\"\", \" android:onClick=\"startService\"\", \" android:text=\"启动 Service\" />\", \"\", \" <Button\", \" android:layout_width=\"wrap_content\"\", \" android:layout_height=\"wrap_content\"\", \" android:onClick=\"stopService\"\", \" android:text=\"停止 Service\" />\", \"\", \"</LinearLayout>\", \"", "5. 在 res 目录下创建一个 drawable 目录,并添加一个 notification_icon.png 文件,作为通知栏的图标。", "", "完成以上步骤后,运行应用程序,点击"启动 Service"按钮即可在通知栏中看到相应的通知。点击通知可以跳转到 MainActivity。点击"停止 Service"按钮可以停止通知栏的显示。".
原文地址: https://www.cveoy.top/t/topic/psOT 著作权归作者所有。请勿转载和采集!