So in the app I am developing the user can types in a string and selects an address. When they select done the address is put into a geofence and when they enter that geofence a notification is posted. The problem I am having is when I try to add the strings of a row from that name on notification press. The names are the same yet the cursor can not find the row with the string. Thanks in advance!
Here is the error:
android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at com.nick.mowen.receiptmanager.ManagerDatabaseAdapter.getDataArray(ManagerDatabaseAdapter.java:64)
at com.nick.mowen.receiptmanager.GeofenceTransitionsIntentService.getAppToOpen(GeofenceTransitionsIntentService.java:35)
at com.nick.mowen.receiptmanager.GeofenceTransitionsIntentService.sendNotification(GeofenceTransitionsIntentService.java:82)
at com.nick.mowen.receiptmanager.GeofenceTransitionsIntentService.onHandleIntent(GeofenceTransitionsIntentService.java:64)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.os.HandlerThread.run(HandlerThread.java:61)
Here is the method to get the String[] of values:
public String[] getDataArray(String name) {
String[] columns = {ManagerHelper.NAME,ManagerHelper.CODE,ManagerHelper.ADDRESS};
SQLiteDatabase db = helper.getWritableDatabase();
Cursor cursor = db.query(ManagerHelper.TABLE_NAME, columns, ManagerHelper.NAME + " = '" + name + "'", null, null, null, null);
String[] data = new String[3];
data[0] = cursor.getString(1);
data[1] = cursor.getString(2);
data[2] = cursor.getString(3);
return data;
}
Here is the code to add the notification and get details:
public String[] getAppToOpen(String names) {
managerDatabaseAdapter = new ManagerDatabaseAdapter(this);
selectArgs = managerDatabaseAdapter.getDataArray(names);
return selectArgs;
}
protected void onHandleIntent(Intent intent) {
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError()) {
String errorMessage = "There is hopefully no error";
Log.e(TAG, errorMessage);
return;
}
// Get the transition type.
int geofenceTransition = geofencingEvent.getGeofenceTransition();
// Test that the reported transition was of interest.
if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) {
// Get the geofences that were triggered. A single event can trigger
// multiple geofences.
List triggeringGeofences = geofencingEvent.getTriggeringGeofences();
// Get the transition details as a String.
String geofenceTransitionDetails = getGeofenceTransitionDetails(
geofenceTransition,
triggeringGeofences
);
// Send notification and log the transition details.
sendNotification(geofenceTransitionDetails);
//Log.i(TAG, geofenceTransitionDetails);
} else {
// Log the error.
}
}
private String getGeofenceTransitionDetails(int geofenceTransition, List triggeringGeofences) {
String[] split = {":", " "};
String name = triggeringGeofences.get(0).toString();
newSplit = name.split(split[0]);
secondSplit = newSplit[1].split(split[1]);
return secondSplit[0];
}
private void sendNotification(String geofenceTransitionDetails) {
NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(this).setContentTitle("Receipt Code Reminder").setContentText(geofenceTransitionDetails).setSmallIcon(R.drawable.ic_stat_maps_local_restaurant).setTicker("Receipt Code Reminder").setAutoCancel(true);
Intent localIntent = new Intent(this, ViewCodeActivity.class);
localIntent.putExtra(EXTRA_MESSAGE, getAppToOpen(geofenceTransitionDetails));
TaskStackBuilder localTaskStackBuilder = TaskStackBuilder.create(this);
localTaskStackBuilder.addParentStack(ViewCodeActivity.class);
localTaskStackBuilder.addNextIntent(localIntent);
builder.setContentIntent(localTaskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT));
((NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE)).notify(this.mID, builder.build());
}
}
"Index -1 requested" indicates that you are querying a Cursor without first calling cursor.moveToFirst(). Also, it is highly recommended that you use constants with semantic names for your columns, rather than 1, 2 and 3.
Related
My purpose is to set a list that comes from API which includes phonenumbers of the users of my app. So i only need to see contact that are in this list. But I dont know how set below code to do this. I mean in the below it opens contacts with all of the contact that are in the phone. I dont need to see the all the contacts but the ones that are in the given list
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent,1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case 1 :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(contactData, null, null, null, null);
if (cur.getCount() > 0) {// thats mean some resutl has been found
if(cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.e("Names", name);
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
// Query phone here. Covered next
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.e("Number", phoneNumber);
}
phones.close();
}
}
}
cur.close();
}
break;
}
}
Consider you already have a list which has data from API, and create new list which will have common elements from API list and ContentResolver, now to add elements to your desired list, in ContentResolver check if the PhoneNumber is in API list before adding it to output.
ArrayList<String> apiPhoneNumbers;// this data is populated by API
ArrayList<String> commonPhoneNumbers = new ArrayList<>();
//inside onActivityResult
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0){
// Query phone here. Covered next
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.e("Number", phoneNumber);
if(apiPhoneNumbers.contains(phoneNumber)){//add to output only if it is in API list
commonPhoneNumbers.add(phoneNumber);
}
}
phones.close();
}
I'm using a contact picker as such:
startActivityForResult(
new Intent(
Intent.ACTION_PICK,
Phone.CONTENT_URI
), CONTACT_PICKER_RESULT //1001
);
This type of contact picker picks out a specific phone number that a contact has.
I then get an ID for this contact with:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode != CONTACT_PICKER_RESULT || resultCode != RESULT_OK)
return;
String id = data.getData.getLastPathSegment();
}
However, when I use a PhoneLookup query, such as:
Cursor cur = getContentResolver().query(
Uri.withAppendedPath(
PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(...) //phone number of contact is filled in
),
new String[] {
PhoneLookup.DISPLAY_NAME,
Phone._ID
}, null, null, null
);
if (!cur.moveToFirst())
return;
String id = cur.getString(
cur.getColumnIndex(Phone._ID)
);
The ID I get from the PhoneLookup is different from the onActivityResult. For example, the contact picker returns 1408 while the cursor returns 444.
How can I get:
data.getData().getLastPathSegment() equal to cur.getColumnIndex(...)?
You can't use a PhoneLookup in this case. You need to query Phone.CONTENT_URI to obtain the ID that matches up with the contact picker. The following code below does exactly this.
Cursor cur = getContentResolver().query(
Phone.CONTENT_URI,
new String[] {
Phone.DISPLAY_NAME,
Phone._ID
},
Phone.NORMALIZED_NUMBER + "=?",
new String[] {
... //phone number placed here
}, null);
if (!cur.moveToFirst())
return;
String id = cur.getString(cur.getColumnIndex(Phone._ID)));
I have made two geofences by adding the coordinates from a sqlite table. It does create the geofences on the map and transitions also do occur.
My problem is that all the transitions trigger the same broadcast, this means no matter what two geofences I enter, it shows the same notification.
How do I get location specific messages?
I have used this line of code to get the triggered fence's ID and this works properly, but it also gives unwanted things such as the longitude, latitude and radius. How do I extract only the ID from this?
List<Geofence> ab = LocationClient.getTriggeringGeofences(intent);
This is the relevant part of the code.
#Override
public void onConnected(Bundle arg0) {
mClient.requestLocationUpdates(mRequest, this);
SQLiteDatabase db = database.getWritableDatabase();
String[] columns = {PromoDatabase.LID,PromoDatabase.lRestuarantID,PromoDatabase.lBranchID,PromoDatabase.Latitude,PromoDatabase.Longitude};
Cursor cursor = db.query(PromoDatabase.LOCATION_TABLE, columns, null, null, null, null, null);
String RestuarantName = null;
while(cursor.moveToNext()) //create the two geofences with with help of sqlite table
{
String LocationID = cursor.getString(0);
String RestuarantID = cursor.getString(1);
double latitude = cursor.getDouble(3);
double longitude = cursor.getDouble(4);
RestuarantName = getData(RestuarantID); //get the restuarant name by giving the RestuarantID
float radius = 800;
// Build a Geofence
Geofence fence = new Geofence.Builder()
.setRequestId(LocationID)
.setCircularRegion(latitude, longitude, radius)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.build();
mList.add(fence);
googleMap.addMarker( new MarkerOptions()
.position( new LatLng(latitude, longitude) )
.title(RestuarantName+": Fence " + LocationID)
.snippet("Radius: " + radius) ).showInfoWindow();
circleOptions = new CircleOptions()
.center( new LatLng(latitude, longitude) )
.radius( radius )
.fillColor(0x40ff0000)
.strokeColor(Color.TRANSPARENT)
.strokeWidth(2);
googleMap.addCircle(circleOptions);
}
// Method 2: Using Broadcast
Intent intent = new Intent();
intent.setAction(GeofenceEventReceiver.GEOFENCE_EVENTS); // Specify the action, a.k.a. receivers
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.putExtra("Location", "KFC");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Send out the Geofence request
mClient.addGeofences(mList, pendingIntent, this);
}
// Broadcast receiver used to receive broadcast sent from the GeofenceIntentService
public class GeofenceEventReceiver extends BroadcastReceiver {
public static final String GEOFENCE_EVENTS = "com.Drogo.proto.GeofenceEvents";
#Override
public void onReceive(Context context, Intent intent) {
String locationInfo = "Arraived at " + intent.getStringExtra("Location");
List<Geofence> BreachedGeofence = LocationClient.getTriggeringGeofences(intent);
Toast.makeText(context, locationInfo + BreachedGeofence, Toast.LENGTH_LONG).show();
notifyMe(context,intent, locationInfo); //this will basically give me a notification through the notification manager.
}
Use
List <Geofence> triggerList = getTriggeringGeofences(intent);
String[] triggerIds = new String[geofenceList.size()];
for (int i = 0; i < triggerIds.length; i++) {
triggerIds[i] = triggerList.get(i).getRequestId();
}
taken from the Android documentation here.
Did you want something different from that?
My application has the following modules,
To collect users CB location code.
To save that in a database of user's choice, say for example my CB code is 465783 and I can save that as 'College' in my database.
To provide alarm feature, in this module I can give a text input say I give it as 'College' and when the Cell Broadcast is updated if the value college matches alarm is given out.
Now, in my below code I've achieved first 2 modules and also the required database entries, databases search etc, I'm not able to read the updated CB location value.
public class Alarm extends MainActivity {
public String str;
public void onReceive(Context context, Intent intent) {
//---get the CB message passed in---
Bundle bundle = intent.getExtras();
SmsCbMessage[] msgs = null;
str = "";
if (bundle != null) {
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsCbMessage[pdus.length];
for (int i=0; i<msgs.length; i++) {
msgs[i] = SmsCbMessage.createFromPdu((byte[])pdus[i]);
str += "CB " + msgs[i].getGeographicalScope() + msgs[i].getMessageCode() + msgs[i].getMessageIdentifier() + msgs[i].getUpdateNumber();
str += " :";
str += "\n";
}
}
}
EditText user_value;
Button startalarm;
Button stopalarm;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.third);
startalarm = (Button) findViewById(R.id.startalarm);
stopalarm = (Button) findViewById(R.id.stopalarm);
user_value = (EditText) findViewById(R.id.user_value);
final Ringtone ringtone;
ringtone = RingtoneManager.getRingtone(getBaseContext(), RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE));
startalarm.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0)
{
// TODO Auto-generated method stub
if(user_value.length()==0)
{
Toast.makeText(getBaseContext(), "Please enter a value.", Toast.LENGTH_LONG).show();
}
Toast.makeText(getApplicationContext(), "alarm set", Toast.LENGTH_LONG).show();
//I want my alarm event to be started from here whenever a new CB sms arrives.
SQLiteDatabase aa = openOrCreateDatabase("MLIdata", MODE_WORLD_READABLE, null);
Cursor c = aa.rawQuery("SELECT CblocationName FROM MLITable WHERE CblocationCode = '"+str+"'", null);
c.moveToFirst();
c.getString(c.getColumnIndex("CblocationName"));
String sas = user_value.getText().toString();
if(sas.equals(c.getString(c.getColumnIndex("CblocationName"))))
{
//here comes the alarm code
if(ringtone == null)
{
Log.d("Debug", "ringtone is null");
}
else
{
ringtone.play();
}
}
}
});
stopalarm.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
ringtone.stop();
}
});
}
}
Detailed Explanation : whenever a user enters a new tower location he gets the updated Cell Broadcast message from the tower, so when this cbsms arrives I need to start my event of retrieving the CB area code and compare it with my database of area codes (which obviously have corresponding area code names set by the user) and when there is a match between the user given area name's corresponding area code with my current area code an alarm needs to be started, here I'm not able to do detect the arrival of updated location.
If further explanation is required of my problem statement, please comment.
From the comments received below, I've deduced that I'd need a receiver class, I've created one for my widget which does the same function (Displays the Cb location code on the widget), Now I do not how to activate that in my app.
My WidgetReceiver.java
public class CbReceiver extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent) {
//---get the CB message passed in---
Bundle bundle = intent.getExtras();
SmsCbMessage[] msgs = null;
String str = "";
if (bundle != null) {
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsCbMessage[pdus.length];
for (int i=0; i<msgs.length; i++) {
msgs[i] = SmsCbMessage.createFromPdu((byte[])pdus[i]);
str += "CB " + msgs[i].getGeographicalScope() + msgs[i].getMessageCode() + msgs[i].getMessageIdentifier() + msgs[i].getUpdateNumber();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
abortBroadcast();
Toast.makeText(context, str, Toast.LENGTH_LONG).show();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget);
ComponentName thisWidget = new ComponentName(context,MyWidget.class);
remoteViews.setTextViewText(R.id.update,str);
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
}}
}
Help required.
Thank you.
Start your activity from your receiver on tower change. Use Intents to start your activity from receivers.
Ex: Refer this alarm example for above point, Alarm Example
Register your receiver in Android Manifest.xml.
Send a broadcast message from your receiver class also update your DB inside your receiver.
Catch the same in your activity. You can use Custom Broadcast for this.
Now activate your alarm in activity.
Hope these steps will help you. Refer the example I have mentioned.
i'm writing an application that need to add some events to a calendar in android. For inserting i just used the following code:
public void onItemClick(AdapterView<?> adapter, View curview, int position, long id) {
WhoisEntry entry = this.adapter.getItem(position);
String domainName = entry.getDomainName();
Date expDate = entry.expirationDate;
Toast.makeText(getApplicationContext(), "Domain: " + domainName, Toast.LENGTH_SHORT).show();
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", entry.expirationDate);
intent.putExtra("allDay", false);
intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
intent.putExtra("title", "Expiration of " + entry.domainName);
startActivity(intent);
}
Now i'm wondering if is possible to get an id associated to that event, in that way after an event is inserted, and its id is saved into my application, the user can recall that event directly from inside the application.
Is it possible?
I extracted a list of columns used to store events into android calendar.
Here the list:
[0] "originalEvent" (id=830007842672)
[1] "availabilityStatus" (id=830007842752)
[2] "ownerAccount" (id=830007842840)
[3] "_sync_account_type" (id=830007842920)
[4] "visibility" (id=830007843008)
[5] "rrule" (id=830007843080)
[6] "lastDate" (id=830007843144)
[7] "hasAlarm" (id=830007843216)
[8] "guestsCanModify" (id=830007843288)
[9] "guestsCanSeeGuests" (id=830007843376)
[10] "exrule" (id=830007843464)
[11] "rdate" (id=830007843528)
[12] "transparency" (id=830007843592)
[13] "timezone" (id=830007843672)
[14] "selected" (id=830007843744)
[15] "dtstart" (id=830007843816)
[16] "title" (id=830007843888)
[17] "_sync_time" (id=830007843952)
[18] "_id" (id=830007844024)
[19] "hasAttendeeData" (id=830007844088)
[20] "_sync_id" (id=830007844176)
[21] "commentsUri" (id=830007844248)
[22] "description" (id=830007844328)
[23] "htmlUri" (id=830007844408)
[24] "_sync_account" (id=830007844480)
[25] "_sync_version" (id=830007844560)
[26] "hasExtendedProperties" (id=830007844640)
[27] "calendar_id" (id=830007844736)
Then if i want to get the new event id for my event:
public static long getNewEventId(ContentResolver cr, Uri cal_uri){
Uri local_uri = cal_uri;
if(cal_uri == null){
local_uri = Uri.parse(calendar_uri+"events");
}
Cursor cursor = cr.query(local_uri, new String [] {"MAX(_id) as max_id"}, null, null, "_id");
cursor.moveToFirst();
long max_val = cursor.getLong(cursor.getColumnIndex("max_id"));
return max_val+1;
}
ANd for insert event:
public void insertDomainEntry(Date exp_date, String name, long event_id){
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put("exp_date", exp_date.getTime()/1000);
values.put("event_id", event_id);
values.put("domainname", name);
db.insertOrThrow("domains_events", null, values);
}
That solution seems to work, even if probably this is not a very good solution.
EDIT 02/2015
The purpose of getNextEventId is to create a new Event Entry for the event table, here the code with the usage of this method:
#Override
public void onItemClick(AdapterView<?> adapter, View curview, int position,
long id) {
WhoisEntry entry = this.adapter.getItem(position);
long event_id = CalendarUtils.getNewEventId(getContentResolver(), null);
Toast.makeText(getApplicationContext(), "Domain: " + entry.getDomainName(),
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", entry.getExpiration().getTime());
intent.putExtra("_id", event_id);
intent.putExtra("allDay", false);
intent.putExtra("endTime", entry.getExpiration().getTime()+60*30);
intent.putExtra("title", "Expiration of " + entry.getDomainName());
startActivity(intent);
database.insertDomainEntry(entry.getExpiration(),
entry.getDomainName(), event_id);
}
Update 09/2015
As requested in the comment i add how to get the Calendar URI (it is basically where the calendar is stored, and the application try to guess it, searching in all known possible calendar paths)
public static String getCalendarUriBase(Activity act) {
String calendarUriBase = null;
Uri calendars = Uri.parse("content://calendar/calendars");
Cursor managedCursor = null;
try {
managedCursor = act.getContentResolver().query(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.getContentResolver().query(calendars,
null, null, null, null);
} catch (Exception e) {
}
if (managedCursor != null) {
calendarUriBase = "content://com.android.calendar/";
}
}
calendar_uri= calendarUriBase;
return calendarUriBase;
}
You can easily get event id after inserting an event.
long calID = 3;
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2012, 9, 14, 7, 30);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2012, 9, 14, 8, 45);
endMillis = endTime.getTimeInMillis();
...
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(Events.DTSTART, startMillis);
values.put(Events.DTEND, endMillis);
values.put(Events.TITLE, "Jazzercise");
values.put(Events.DESCRIPTION, "Group workout");
values.put(Events.CALENDAR_ID, calID);
values.put(Events.EVENT_TIMEZONE, "America/Los_Angeles");
Uri uri = cr.insert(Events.CONTENT_URI, values);
// get the event ID that is the last element in the Uri
long eventID = Long.parseLong(uri.getLastPathSegment());
//
// ... do something with event ID
//
//