AndroidStudio-每日通知导致应用程序崩溃
创始人
2024-10-10 20:30:38
0次
  1. 首先,在AndroidManifest.xml文件中添加以下权限:

    
    
  2. 在MainActivity.java中添加以下代码:

    //设置每日通知
    private void setDailyNotification(){
    
        //获取AlarmManager系统服务
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    
        Intent notificationIntent = new Intent(getApplicationContext(), MyNotificationPublisher.class);
    
        //设置通知时间为每天早上8点
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 8);
        calendar.set(Calendar.MINUTE, 0);
    
        //将通知Intent放入PendingIntent中
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
        //设置重复闹钟
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
    
    }
    
  3. 在MyNotificationPublisher.java中添加以下代码:

    public class MyNotificationPublisher extends BroadcastReceiver {
    
        private static final int NOTIFICATION_ID = 1;
        private static final String CHANNEL_ID = "notification_channel";
    
        @Override
        public void onReceive(Context context, Intent intent) {
    
            //创建通知渠道(Android 8.0及以上版本需要创建通知渠道)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Daily Notification", NotificationManager.IMPORTANCE_HIGH);
                channel.setDescription("Daily Notification");
                NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
                notificationManager.createNotificationChannel(channel);
            }
    
            //创建通知
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
                    .setSmallIcon(R.drawable.notification_icon)
                    .setContentTitle("Daily Notification")
                    .setContentText("Today is a new day!")
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setAutoCancel(true);
    
            //创建PendingIntent
            Intent notificationIntent = new Intent(context, MainActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 

相关内容

热门资讯

重大通报透视器!娱家麻将透视器... 重大通报透视器!娱家麻将透视器AI脚本,娱家麻将多功能切实真的是有透视器脚本1、娱家麻将辅助软件下载...
重大消息开挂!家乡大贰开挂AI... 重大消息开挂!家乡大贰开挂AI插件,家乡大贰多功能果然有开挂插件家乡大贰脚本下载中分为三种模型:家乡...
玩家必看科普外挂!河洛杠次外挂... 玩家必看科普外挂!河洛杠次外挂AI工具,河洛杠次多功能一直是真的有外挂工具亲,关键说明,河洛杠次透视...
重大来袭开挂!广西老友玩开挂A... 重大来袭开挂!广西老友玩开挂AIapp,广西老友玩多功能竟然有开挂app1、每一步都需要思考,不同水...
科技揭秘透视挂!嘟咪互动麻将透... 科技揭秘透视挂!嘟咪互动麻将透视挂AI插件,嘟咪互动麻将多功能竟然是有透视挂插件1、玩家可以在嘟咪互...
盘点几款辅助器!微乐河北麻将辅... 盘点几款辅助器!微乐河北麻将辅助器AI工具,微乐河北麻将多功能竟然存在有辅助器工具盘点几款辅助器!微...
盘点一款透视挂!星际互联透视挂... 盘点一款透视挂!星际互联透视挂AI方法,星际互联多功能竟然是有透视挂方法1、星际互联破解器简单,星际...
推荐十款开挂!MG棋牌开挂AI... 推荐十款开挂!MG棋牌开挂AI教程,MG棋牌多功能一直是真的有开挂教程1、每一步都需要思考,不同水平...
记者揭秘辅助器!大圣天辅助器A... 记者揭秘辅助器!大圣天辅助器AI教程,大圣天多功能切实真的有辅助器教程1)大圣天免费钻石:进一步探索...
技术分享作弊器!赣牌圈打炸作弊... 技术分享作弊器!赣牌圈打炸作弊器AI插件,赣牌圈打炸多功能确实是有作弊器插件1、起透看视 赣牌圈打炸...