【问题标题】:Changing a reminder on a calendar event changes the event id, not reminder id更改日历事件的提醒会更改事件 ID,而不是提醒 ID
【发布时间】:2013-02-23 00:19:47
【问题描述】:

我正在尝试编写一些代码来判断事件的提醒是否已更改(添加或删除),但我遇到了一个奇怪的事件。而不是删除或更改提醒,似乎正在删除并重新添加事件!我的代码不正确吗?或者这就是真正发生的事情?

要对此进行测试,您需要:

  1. 创建带有提醒的事件

  2. 在你的模拟器同步后点击按钮

  3. 移除事件提醒

  4. 等待更改同步到您的模拟器,然后再次单击按钮

JAVA 代码

package com.example.remindertest;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CalendarContract.CalendarAlerts;
import android.provider.CalendarContract.Events;
import android.provider.CalendarContract.Instances;
import android.provider.CalendarContract.Reminders;
import android.util.Log;
import android.view.View;

public class ReminderActivity extends Activity
{
    final static public String TAG = "ReminderActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Add button
        setContentView(R.layout.reminder_activity);
        Log.d(TAG, "Started");
    }

    public void onClick(View v)
    {
        Cursor cursor = getContentResolver().query(
            Reminders.CONTENT_URI,
            null, null, null, null
        );

        String str = "";

        /*
        * Some crappy code to test this
        */
        while ( cursor.moveToNext())
        {
            int eventId = cursor.getInt(cursor.getColumnIndex(Reminders.EVENT_ID));
            Cursor eCursor = getContentResolver().query(
                Events.CONTENT_URI,
                new String[] { Events._ID, Events.TITLE },
                "_id = ?", new String[] {"" + eventId}, ( String ) null);

            eCursor.moveToNext();
            String title = eCursor.getString(eCursor.getColumnIndex(Events.TITLE));

            str += eventId + " = " + title + " = " +
                cursor.getInt(cursor.getColumnIndex(Reminders._ID)) + " = " +
                cursor.getInt(cursor.getColumnIndex(Reminders.METHOD)) + " = " +
                cursor.getLong(cursor.getColumnIndex(Reminders.MINUTES)) + "\n";
        }

        Log.d(TAG, str);
    }
}

布局 XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".ReminderActivity" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="98dp"
    android:onClick="onClick"
    android:text="Button" />

</RelativeLayout>

清单 XML

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.remindertest"
  android:versionCode="1"
  android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.READ_CALENDAR"/>
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
    android:name="com.example.remindertest.ReminderActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>
</application>

</manifest>

【问题讨论】:

  • 在我的手机上发生的情况完全一样......奇怪。

标签: android android-contentprovider android-calendar


【解决方案1】:

不,您的代码是正确的。在我的 6 台设备上发生的情况完全相同 - 当您编辑它时,事件会更改它的 id(可能是重新创建)。顺便说一句,当您更改活动的时间、标题、地点或任何其他细节时也会发生这种情况 - 而不仅仅是提醒。有趣的是,它并不总是发生——只是有时。但通常 2-3 次编辑事件足以模拟这种行为。

我不太清楚 event 频繁更改 id 的原因,但基于此我想说,事件的 id 不应该用作唯一标识符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-07
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    • 2019-12-21
    • 2013-06-27
    相关资源
    最近更新 更多