使用来自BroadcastReceiver Android的警报添加日历事件

时间:2015-11-05 04:10:09

标签: java android calendar broadcastreceiver android-contentresolver

我需要在收到推送通知时以编程方式设置日历事件。我的BroadcastReceiver中已经有了所有推送通知代码,但是当我尝试使用ContentResolver添加日历事件时,我的应用程序崩溃了。

我得到的错误是: java.lang.RuntimeException:无法启动接收器m.example.com.rm.CustomHandler:java.lang.SecurityException:Permission Denial:从ProcessRecord打开提供程序com.android.providers.calendar.CalendarProvider2 {e60c308 30235:m .example.com.rm / u0a63}(pid = 30235,uid = 10063)要求

android.permission.READ_CALENDAR 

android.permission.WRITE_CALENDAR

(我有added两个必要permissions

这是我的代码:

       public class CustomHandler extends BroadcastReceiver
       {

            public void onReceive(Context context, Intent intent)
            {
                    long startMillis = 0 ;
                    long endMillis = 0;

                    Calendar beginTime = Calendar.getInstance() ;
                    beginTime.setTime(s.getDate()) ;
                    startMillis = beginTime.getTimeInMillis() ;

                    Calendar endTime = Calendar.getInstance();
                    Date endDate = s.getDate() ;
                    endDate.setMinutes(endDate.getMinutes() + 5) ;
                    endTime.setTime(endDate);
                    endMillis = endTime.getTimeInMillis();


                    long calID = 3;
                    ContentResolver cr = c.getContentResolver();
                    ContentValues values = new ContentValues();
                    values.put(Events.DTSTART, startMillis);
                    values.put(Events.DTEND, endMillis);
                    values.put(Events.TITLE, "BP Reading");
                    values.put(Events.DESCRIPTION, "Please open the RMCuff App to take your scheduled reading :)");

                    values.put(Events.CALENDAR_ID, calID);

                    values.put(Events.EVENT_TIMEZONE, "America/New_York");
                    values.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
                    Uri uri = cr.insert(Events.CONTENT_URI, values);
                    }
            }

清单文件:

<?xml version="1.0" encoding="utf-8"?>

<uses-feature
    android:name="android.hardware.telephony"
    android:required="false" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<!-- GCM connects to Google Services. -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<permission
    android:name="com.example.m.rm.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example.m.rm.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive dataf message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<application
    android:name=".ObjectPreference"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:launchMode="singleInstance" >
        <intent-filter>
            <action android:name="com.example.m.rm.MESSAGE" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".CaregiversPage"
        android:label="@string/title_activity_caregivers_page"
        android:launchMode="singleInstance"
        android:windowSoftInputMode="stateHidden" >
    </activity>
    <activity
        android:name=".NewCaregiverPage"
        android:label="@string/title_activity_new_caregiver_page"
        android:launchMode="singleInstance"
        android:windowSoftInputMode="stateHidden" >
    </activity>

    <receiver
        android:name="com.pushbots.google.gcm.GCMBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>

            <!-- Receives the actual messages. -->
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <!-- Receives the registration id. -->
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <category android:name="com.example.m.rm" />
        </intent-filter>
    </receiver>
    <receiver android:name=".CustomHandler" />

    <service android:name="com.pushbots.push.GCMIntentService" />

    <activity
        android:name=".NewSchedulePage"
        android:label="@string/title_activity_new_schedule_page"
        android:launchMode="singleInstance"
        android:windowSoftInputMode="stateHidden">
    </activity>
</application>

0 个答案:

没有答案