Android Studio实现番茄钟功能(20221009)

作者 by admin / 2022-10-09 / 暂无评论 / 2498 个足迹

题目
使用Android Studio写一个番茄钟,需要包含计时界面,番茄钟调整(工作时间、休息时间、工作组数),工作或休息时间结束时提醒用户并自动开始下一个番茄周期。
要求项目必须集成Github中的至少一个项目或框架。
Github的推荐安卓项目/框架:
AndroidProject(安卓技术中台):https://github.com/getActivity/AndroidProject
XXPermissions(权限请求框架):https://github.com/getActivity/XXPermissions
SmartRefreshLayout(智能下拉刷新框架):https://github.com/scwang90/SmartRefreshLayout
XPopup(弹窗框架):https://github.com/li-xiaojun/XPopup
EasyFloat(悬浮窗框架):https://github.com/princekin-f/EasyFloat

中等:实现番茄钟计时界面、番茄钟调整、时间到了提醒用户
所需知识:
Java:基础知识
Android:子线程(后台计时)、动态更新布局(页面计时时间)、权限配置(震动或响铃提醒用户)

较难:在中等的前提下,实现用户数据记忆(时间及周期),屏幕常亮(用户切换)
中等前提下还需要你掌握:
SharedPreferences、getWindow().addFlags()


题目不难,总体来说抽成几个部分:计时界面、计时功能、提醒功能。
计时界面和功能方面,我使用了Github上的CircleTimerView项目,提取其中的界面代码,并且计时功能已在其项目中集成,只是需要自己略加修改。
提醒功能方面,我写了一个MyNotification类,在其中写好通知方法和响铃/震动。
通知函数代码如下:

/**
     * 专用于TimerActivity的通知函数
     * 发送一个无声通知
     * 支持通知点击跳转
     * @param context
     * @param Title
     * @param ContentText
     * @param SubText
     * @param Ticker
     */
    public static void SendNotification(Context context,String Title,String ContentText,String SubText,String Ticker){
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Intent intent=new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setComponent(new ComponentName(context, TimerActivity.class));//用ComponentName得到class对象
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);// 关键的一步,设置启动模式,两种情况
        PendingIntent pendingIntent;
        //PendingIntent 是用于构建通知跳转路径的
        // Android12以后,构建时必须指定标识 PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_IMMUTABLE,否则通知将构建失败抛出异常
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_MUTABLE);
        }else {
            pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        }
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // 使用NotificationChannel 类构建一个通知渠道
            NotificationChannel notificationChannel = new NotificationChannel("2", Title,
                    NotificationManager.IMPORTANCE_LOW);
            notificationChannel.setSound(null,null);//设置静音
            //通知的一些行为样式配置。
            notificationChannel.enableLights(true); //是否在launcher icon右上角展示提示点
            notificationChannel.setLightColor(Color.RED); //提示点颜色

            notificationManager.createNotificationChannel(notificationChannel);

            Notification notification = new Notification.Builder(context,"2")
                    .setContentTitle(Title)//标题
                    .setContentText(ContentText)//内容
                    .setSubText(SubText)//内容下面的一小段文字
                    .setTicker(Ticker)//收到信息后状态栏显示的文字信息
                    .setContentIntent(pendingIntent)//跳转页面
                    .setWhen(System.currentTimeMillis())//设置通知时间
                    .setSmallIcon(R.mipmap.ic_launcher_round)//设置小图标
                    .build();
            notificationManager.notify(1, notification);
        }else {
            Notification notification = new Notification.Builder(context)
                    .setContentTitle(Title)//标题
                    .setContentText(ContentText)//内容
                    .setSubText(SubText)//内容下面的一小段文字
                    .setTicker(Ticker)//收到信息后状态栏显示的文字信息
                    .setSound(null)//设置静音
                    .setContentIntent(pendingIntent)//跳转页面
                    .setWhen(System.currentTimeMillis())//设置通知时间
                    .setSmallIcon(R.mipmap.ic_launcher_round)//设置小图标
                    .build();
            notificationManager.notify(1, notification);
        }
    }

注意:Android12以后,构建时必须指定标识 PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_IMMUTABLE,否则通知将构建失败抛出异常。
响铃和震动直接调用即可,不在此特别说明,只是不要忘记向manifest中加入权限。
用户数据记忆用SharedPreferences即可,例:

        //初始化SharedPreferences储存
        sharedPreferences= getApplicationContext().getSharedPreferences("Pomodoro", Context.MODE_PRIVATE);
        //将SharedPreferences储存可编辑化
        editor=sharedPreferences.edit();
        sharedPreferences.getInt("workTime", 25)
        ......

屏幕常亮:

            //在Window增加flag打开屏幕常亮:
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            ScreenOnBtu.setBackgroundResource(R.drawable.ic_keep_screen_on);
            //在Window去除flag关闭屏幕常亮:
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            ScreenOnBtu.setBackgroundResource(R.drawable.ic_keep_screen_off);

我的工程文件如下:
FocusOn.zip

评论已关闭