Insert events in android contacts - android

I'm trying to insert event using this method, it is getting executed properly but no contact event is in the contact.
private static void addEvent(ArrayList<ContentProviderOperation> contact_list,
ContactDataBean contactBean, int rawContactID){
contact_list.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID,
rawContactID)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Event.TYPE,
ContactsContract.CommonDataKinds.Event.TYPE_OTHER)
.withValue(ContactsContract.CommonDataKinds.Event.START_DATE,
contactBean.value).build());
try {
// Executing all the insert operations as a single
// database
// transaction
context.getContentResolver().applyBatch(
ContactsContract.AUTHORITY, contact_list);
Toast.makeText(context, "Contact is successfully added",
Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
e.printStackTrace();
} catch (OperationApplicationException e) {
e.printStackTrace();
}
}
No Error and no event is getting inserted.What's the problem??

you should add account(using calendar) or using intent
using intent:
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("title", "Event with " + cname);
intent.putExtra("beginTime", System.currentTimeMillis());
intent.putExtra("endTime", System.currentTimeMillis() + 1800 * 1000);
intent.putExtra("allDay", 0);
intent.putExtra("hasAlarm", 1);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, 103);
or using calander
TimeZone tz = TimeZone.getDefault();
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, startMillis);
values.put(CalendarContract.Events.DTEND, endMillis);
values.put(CalendarContract.Events.TITLE, "Reminder - " + cname);
values.put(CalendarContract.Events.DESCRIPTION, note);
values.put(CalendarContract.Events.CALENDAR_ID, calID);
values.put(CalendarContract.Events.EVENT_TIMEZONE, tz.getDisplayName());
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
long eventID = Long.parseLong(uri.getLastPathSegment());
values = new ContentValues();
values.put(CalendarContract.Reminders.MINUTES, 5);
values.put(CalendarContract.Reminders.EVENT_ID, eventID);
values.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
uri = cr.insert(CalendarContract.Reminders.CONTENT_URI, values);

Related

Adding Event to Outlook Calendar

Below is my code to Add calendar Account and then adding Event to that calendar,
AuthenticationManager.getInstance().setContextActivity(this);
AuthenticationManager.getInstance().connect(
new AuthenticationCallback<AuthenticationResult>() {
#Override
public void onSuccess(AuthenticationResult result) {
Log.i(TAG, "onConnectButtonClick - Successfully connected to Office 365");
String type = result.getUserInfo().getIdentityProvider();
String accountOwner = result.getUserInfo().getDisplayableId();
addCalendar(type, accountOwner);
}
#Override
public void onError(final Exception e) {
Log.e(TAG, "onCreate - " + e.getMessage());
}
});
Calendar Method:
public void addCalendar(String type, String accountOwner) {
ContentValues contentValues = new ContentValues();
contentValues.put(CalendarContract.Calendars.ACCOUNT_NAME, accountOwner);
contentValues.put(CalendarContract.Calendars.ACCOUNT_TYPE, type);
contentValues.put(CalendarContract.Calendars.NAME, accountOwner);
contentValues.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, "Outlook");
contentValues.put(CalendarContract.Calendars.CALENDAR_COLOR, "232323");
contentValues.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_EDITOR);
contentValues.put(CalendarContract.Calendars.OWNER_ACCOUNT, accountOwner);
contentValues.put(CalendarContract.Calendars.ALLOWED_REMINDERS, "METHOD_ALERT, METHOD_EMAIL, METHOD_ALARM");
contentValues.put(CalendarContract.Calendars.ALLOWED_ATTENDEE_TYPES, "TYPE_OPTIONAL, TYPE_REQUIRED, TYPE_RESOURCE");
contentValues.put(CalendarContract.Calendars.ALLOWED_AVAILABILITY, "AVAILABILITY_BUSY, AVAILABILITY_FREE, AVAILABILITY_TENTATIVE");
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CALENDAR}, 23);
}
Uri uri = CalendarContract.Calendars.CONTENT_URI;
uri = uri.buildUpon().appendQueryParameter(android.provider.CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, accountOwner)
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, type).build();
getContentResolver().insert(uri, contentValues);
}
Adding Event:
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, beginTime.getTimeInMillis());
values.put(CalendarContract.Events.DTEND, endTime.getTimeInMillis());
values.put(CalendarContract.Events.TITLE, "Tech Stores");
values.put(CalendarContract.Events.DESCRIPTION, "Successful Startups");
values.put(CalendarContract.Events.CALENDAR_ID, 10);
values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
values.put(CalendarContract.Events.EVENT_LOCATION, "London");
values.put(CalendarContract.Events.GUESTS_CAN_INVITE_OTHERS, "1");
values.put(CalendarContract.Events.GUESTS_CAN_SEE_GUESTS, "1");
values.put(CalendarContract.Events.ORGANIZER, "azhar#outlook.com");
cr.insert(CalendarContract.Events.CONTENT_URI, values);
The problem is, the event is not getting added to the desired Calendar. I'm not getting any error too. Any correction needed here?
You could add “ CALENDAR_ID ” parameter to the Events, For example:
values.put(CalendarContract.Events. CALENDAR_ID, “”);
For more information, please refer to this link:
CalendarContract.Events

