Android多个闹钟,带有不同的通知
创始人
2024-10-07 15:34:13
0

要实现Android上的多个闹钟,并且每个闹钟都有不同的通知,可以使用AlarmManager和NotificationManager来实现。

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



接下来,创建一个AlarmReceiver类,用于接收闹钟触发的广播,并在其中处理通知逻辑:

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // 处理通知逻辑
        String message = intent.getStringExtra("message");
        showNotification(context, message);
    }

    private void showNotification(Context context, String message) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "channel_id")
                .setSmallIcon(R.drawable.notification_icon)
                .setContentTitle("闹钟通知")
                .setContentText(message)
                .setPriority(NotificationCompat.PRIORITY_HIGH);
        
        // 显示通知
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        notificationManager.notify(0, builder.build());
    }
}

然后,在Activity中设置闹钟和通知:

public class MainActivity extends AppCompatActivity {
    private AlarmManager alarmManager;
    private PendingIntent alarmIntent;

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

        alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, AlarmReceiver.class);
        intent.putExtra("message", "闹钟1");
        alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

        // 设置第一个闹钟
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTimeInMillis(System.currentTimeMillis());
        calendar1.set(Calendar.HOUR_OF_DAY, 8);
        calendar1.set(Calendar.MINUTE, 0);
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), alarmIntent);

        // 设置第二个闹钟
        Intent intent2 = new Intent(this, AlarmReceiver.class);
        intent2.putExtra("message", "闹钟2");
        PendingIntent alarmIntent2 = PendingIntent.getBroadcast(this, 1, intent2, 0);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTimeInMillis(System.currentTimeMillis());
        calendar2.set(Calendar.HOUR_OF_DAY, 9);
        calendar2.set(Calendar.MINUTE, 0);
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar2.getTimeInMillis(), alarmIntent2);
    }
}

在上面的代码中,我们创建了两个闹钟,分别在早上8点和9点触发。每个闹钟都会发送一个包含通知内容的广播。在AlarmReceiver中,我们通过获取广播中的消息内容,使用NotificationCompat.Builder创建通知,并使用NotificationManagerCompat显示通知。

需要注意的是,为了确保闹钟能够准确触发,我们使用了AlarmManager的setExact方法,并设置了不同的requestCode来区分不同的闹钟。此外,我们还需要在AndroidManifest.xml文件中注册AlarmReceiver:


以上就是实现Android多个闹钟并带有不同通知的示例代码。

相关内容

热门资讯

据统计!pokemomo辅助软... 据统计!pokemomo辅助软件,八张透视辅助,演示教程(有挂细节)1、全新机制【八张透视辅助ai辅...
明白辅助挂!红龙poker作弊... 明白辅助挂!红龙poker作弊指令,奇迹脚本辅助,大纲教程(有挂方针)1、游戏颠覆性的策略玩法,独创...
目前!德州圈脚本,德普之星辅助... 目前!德州圈脚本,德普之星辅助器,积累教程(真的有挂)1、操作简单,无需德普之星辅助器手机版透视脚本...
相较于以往!智星菠萝有挂吗,来... 相较于以往!智星菠萝有挂吗,来来拼十辅助免费辅助,方针教程(存在有挂)1、首先打开来来拼十辅助免费辅...
据通报!德扑之心免费透视,广东... 据通报!德扑之心免费透视,广东雀神挂机怎么样,步骤教程(有挂方略)1、该软件可以轻松地帮助玩家将广东...
有玩家发现!aapoker真的... 有玩家发现!aapoker真的假的,闲聚辅助器,绝活儿教程(讲解有挂)1)闲聚辅助器免费钻石:进一步...
方法辅助挂!德州局脚本,博雅红... 方法辅助挂!德州局脚本,博雅红河西元红河挂,方式教程(有挂讲解)1、博雅红河西元红河挂免费辅助多个强...
有玩家发现!扑克之星辅助,jj... 有玩家发现!扑克之星辅助,jj斗地主外卦,讲义教程(有挂分析)1、进入到jj斗地主外卦是否有挂之后,...
黑科技辅助挂!hhpoker辅... 黑科技辅助挂!hhpoker辅助,陕麻圈辅助开挂软件,妙招教程(有挂详情);1、下载好陕麻圈辅助开挂...
为切实保障!epoker透视底... 为切实保障!epoker透视底牌,哈局八张辅助,总结教程(有挂方略)1、该软件可以轻松地帮助玩家将哈...