android 消息提醒

本文介绍了如何在Android中创建和管理两种服务,MyBackgroundService用于定期执行任务并显示通知,DaemonService作为守护进程在应用关闭后保持运行。文章详细描述了服务的生命周期方法、通知渠道的创建以及在AndroidManifest.xml中的注册。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.创建 MyBackgroundService.java  继承 Service

public class MyBackgroundService extends Service {
   
    @Override
    public void onCreate() {
        super.onCreate();
        Log.i("业务服务", "开起业务服务");
        //调用服务后在页面手机上创建一个通知消息。
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationChannel channel = null;
            channel = new NotificationChannel("100", getString(R.string.app_name), NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(channel);
            Notification notification = new Notification.Builder(getApplicationContext(), "100")
                    .setVisibility(Notification.VISIBILITY_PRIVATE).build();
            startForeground(100, notification);
        }

    }
    @Override
    public int onStartCommand(Intent intent,int flsgs,int startId){
        timer.schedule(task, 60000, 60000);    //定时执行代码
        return  START_STICKY;
    }



    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
//构造消息(在需要发送消息的方法中调用方法)
private void sendMessgae(String my_channel_ID,String my_channel_NAME,String msgContext,int notifyid,String msgType){
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0);
    String channelId = createNotificationChannel(my_channel_ID, my_channel_NAME, NotificationManager.IMPORTANCE_HIGH);
    NotificationCompat.Builder notification = new NotificationCompat.Builder(getApplicationContext(), channelId)
            .setContentTitle("管理系统")
            .setContentText(msgContext)
            .setContentIntent(pendingIntent)
            .setSmallIcon(R.drawable.newlogo)
            .setAutoCancel(true);
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getApplicationContext());
    if(notification !=null){
        notificationManager.notify(notifyid,notification.build());
        savemsg(my_channel_ID+"",msgContext,msgType);
    }
}

//消息设置
private String createNotificationChannel(String channelID, String channelNAME, int level) {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationManager manager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel channel = new NotificationChannel(channelID, channelNAME, level);
        //开启震动
        channel.enableVibration(true);
        //设置锁屏提示
        channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
        manager.createNotificationChannel(channel);
        return channelID;
    } else {
        return null;
    }
}

2.创建 DaemonService.java 守护服务

public class DaemonService extends Service {
        private final static String TAG = DaemonService.class.getSimpleName();

        @Override
        public void onCreate() {
            super.onCreate();
            Log.i("测试开启服务", "开始守护服务");
        }

        @Override
        public int onStartCommand(Intent intent,int flags,int startId){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                startForegroundService(new Intent(this, MyBackgroundService.class));
            } else {
                startService(new Intent(this, MyBackgroundService.class));
            }
            return START_STICKY;
        }
}

 3.在 AndroidManifest.xml 中注册服务

<service android:name=".tools.DaemonService"
    android:process=":daemon" />

<service
    android:name=".tools.MyBackgroundService"
    android:enabled="true"
    android:exported="false"
    android:stopWithTask="false" />

手动关闭应用程序后后台进程一直存在。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值