编辑预定的待处理意向

2023-12-15

我编写了一个应用程序,可以在之前选择的预定时间打开/关闭 WiFi。 它的工作方式非常简单:从时间选择器中选择时间,然后添加它。它以编程方式从时间选择器获取数据并设置和警报。 我首先写下我的活动和广播接收器的代码,在这段代码下面我将写下我的问题。别担心,我在代码中添加了注释,以便更清晰地阅读和理解

Wifi.类:

public class WiFi extends AppCompatActivity {
private final int TEMP_REQUEST_CODE = 1;
private AlarmManager alarmManager;
private TimePicker timePicker;
private PendingIntent pendingIntent;
private Intent intent;
private Context context;
private Calendar calendar;
Intent alarmIntent;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.wifi_class);
    timePicker = (TimePicker) findViewById(R.id.timePicker1);
    timePicker.setIs24HourView(true);
    setTitle("Set WiFi off/on");
    //store context in global variable
    context = getApplicationContext();
    //creates an intent that will be used in pending intent
    initializeView();
    //creates pending intent
    createAlarmIntent();
    //checks if alarm exists and sets matching text on the screen
    checkAlarmExists();

}

// I got an add button in my toolbar that saves the alarm, same as to delete 
an alarm
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.tick:
            Alarm();
            finish();
            return true;
        case R.id.dismiss:
            cancelAlarm();
        default:
            return super.onOptionsItemSelected(item);
    }
}
//this method initialize intent and stores it into variable, 
WiFiService.class extends BroadcastReceiver
//and all what it has to do is to turn wifi on/off
private void initializeView () {
    intent = new Intent(context, WifiService.class);

    //creating global pendingIntent variable
     pendingIntent = PendingIntent.getBroadcast(context,
            0,
            intent,
            0);
    //creating global alarmManager variable
     alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
}
//this method gets selected time from timepicker and calls another method to 
create an alarm
private void Alarm() {
     calendar = Calendar.getInstance();
    if (Build.VERSION.SDK_INT >= 23) {
        calendar.set(
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH),
                timePicker.getHour(),
                timePicker.getMinute(), 0
        );

    } else {
        calendar.set(
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH),
                timePicker.getCurrentHour(),
                timePicker.getCurrentMinute(),
                0);
    }
    setAlarm();
}

/**
 * This method sets Alarm that will be executed even in Doze mode and will 
intent WifiServices class,
 * in the result it will turn wifi off or on
 *
 */
private PendingIntent createAlarmIntent() {
     alarmIntent = new Intent(this, WifiService.class);
    return PendingIntent.getBroadcast(this, TEMP_REQUEST_CODE,
            alarmIntent, 0);
}
private void setAlarm() {
//setting alarm
    PendingIntent intent = createAlarmIntent();
    if (Build.VERSION.SDK_INT >= 23) {
        alarmManager.setAlarmClock(new 
AlarmManager.AlarmClockInfo(calendar.getTimeInMillis(), pendingIntent), 
intent);
}
    else {
        alarmManager.setExact(AlarmManager.RTC, calendar.getTimeInMillis(), 
pendingIntent);
    }

    long AlarmMgs = calendar.getTimeInMillis() - 
Calendar.getInstance().getTimeInMillis();
    Toast.makeText(context, "Alarm will be executed in " + AlarmMgs / 1000 / 
60 + "min", Toast.LENGTH_SHORT).show();

}


/**
 * This method should cancel alarm that is being currently set. There is no 
 if/else statement 
 * because it always says that alarm exists (see next method)
 */
private void cancelAlarm() {

    alarmManager.cancel(createAlarmIntent());
    Toast.makeText(context, "Alarm dismissed", Toast.LENGTH_SHORT).show();
    checkAlarmExists();
}

/**
 * This method checks wrether alarm is set or not and assigns that into 
 TextView
 * Unfortunately it always says it exists
 */
private void checkAlarmExists() {

    boolean alarmExists =
            (PendingIntent.getBroadcast(context,
                    0,
                    intent,
                    PendingIntent.FLAG_NO_CREATE)
                    != null);

    TextView alarmSetter = (TextView) findViewById(R.id.alarmSet);
    if (alarmExists) {
        alarmSetter.setText("Alarm is set");

    } else {
        alarmSetter.setText("Alarm is not set yet");
    }
}
    //TODO: (1): Create more than 1 alarm without replacing one before
    //TODO: (2): Cancel one of them, not all of them
    //TODO: (3): Edit those alarms 
}

