【问题标题】:How to set date to device calendar through user app and get the reminder in android?如何通过用户应用程序将日期设置为设备日历并在 android 中获取提醒?
【发布时间】:2014-04-03 10:27:34
【问题描述】:

大家好,请有人帮助我,我想通过我的应用程序将事件设置为设备日历。我需要从编辑文本(这是用户输入)中获取日期并单击按钮(比如设置日期)它应该作为事件保存到设备日历中。在上述日期,它应该提醒应用用户该事件。

我已经尝试了很多,但我无法做到这一点。作为一名大学生,我已经尽力了,但真的需要有人的帮助..

如果有人知道,请告诉我解决方案(如果提供了示例,那将是一个很大的帮助(请不要只告诉我有关 api 的信息,请使用 calendarprovider 和这个,我需要适当的示例))。 .

提前致谢

我通过以下代码打开了设备日历。这允许我手动设置事件,但我想在单击按钮后自动设置事件。 有人可以在这方面帮助我吗? 意图 i = new Intent();

//Froyo 或更高版本(请注意,我刚刚在 CM7 上测试过,小于 froyo 的版本可以正常工作,因此取决于手机...) cn = new ComponentName("com.google.android.calendar", "com.android.calendar.LaunchActivity");

//小于弗罗约 cn = new ComponentName("com.android.calendar", "com.android.calendar.LaunchActivity");

i.setComponent(cn); 开始活动(i);

上面的代码显示本地日历并允许用户自己设置事件。 但我不希望这种情况发生,而是希望本机日历接受用户输入并设置事件。因此,为此我实现了以下代码: 公共无效 addCalendarEvent(){

    Calendar cal = Calendar.getInstance();     
    Intent intent = new Intent(Intent.ACTION_EDIT);
    intent.setType("vnd.android.cursor.item/event");
    intent.putExtra("beginTime", cal.getTimeInMillis());
    intent.putExtra("allDay", true);
    intent.putExtra("rrule", "FREQ=YEARLY");
    intent.putExtra("UNTIL=20140404T080000Z",true);
    intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
    intent.putExtra("title", "A Test Event from android app");
    intent.putExtra("description", "A Test Description from android app");
    intent.putExtra("eventLocation", "Geolocation");
    startActivity(intent);

  }

我在按钮的 onClick() 方法中调用了这个方法。 我将日期硬编码为:intent.putExtra("UNTIL=20140404T080000Z",true); 但这不起作用。但是,我不想对其进行硬编码,它应该接受用户输入(动态它应该可以工作,比如获取edittext数据并通过意图传递)。 请问有人可以在这方面帮助我吗??

【问题讨论】:

    标签: android


    【解决方案1】:

    在单独使用这个答案之前,你必须在谷歌上进行一些搜索。这是您以完美方式学习新事物的唯一方法。
    1) 报警管理器
    2) 广播接收器
    3) 应用的意图和上下文

    好的,我们开始;
    首先,您需要获取日历对象以将日期和时间设置到 Android 警报管理器中。 MainActivity.java中的这个方法,在你的按钮点击事件中调用这个方法,同时传递相关参数。

    public void addReminder(int mYear, int mMonth, int mDay, int mHour, int mMinute){
    Calendar c = Calendar.getInstance();
    
                    //set Reminder time and date into calendar object               
                    c.set(Calendar.YEAR,mYear);
                    c.set(Calendar.MONTH, mMonth);//Don't use exact numeric value of the month, use one minus.Ex: April=>as 3
                    c.set(Calendar.DATE, mDay);
                    c.set(Calendar.HOUR_OF_DAY, mHour);             
                    c.set(Calendar.MINUTE, mMinute);
                    c.set(Calendar.SECOND, 0);
    
    
    
                    //Unique Alarm ID creation
    int alrmId=0;
                alrmId=Integer.parseInt(mMonth+""+mDay+""+mHour+""+mMinute);
                    //Alarm task creation
                    Intent in=new Intent(this,ReminderReceiver.class);
                    in.putExtra("text", "You have a Reminder!");
                    in.putExtra("AlrmId", alrmId);
    
    PendingIntent pi;
    
                    pi = PendingIntent.getBroadcast( this, alrmId, in,0 );
    
    AlarmManager am;
    
    
                    am = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));
    
                    am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),pi);                    
    
                }
    

    接下来,您需要有接收器来捕捉此 Firing 事件并显示通知、发出声音、振动设备。为此,我将 ReminderReceiver 类添加到项目中,

    public class ReminderReceiver extends BroadcastReceiver {
    // Vibrator object
    public Vibrator vibrator;
    long[] pattern = { 0L, 250L, 200L, 250L, 200L, 250L, 200L, 250L, 200L,
            250L, 200L, 250L, 200L, 250L, 200L };
    // Ringtone object
    Uri ringT;
    Ringtone ringTone;
    
    @Override
    public void onReceive(Context context, Intent intent) {
        String remindText = intent.getStringExtra("text");
    //Unique notification id for every alarm
        int receiverID = intent.getIntExtra("AlrmId", 0);
        // Notification creation
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                context).setSmallIcon(android.R.drawable.ic_popup_reminder)
                .setContentTitle("Reminder").setContentText(remindText);
    
        // Create vibrator pattern
        vibrator = (Vibrator) context
                .getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(pattern, -1);// No repetition
    
        // Notification tone creation and play
        ringT = RingtoneManager
                .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        ringTone = RingtoneManager.getRingtone(context, ringT);
        ringTone.play();
        // Create toast and show on center of the screen
        Toast toast = Toast.makeText(context, remindText, Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
        // end of toast...
    
        // Show status bar notification
        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(receiverID, mBuilder.build());
    
    }
    

    }
    全部做完 !!!只有一件事,在 Android Manifest.xml 中设置权限

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

    并注册 ReminderReceiver 类,将此行添加到 &lt;application&gt; &lt;/application&gt; 标记中(不在任何其他标记内!!!),

    <receiver android:name=".ReminderReceiver">
         </receiver>
    

    希望这会对您有所帮助。
    干杯!!!

    我用谷歌搜索了很多,但我没有找到解决方案或任何提示来实现它。 而且这段代码也不适合我。它不会在本地日历中创建任何事件。我签入了模拟器、bluestack 以及我的手机(未创建 apk 文件)。 请帮助我解决任何其他解决方案或改进这个
    这个Link 可以满足您的要求。

    【讨论】:

    • 此代码不会将任何事件添加到系统日历中,这是另一回事。您可以将其用作日历活动的提醒。
    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-10
    • 2017-07-14
    • 1970-01-01
    • 2016-09-16
    • 2012-04-21
    相关资源
    最近更新 更多