Add events to Calendar programmatically

I'm using below code in Marshmellow device to add the event to the Calendar programmatically but it's not working. Any idea? I cannot see this event in the Calendar app.
long startMillis = 0;
long endMillis = 0;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startDate = null, endDate = null;
try{
startDate = simpleDateFormat.parse("2017-05-01 01:30:00");
startMillis = startDate.getTime();
endDate = simpleDateFormat.parse("2017-05-01 03:30:00");
endMillis = endDate.getTime();
}catch (ParseException e){
e.printStackTrace();
}
ContentResolver cr = this.getContentResolver();
ContentValues values = new ContentValues();
TimeZone timeZone = TimeZone.getDefault();
values.put(CalendarContract.Events.DTSTART, startMillis);
values.put(CalendarContract.Events.DTEND, endMillis);
values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
values.put(CalendarContract.Events.TITLE, "Hello Title");
values.put(CalendarContract.Events.DESCRIPTION, "Add events to Calendar");
values.put(CalendarContract.Events.CALENDAR_ID, 879);
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
PS: It works if only one gmail account is synced with the calendar app.
Am also find so many solutions finally i got solution and successfully calendar event added with Schedule event.
Step -1 > Enable Google calnedar API From google console.
Step - 2 > Add the permission in Androidmanifest.xml
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>
Step =3 > Add library for checking Permissions in your .gradle
compile 'com.karumi:dexter:4.1.0'
Dexter.withActivity(YoutActivityName.this)
.withPermission(Manifest.permission.WRITE_CALENDAR)
.withListener(new PermissionListener()
{
#Override
public void onPermissionGranted(PermissionGrantedResponse response) {
try {
int calenderId = -1;
String calenderEmaillAddress = "sathishmicit2012#gmail.com";
String[] projection = new String[]{
CalendarContract.Calendars._ID,
CalendarContract.Calendars.ACCOUNT_NAME};
ContentResolver cr = context.getContentResolver();
Cursor cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"), projection,
CalendarContract.Calendars.ACCOUNT_NAME + "=? and (" +
CalendarContract.Calendars.NAME + "=? or " +
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME + "=?)",
new String[]{calenderEmaillAddress, calenderEmaillAddress,
calenderEmaillAddress}, null);
if (cursor.moveToFirst()) {
if (cursor.getString(1).equals(calenderEmaillAddress)) {
calenderId = cursor.getInt(0);
}
}
long start2 = Calendar.getInstance().getTimeInMillis(); // 2011-02-12 12h00
long end2 = Calendar.getInstance().getTimeInMillis() + (4 * 60 * 60 * 1000); // 2011-02-12 13h00
String title = "This is my demo test with alaram with 5 minutes";
ContentValues cvEvent = new ContentValues();
cvEvent.put("calendar_id", calenderId);
cvEvent.put(CalendarContract.Events.TITLE, title);
cvEvent.put(CalendarContract.Events.DESCRIPTION, String.valueOf(start2));
cvEvent.put(CalendarContract.Events.EVENT_LOCATION, "Bhatar,Surat");
cvEvent.put("dtstart", start2);
cvEvent.put("hasAlarm", 1);
cvEvent.put("dtend", end2);
cvEvent.put("eventTimezone", TimeZone.getDefault().getID());
Uri uri = getContentResolver().insert(Uri.parse("content://com.android.calendar/events"), cvEvent);
// get the event ID that is the last element in the Uri
long eventID = Long.parseLong(uri.getLastPathSegment());
ContentValues values = new ContentValues();
values.put(CalendarContract.Reminders.MINUTES, 2);
values.put(CalendarContract.Reminders.EVENT_ID, eventID);
values.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALARM);
cr.insert(CalendarContract.Reminders.CONTENT_URI, values);
//Uri uri = cr.insert(CalendarContract.Reminders.CONTENT_URI, values);
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void onPermissionDenied(PermissionDeniedResponse response) {/* ... */}
#Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {/* ... */}
}).check();
Hello Calender ID is 879 you cant take it random. Calender ID must be from this URI content://com.android.calendar/calendars
int calenderId=-1;
String calenderEmaillAddress="xxx#gmail.com";
String[] projection = new String[]{
CalendarContract.Calendars._ID,
CalendarContract.Calendars.ACCOUNT_NAME};
ContentResolver cr = activity.getContentResolver();
Cursor cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"), projection,
CalendarContract.Calendars.ACCOUNT_NAME + "=? and (" +
CalendarContract.Calendars.NAME + "=? or " +
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME + "=?)",
new String[]{calenderEmaillAddress, calenderEmaillAddress,
calenderEmaillAddress}, null);
if (cursor.moveToFirst()) {
if (cursor.getString(1).equals(calenderEmaillAddress))
calenderId=cursor.getInt(0); //youre calender id to be insered in above your code
}

