Android前台服务同一服务的多个通知
创始人
2024-10-09 15:02:07
0

要实现Android前台服务显示多个通知,可以通过创建多个通知通道来实现。每个通知通道都可以显示一个独立的通知。

以下是一个示例代码,演示如何在一个前台服务中显示多个通知:

  1. 首先,在AndroidManifest.xml文件中添加以下权限:

  1. 创建一个前台服务类,例如MyForegroundService.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 android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;

public class MyForegroundService extends Service {

    private static final String CHANNEL_ID_1 = "channel_1";
    private static final String CHANNEL_ID_2 = "channel_2";

    private NotificationManager notificationManager;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        createNotificationChannels();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 在这里根据需要显示多个通知
        showNotification1();
        showNotification2();
        // 返回START_STICKY,确保服务在被杀死后能够自动重启
        return START_STICKY;
    }

    private void createNotificationChannels() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // 创建第一个通知通道
            NotificationChannel channel1 = new NotificationChannel(
                    CHANNEL_ID_1,
                    "Channel 1",
                    NotificationManager.IMPORTANCE_DEFAULT
            );
            channel1.setDescription("This is Channel 1");
            notificationManager.createNotificationChannel(channel1);

            // 创建第二个通知通道
            NotificationChannel channel2 = new NotificationChannel(
                    CHANNEL_ID_2,
                    "Channel 2",
                    NotificationManager.IMPORTANCE_DEFAULT
            );
            channel2.setDescription("This is Channel 2");
            notificationManager.createNotificationChannel(channel2);
        }
    }

    private void showNotification1() {
        // 创建第一个通知
        Notification notification1 = new NotificationCompat.Builder(this, CHANNEL_ID_1)
                .setContentTitle("Notification 1")
                .setContentText("This is Notification 1")
                .setSmallIcon(R.drawable.ic_notification)
                .build();

        // 启动前台服务并显示第一个通知
        startForeground(1, notification1);
    }

    private void showNotification2() {
        // 创建第二个通知
        Notification notification2 = new NotificationCompat.Builder(this, CHANNEL_ID_2)
                .setContentTitle("Notification 2")
                .setContentText("This is Notification 2")
                .setSmallIcon(R.drawable.ic_notification)
                .build();

        // 显示第二个通知
        notificationManager.notify(2, notification2);
    }
}
  1. 在MainActivity.java中启动前台服务:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 启动前台服务
        startService(new Intent(this, MyForegroundService.class));
    }
}

请注意,以上示例中创建了两个通知通道(CHANNEL_ID_1和CHANNEL_ID_2),并在MyForegroundService中的onStartCommand方法中显示了两个通知(showNotification1和showNotification2)。可以根据需要添加更多的通知通道和通知。

希望这个示例能帮助到你!

相关内容

热门资讯

黑科技好友(德州)外挂辅助插件... 黑科技好友(德州)外挂辅助插件(透视)新版2025教程(好像真的是有挂)进入游戏-大厅左侧-新手福利...
黑科技透明挂!(WpK)透视辅... 黑科技透明挂!(WpK)透视辅助app,(wpK)原先是真的有挂,安装教程(有挂软件)1、不需要AI...
黑科技教学(WePoKer)黑... 黑科技教学(WePoKer)黑科技透明挂辅助器(透视)解密教程(好像有挂)1、全新机制【WePoKe...
黑科技玄学!(wepoker)... 黑科技玄学!(wepoker)软件透明挂,(wepoker)最初有挂,靠谱教程(有挂方法);小薇(透...
黑科技存在!(WPK)透视辅助... 黑科技存在!(WPK)透视辅助软件,(WPk)本然真的有挂,解密教程(有挂技巧);科技安装教程;13...
黑科技规律(微扑克大厅)外挂透... 黑科技规律(微扑克大厅)外挂透视辅助app(透视)科技教程(都是存在有挂)1、在微扑克大厅ai机器人...
黑科技脚本!(aaPOKER)... 黑科技脚本!(aaPOKER)透明挂辅助插件,(德州aapoker俱乐部)起初是有挂,扑克教程(有挂...
黑科技智能ai(We辅poke... 黑科技智能ai(We辅poker助)黑科技透明挂辅助下载(透视)AI教程(竟然是真的有挂);暗藏猫腻...
黑科技线上!(Wpk)透视辅助... 1、黑科技线上!(Wpk)透视辅助机制,(wpK)一向存在有挂,攻略方法(有挂神器)2、进入游戏-大...
黑科技智能ai(微扑克软件)外... 黑科技智能ai(微扑克软件)外挂透视辅助器(透视)wpk教程(确实有挂)1、超多福利:超高返利,海量...