Wifi服务类:

public class WifiService extends BroadcastReceiver {

public static final String TAG = "WiFi";

@Override
public void onReceive(Context context, Intent intent) {
//        if 
(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) 
{

        WifiManager wifiManager = (WifiManager) 
context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        Log.v("Wifi", "checked");
        if (wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(false);
            Toast.makeText(context, "Turning wifi off", 
Toast.LENGTH_SHORT).show();
            Log.v(TAG, "Turned off");
        } else {
            wifiManager.setWifiEnabled(true);
            Toast.makeText(context, "turning wifi on", 
Toast.LENGTH_SHORT).show();
            Log.v(TAG, "turned off");
        }
//        }
    Log.v(TAG, "Works");
}

}

好的,我在代码中留下了 3 个 TODO: 1. 我想创建 1 个或多个警报而不替换现有的警报,我所要做的就是设置待处理标志以在每次设置警报时更新? 2. 如果我创建了例如 3 个警报,我想取消其中一个,指定一个。无论如何我都会将这些警报存储在数据库中,所以我的想法是: 创建警报 --> 将其添加到数据库 --> 在列表中显示 --> 如果删除 --> 从列表中删除 --> 停用 3. 编辑警报。我知道如何编辑数据库中的项目,但如何编辑预定的警报?我的意思是编辑它的着火时间。这一切都是为了重新创建警报吗?

有人可以回答我的问题吗?提前致谢。


当你设置闹钟时,你会通过一个PendingIntent to AlarmManager. The PendingIntent包裹一个Intent。当你设置闹钟时,AlarmManager删除已安排的与PendingIntent。以确定是否PendingIntents匹配,比较如下:

  • The requestCode in the PendingIntent.getBroadcast() call
  • 中的行动Intent
  • 中的类别Intent
  • 中的数据Intent
  • The Component(包名、类名)中Intent

注意:“额外”在Intent没有比较

所以,如果我们想重新安排闹钟,你只需要创建一个PendingIntent与相同的requestCode、行动和Component与上一次一样并再次设置闹钟。AlarmManager将删除前一项并设置新的。

如果您想并行安排多个警报,则需要确保requestCodes, the Components 或 ACTION 不同,否则会互相覆盖。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

编辑预定的待处理意向 的相关文章

随机推荐

  • 如何将二维数组从 C 传递到 Python

    我想将二维数组从 C 传递给 python 我怎样才能使用Py BuildValue and PyEval CallObject 例如 我可以使用以下代码将字符串从 C 传递到 python pModule PyImport ImportM
  • iPhone - 双击故障安全方法

    我试图检测视图上的双击 但是当双击到来时 第一次点击会触发 TouchesBegan 上的操作 因此 在检测双击之前 始终首先检测到单击 我怎样才能以只检测双击的方式做到这一点 我无法使用 OS 3 x 手势 因为我必须使其与旧操作系统版本
  • 如何清除javafx条形图中添加的文本?

    我在条形顶部添加一些文本 每个条形的值 它正在工作 但问题是我想在每次更新图表时删除此文本 事实上 更新数据后文本仍然保留 For the first chart since there is no previous data it s d
  • 如何使用 linq to sql 的文本框编辑/更新数据库中的记录?

    我使用 linq to sql 的 Visual Basic 2008 Express Edition 进行数据库操作 例如编辑记录 我没有使用任何 SQL Server 但我只是使用 Visual Basic 2008 Express 中
  • 如何获取数据表上某个类的行索引

    我想获取具有 selected 类的行的索引 我怎样才能做到这一点 这失败了 var datatable mytableid var selectedclass selected var table datatable dataTable
  • 以编程方式获取另一个进程的父进程pid?

    我尝试谷歌 但发现getppid 它获取的父pidcurrent过程 我需要类似的东西getppid some other pid 有这样的事吗 基本上获取某个进程的 pid 并返回父进程的 pid 我认为最简单的事情是打开 proc 并解
  • Bash - 从文件填充二维数组