How to create a reminder notification in a specific day of the year?

I am trying to notify my app users with a notification in a specific day in the year and I am stuck in the middle as I did scheduled notifications at a regular bases (daily, weekly,..) and I don't want that.
Here is the code:
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", false);
intent.putExtra("rrule", "FREQ=DAILY");
intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
intent.putExtra("title", "A Test Event from android app");
startActivity(intent);
private String getCalendarUriBase(Activity act) {
String calendarUriBase = null;
Uri calendars = Uri.parse("content://calendar/calendars");
Cursor managedCursor = null;
try {
managedCursor = act.managedQuery(calendars, null, null, null, null);
} catch (Exception e) {
}
if (managedCursor != null) {
calendarUriBase = "content://calendar/";
} else {
calendars = Uri.parse("content://com.android.calendar/calendars");
try {
managedCursor = act.managedQuery(calendars, null, null, null, null);
} catch (Exception e) {
}
if (managedCursor != null) {
calendarUriBase = "content://com.android.calendar/";
}
}
return calendarUriBase; }
// get calendar
Calendar cal = Calendar.getInstance();
Uri EVENTS_URI = Uri.parse(getCalendarUriBase(this) + "events");
ContentResolver cr = getContentResolver();
// event insert
ContentValues values = new ContentValues();
values.put("calendar_id", 1);
values.put("title", "Reminder Title");
values.put("allDay", 0);
values.put("dtstart", cal.getTimeInMillis() + 11*60*1000); // event starts at 11 minutes from now
values.put("dtend", cal.getTimeInMillis()+60*60*1000); // ends 60 minutes from now
values.put("description", "Reminder description");
values.put("visibility", 0);
values.put("hasAlarm", 1);
Uri event = cr.insert(EVENTS_URI, values);
// reminder insert
Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(this) + "reminders");
values = new ContentValues();
values.put( "event_id", Long.parseLong(event.getLastPathSegment()));
values.put( "method", 1 );
values.put( "minutes", 10 );
cr.insert( REMINDERS_URI, values );
manifest
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
But I want to create a reminder notification in a specific day of the year not at regular bases (daily, weekly, etc). How can I write the code for that?
Use AlarmManager. Something like this:
Calendar cal = Calendar.getInstance();
cal.setTime(...);
Intent i = new Intent(context, YourBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);
You can use alarmManager service for this purpose!

Calendar sync with custom add account in android

I want to add events in calendar, i am able to add account but i don`t know how to sync with calendar.
should i create a new calendar id ?
how to verify event is created on calendar?
Check out this Google code project which will guide you.
In that project there is a method which will give you id of account id associated with Calender.
private int ListSelectedCalendars() {
int result = 0;
String[] projection = new String[] { "_id", "name" };
String selection = "selected=1";
String path = "calendars";
Cursor managedCursor = getCalendarManagedCursor(projection, selection,
path);
if (managedCursor != null && managedCursor.moveToFirst()) {
Log.i(DEBUG_TAG, "Listing Selected Calendars Only");
int nameColumn = managedCursor.getColumnIndex("name");
int idColumn = managedCursor.getColumnIndex("_id");
do {
String calName = managedCursor.getString(nameColumn);
String calId = managedCursor.getString(idColumn);
Log.i(DEBUG_TAG, "Found Calendar '" + calName + "' (ID="
+ calId + ")");
// You have to give email id in below line, right now i puted my email id
if (calName != null && calName.contains("vimalrajpara2006#gmail.com")) {
result = Integer.parseInt(calId);
}
} while (managedCursor.moveToNext());
} else {
Log.i(DEBUG_TAG, "No Calendars");
}
return result;
}
based on that id you can add events as well search also.
private Uri MakeNewCalendarEntry(int calId/*Value received from ListSelectedCalendars function*/) {
ContentValues event = new ContentValues();
event.put("calendar_id", calId);
event.put("title", "Today's Event [TEST]");
event.put("description", "2 Hour Presentation");
event.put("eventLocation", "Online");
long startTime = System.currentTimeMillis() + 1000 * 60 * 60;
long endTime = System.currentTimeMillis() + 1000 * 60 * 60 * 2;
event.put("dtstart", startTime);
event.put("dtend", endTime);
event.put("allDay", 0); // 0 for false, 1 for true
event.put("eventStatus", 1);
event.put("visibility", 0);
event.put("transparency", 0);
event.put("hasAlarm", 0); // 0 for false, 1 for true
Uri eventsUri = Uri.parse(getCalendarUriBase()+"events");
Uri insertedUri = getContentResolver().insert(eventsUri, event);
return insertedUri;
}
For more functionality check out above mention Google code project.
this is work for me remember I am working on android 4.0
void createCalendar(Datahelper dh, Context mContext, Account account)
{
final ContentValues v = new ContentValues();
v.put(CalendarContract.Calendars.NAME,account.name);
v.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, account.name);
v.put(CalendarContract.Calendars.ACCOUNT_NAME, account.name);
v.put(CalendarContract.Calendars.ACCOUNT_TYPE, account.type);
v.put(CalendarContract.Calendars.CALENDAR_COLOR, Color.GREEN);
v.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL,Calendars.CAL_ACCESS_OWNER);
v.put(CalendarContract.Calendars.OWNER_ACCOUNT, account.name);
v.put(CalendarContract.Calendars._ID, 123);// u can give any id there and use same id any where u need to create event
v.put(Calendars.SYNC_EVENTS, 1);
v.put(Calendars.VISIBLE, 1);
Uri creationUri = asSyncAdapter(Calendars.CONTENT_URI, account.name, account.type);
Uri calendarData = mContext.getContentResolver().insert(creationUri, v);
long id = Long.parseLong(calendarData.getLastPathSegment());
}
private Uri asSyncAdapter(Uri uri, String account, String accountType)
{
return uri.buildUpon().appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true").appendQueryParameter (Calendars.ACCOUNT_NAME,account) .appendQueryParameter(Calendars.ACCOUNT_TYPE, accountType) .build();
}
ContentValues values = new ContentValues();
String eventTitle = eventsubject[i];
values.put(Events.DTSTART, startMillis);
values.put(Events.HAS_ALARM, 1);
values.put(Events.DTEND, endMillis);
values.put(Events.EVENT_COLOR, Color.BLUE);
values.put(Events.TITLE, eventTitle);
values.put(Events.DESCRIPTION, "");
values.put(Events.CALENDAR_ID, calendarId1);
values.put(Events.EVENT_TIMEZONE, TimeZone.getDefault() .getID());
Uri uri = cr.insert(Events.CONTENT_URI, values);
long eventID = Long.parseLong(uri.getLastPathSegment());
ContentValues reminders = new ContentValues();
reminders.put(Reminders.EVENT_ID, eventID);
reminders.put(Reminders.METHOD, Reminders.METHOD_ALERT);
reminders.put(Reminders.MINUTES, 2);
Uri uri2 = cr.insert(Reminders.CONTENT_URI, reminders);
Log.e("Reminder", "addreminder" + uri2);

Unable to start activity ComponentInfo java.lang.NullPointerException

Actually I have two java classes of which one is an activity class. Now from that activity class, I want to call a function of second class which is not in an activity class. So all works fine but when I use SharedPreferences inside that function it shows me an error Unable to start activity ComponentInfo java.lang.NullPointerException. Please, anyone help.
code for first java file:
public class SplashScreen extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
CycleManager.getSingletonObject().test();//call functions of another class
}
}
code for second java file:
public class CycleManager
{
private static CycleManager cycleMangrObject;
private CycleManager() {
onInitialization();
//Compute averages using data loaded from register
ComputeAverages();
}
public static synchronized CycleManager getSingletonObject() {
if (cycleMangrObject == null) {
cycleMangrObject = new CycleManager();
}
return cycleMangrObject;
}
public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
public void test()
{
SharedPreferences preferences1 =getSharedPreferences("myPreferences", 0);
}
public void setAlertOnDevice(){
//Delete data
Uri EVENTS_URI = Uri.parse(getCalendarUriBase(this) + "events");
int id = 1; // calendar entry ID
ContentResolver cr = getContentResolver();
EVENTS_URI= ContentUris.withAppendedId(EVENTS_URI, id);
cr.delete(EVENTS_URI, "calendar_id=1", null);
Resources res=getResources();
//set Alerts in device calendar
Date dtStartDate = CycleManager.getSingletonObject().getStartDate();
boolean bDeleteAndReturn = false;
Calendar cal = Calendar.getInstance();
if (dtStartDate.getTime() == CycleManager.getSingletonObject().getDefaultDate().getTime())
{
bDeleteAndReturn = true;
dtStartDate = cal.getTime();
}
getOffsetsForCycleStages(CycleManager.getSingletonObject().iAvgCycleTime);
if(bDeleteAndReturn==false)
{
if (CycleManager.getSingletonObject().bNextCycleAlert && iStart>0)
{
cal.setTime(dtStartDate);
cal.add(Calendar.DATE, iStart);
// ContentResolver cr = getContentResolver();
String str=res.getString(R.string.alert_start);
String strDescription=res.getString(R.string.alert_start_msg);
ContentValues values = new ContentValues();
values.put("calendar_id", 1);
values.put("title", str);
values.put("description", strDescription);
values.put("dtstart", cal.getTimeInMillis());
values.put("dtend", cal.getTimeInMillis());
cr.insert(EVENTS_URI, values);
}
if (CycleManager.getSingletonObject().bSafeAlert)
{
if (iSafe1>0)
{
cal.setTime(dtStartDate);
cal.add(Calendar.DATE, iSafe1);
// ContentResolver cr = getContentResolver();
String str=res.getString(R.string.alert_safe);
String strDescription=res.getString(R.string.alert_safe_msg) + " " + new Integer(iUnsafe1-iSafe1-1);
ContentValues values = new ContentValues();
values.put("calendar_id", 1);
values.put("title", str);
values.put("description", strDescription);
values.put("dtstart", cal.getTimeInMillis());
values.put("dtend", cal.getTimeInMillis());
cr.insert(EVENTS_URI, values);
}
if (iSafe2>0)
{
cal.setTime(dtStartDate);
cal.add(Calendar.DATE, iSafe2);
// ContentResolver cr = getContentResolver();
String str=res.getString(R.string.alert_safe);
String strDescription=res.getString(R.string.alert_safe_msg) + " " + new Integer(CycleManager.getSingletonObject().iAvgCycleTime-iSafe2-1);
ContentValues values = new ContentValues();
values.put("calendar_id", 1);
values.put("title", str);
values.put("description", strDescription);
values.put("dtstart", cal.getTimeInMillis());
values.put("dtend", cal.getTimeInMillis());
cr.insert(EVENTS_URI, values);
}
}
if (CycleManager.getSingletonObject().bUnsafeAlert)
{
if (iUnsafe1>0)
{
cal.setTime(dtStartDate);
cal.add(Calendar.DATE, iUnsafe1);
// ContentResolver cr = getContentResolver();
String str=res.getString(R.string.alert_unsafe);
String strDescription=res.getString(R.string.alert_unsafe_msg) + " " + new Integer(iFertile-iUnsafe1-1);
ContentValues values = new ContentValues();
values.put("calendar_id", 1);
values.put("title", str);
values.put("description", strDescription);
values.put("dtstart", cal.getTimeInMillis());
values.put("dtend", cal.getTimeInMillis());
cr.insert(EVENTS_URI, values);
}
if (iUnsafe2>0)
{
cal.setTime(dtStartDate);
cal.add(Calendar.DATE, iUnsafe2);
// ContentResolver cr = getContentResolver();
String str=res.getString(R.string.alert_unsafe);
String strDescription=res.getString(R.string.alert_unsafe_msg) + " " + new Integer(iSafe2-iUnsafe2-1);
ContentValues values = new ContentValues();
values.put("calendar_id", 1);
values.put("title", str);
values.put("description", strDescription);
values.put("dtstart", cal.getTimeInMillis());
values.put("dtend", cal.getTimeInMillis());
cr.insert(EVENTS_URI, values);
}
}
if (CycleManager.getSingletonObject().bFertileAlert && iFertile>0)
{
cal.setTime(dtStartDate);
cal.add(Calendar.DATE, iFertile);
// ContentResolver cr = getContentResolver();
String str=res.getString(R.string.alert_fertile);
String strDescription=res.getString(R.string.alert_fertile_msg) + " " + new Integer(iUnsafe2-iFertile-1);
ContentValues values = new ContentValues();
values.put("calendar_id", 1);
values.put("title", str);
values.put("description", strDescription);
values.put("dtstart", cal.getTimeInMillis());
values.put("dtend", cal.getTimeInMillis());
cr.insert(EVENTS_URI, values);
}
if (CycleManager.getSingletonObject().bPMSAlert)
{
cal.setTime(dtStartDate);
cal.add(Calendar.DATE, iStart-7);
// ContentResolver cr = getContentResolver();
String str=res.getString(R.string.alert_pms);
String strDescription=res.getString(R.string.alert_pms_msg);
ContentValues values = new ContentValues();
values.put("calendar_id", 1);
values.put("title", str);
values.put("description", strDescription);
values.put("dtstart", cal.getTimeInMillis());
values.put("dtend", cal.getTimeInMillis());
cr.insert(EVENTS_URI, values);
}
}
}
private String getCalendarUriBase(Activity act){
String calendarUriBase = null;
Uri calendars = Uri.parse("content://calendar/calendars");
Cursor managedCursor = null;
try
{
managedCursor = act.managedQuery(calendars, null, null, null, null);
}
catch (Exception e) {}
if (managedCursor != null)
{
calendarUriBase = "content://calendar/";
}
else
{
calendars = Uri.parse("content://com.android.calendar/calendars");
try
{
managedCursor = act.managedQuery(calendars, null, null, null, null);
}
catch (Exception e){}
if (managedCursor != null)
{
calendarUriBase = "content://com.android.calendar/";
}
}
return calendarUriBase;
}
Its not possible because the other class does't know there something called SharedPreference.. so
public void test(Context c)
{
SharedPreferences preferences1 =c.getSharedPreferences("myPreferences", 0);
}
and while calling
CycleManager.getSingletonObject().test(this);//call functions of another class
and again
c.getContentResolver().delete(EVENTS_URI, null, null);
CycleManager.getSingletonObject().test(this);
public void test(Context c)
{
SharedPreferences prefs;
prefs = PreferenceManager.getDefaultSharedPreferences(c);
}
No you cannot use the Activity or Android functions in an ordinary simple java class. Since you can only use java library in that Simple Java Class
One possible solution is:
Create a public static SharedPreferences object in an activity and Create an object of SplashScreen like this:
public static SharedPreferences pref = null;
public static SplashScreen sp;
In onCreate of Splash Screen do this
sp = this;
In your java Class method do this
SplashScreen.pref = SplashScreen.sp.getSharedPreferences("my pref", 0);

Categories

Resources