    我知道这可能很容易 但我对此非常挣扎 问题描述 我有一个文本文件 其坐标格式为 1 2 3 7 其中第一列 x 和第二列 y 坐标 现在我想使用文件中的此坐标填充大小为 N x M 的二维数组 方法是为文件和 中指定的点打印 X 否则 示例
  • Python 3 不带括号打印

    The print以前在Python 2中是一条语句 现在在Python 3中变成了一个需要括号的函数 Python 3 中是否有抑制这些括号的方法 也许通过重新定义打印功能 所以 而不是 print Hello stack over fl
  • 在 .NET 中处理令人难以置信的大量数据

    我正在努力解决以下问题投影网但我一直遇到一些问题 第一个是在 a 中存储大量元素的问题List
  • 有没有一种简单的方法来删除字符串中的多个空格?

    假设这个字符串 The fox jumped over the log 转变为 The fox jumped over the log 在不拆分和进入列表的情况下实现此目的最简单的 1 2行 是什么 gt gt gt import re g
  • 如何定义函数的返回类型/OutputType

    为什么下面的类型会改变 function SomeFunction SomeParameter return SomeParameter 我想我需要设置一个返回类型 但是如何设置呢 一个例子是使用 NewFolder Join Path C
  • XSLT 转换从元素创建新的 qname

    我需要转换 xml 输出中的数据表 如下所示 C1 列 1 c2 列 2 等
  • Anaconda Navigator 不显示新环境

    当我在 Anaconda Navigator 中添加新环境时 它将创建该环境 但不会在 UI 中显示 我可以通过提示看到环境 所以我知道它已经创建了 它只是不会在用户界面中显示它 我需要做什么才能让它显示在用户界面中 请通过运行更新 Ana
  • 如何在 ClearCase 中按文件扩展名删除文件元素?

    我有很多xxx cmd位于多个文件夹中的文件 例如child1 child2 child3等 他们有一个父文件夹parent 是否有一个cleartool命令可以在parent删除所有的文件夹 cmd所有子文件夹中的文件 不容易 因为在执行
  • 枚举当前 Visual Studio 项目中的所有文件

    我正在尝试编写一个简单的 Visual Studio 2012 扩展 我已经生成了扩展模板 并且可以从工具菜单中打开一个对话框 我想枚举当前打开的项目中的所有文件 然后根据一些规则过滤它们 我正在寻找的是返回 IEnumerable 的代码
  • 无法从我的 Flutter Web 访问 RestAPI (FastAPI) - 跨源请求被阻止

    我有一个 Linux 服务器 我有两个 Docker 容器 在第一个容器中 我部署 Flutter Web 在另一个容器中 我使用 FastAPI 运行 RestAPI 我将两个 Docker 容器设置在同一个网络中 因此通信应该可以正常工
  • 在 page_load 上的内容页上传递母版页控制值

    我在主页上有一个下拉列表 我想在加载内容页面时在内容页面上传递选定的值 我的问题是 只有当我更改下拉列表上的值时 该值才会传递 因此 当页面加载时 我必须从下拉列表中重新选择以捕获下拉列表的值 如果我正在浏览内容页面 则所选值不会在页面加载
  • 使用 Selenium WebDriver 直接操作剪贴板

    是否可以使用 Selenium WebDriver 用一些要粘贴的文本预先填充剪贴板 就像文本已复制到另一个应用程序中一样 最好使用 Python 绑定 不 似乎不是 作为一个browserSelenium 是一种操作工具 旨在执行浏览器特
  • Java连接多个数据库

    我正在创建一个连接到多个数据库的java应用程序 用户将能够从下拉框中选择他们想要连接的数据库 然后 程序通过将名称传递给创建初始上下文的方法来连接到数据库 以便它可以与 Oracle Web 逻辑数据源进行通信 public class
  • 编辑预定的待处理意向

    我编写了一个应用程序 可以在之前选择的预定时间打开 关闭 WiFi 它的工作方式非常简单 从时间选择器中选择时间 然后添加它 它以编程方式从时间选择器获取数据并设置和警报 我首先写下我的活动和广播接收器的代码 在这段代码下面我将写下我的问题