I want perform a easy task in android, which when receiving a call the phonenumber will be matched to a number in my database. However I already read that sqlite and BroadcastReceiver are not that easy to combine. Below is the code:
public class incomingcall extends BroadcastReceiver{
String caller;
Cursor c;
#Override
public void onReceive(Context context, Intent intent) {
sqlitedatabase search = new sqlitedatabase(context);
Bundle bundle = intent.getExtras();
if(null == bundle)
return;
Log.i("IncomingCallReceiver",bundle.toString());
String state = bundle.getString(TelephonyManager.EXTRA_STATE);
Log.i("IncomingCallReceiver","State: "+ state);
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
String phonenumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.i("IncomingCallReceiver","Incomng Number: " + phonenumber);
try{
search.open();
c = search.callinfo(phonenumber);
if(c != null){
int iFN = c.getColumnIndex("firstname");
int iLN = c.getColumnIndex("lastname");
String FN = c.getString(iFN);
String LN = c.getString(iLN);
caller = FN + " " + LN;
}
else{
caller = "Unknown";
}
search.close();
}
catch (Exception e){
Toast.makeText(context, "error"+e.toString(), Toast.LENGTH_LONG).show();
}
Toast.makeText(context, caller, Toast.LENGTH_LONG).show();
}
}
}
I don't think the problem lies with the methode ".callinfo()", nonetheless I posted it below:
public Cursor callinfo(String l) {
// TODO Auto-generated method stub
String[] columns = new String[]{"phonenumber", "homenumber"};
Cursor c = db.query("mycontacts", columns, "phonenumber = " + l + " OR " + "homenumber = " + l, null, null, null, null);
return c;
}
The Catch returns : errorandroid.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1.
Thank you in advance for any help you can give me.
Cheers
Because the initial cursor position is -1, try to use if(c.moveToNext()) instead of if(c != null).
Related
Trying to delete the sent sms from app, when I have tried below code in Lenovo A6000(5.0.2) device
public static void deleteMessage(Context context, String phoneNo, String message) {
try {
Log.d(TAG, "deleteMessage: Deleting SMS from inbox");
Uri uriSms = Uri.parse("content://sms/");
Cursor c = context.getContentResolver().query(uriSms,
new String[]{"_id", "thread_id", "address",
"person", "date", "body"}, null, null, null);
Uri uri = null;
if (c != null && c.moveToFirst()) {
do {
long id = c.getLong(0);
long threadId = c.getLong(1);
String address = c.getString(2);
String body = c.getString(5);
int rowsDeleted = 0;
Log.d(TAG, "Deleting threads: " + threadId);
Log.d(TAG, "deleteMessage: id- "+ id + "" +
" threadId- " + threadId + "" +
" body- " + body + "" +
" rowsDeleted- " + rowsDeleted + "" +
" address- " + address);
if (address.equalsIgnoreCase(phoneNo)
&& body.equalsIgnoreCase(message)) {
ConversationQueryHandler handler = new ConversationQueryHandler(context.getContentResolver(), context);
synchronized (sDeletingThreadsLock) {
Log.v(TAG, "Conversation startDelete sDeletingThreads: " + sDeletingThreads);
if (sDeletingThreads) {
Log.e(TAG, "startDeleteAll already in the middle of a delete", new Exception());
}
sDeletingThreads = true;
uri = ContentUris.withAppendedId(Telephony.Threads.CONTENT_URI, threadId);
String selection = true ? null : "locked=0";
handler.setDeleteToken(0);
handler.startDelete(0, new Long(threadId), uri, selection, null);
}
}
} while (c.moveToNext());
}
} catch (Exception e) {
Log.d(TAG, "deleteMessage: Could not delete SMS from inbox: " + e.getMessage());
}
}
The ConversationQueryHandler sends 1 as a result in case of successful deletion of sms on to onDeletionComplete but this doesn't work in all the devices.
private static Object sDeletingThreadsLock = new Object();
private static boolean sDeletingThreads;
public static class ConversationQueryHandler extends AsyncQueryHandler {
private int mDeleteToken;
private Context mContext;
public ConversationQueryHandler(ContentResolver cr, Context context) {
super(cr);
mContext = context;
}
public void setDeleteToken(int token) {
mDeleteToken = token;
}
/**
* Always call this super method from your overridden onDeleteComplete function.
*/
#Override
protected void onDeleteComplete(int token, Object cookie, int result) {
Log.v(TAG, "Conversation onDeleteComplete token: " + token + " cookie- " + cookie + " result- " + result);
if (token == mDeleteToken) {
// release lock
synchronized (sDeletingThreadsLock) {
sDeletingThreads = false;
sDeletingThreadsLock.notifyAll();
}
}
}
}
I have tested this and found it is failed to delete the sms in all the below devices
Sony Xperia Z1(5.1.1)
Lenovo A200 device (5.1)
Samsung J210F (6.0.1)
As I mentioned earlier I am able to delete sms with the same code in
Lenovo A6000(5.0.2)
Is there a chance I am missing something here, or is this a right way of deleting the sent sms. Thank you for the help in advance.
I'm getting many time repeating data from Sqlite / Android database. I want to get recode without repeating in Android cursor.
Here's my query:
public Cursor getQuizQuiestion(String cat, String level, String questionNo) {
String QUERY_SELECT_QUIESTION = "SELECT * FROM " +TABLE_QUIESTION +" WHERE "+COL_CAT+ " = '" +cat+"' AND "
+COL_LEVEL+ " = " +level+" ORDER BY RANDOM() LIMIT 1";
Cursor cursor = db.rawQuery(QUERY_SELECT_QUIESTION, null);
return cursor;
}
I Have Simple Example if I Used In My Project I think MayBe IT's Help You;
public ArrayList<ModelRandomList> getRandomData(String city){
ArrayList<ModelRandomList> modelRandomListArrayListLists = new ArrayList<ModelRandomList>();
String queryRandomData ="SELECT DISTINCT * FROM "+TABLE_NAME+" WHERE "+COL_CITY + "=?"+ "Order BY RANDOM()";
Cursor cursor = db.rawQuery(queryRandomData,new String[] { String.valueOf(city) });
if(cursor.getCount() != 0){
while (cursor.moveToNext()){
/*mName =cursor.getString(0);
mCity = cursor.getString(1);
Log.d(TAG,mName +" City "+mCity );*/
ModelRandomList modelRandomList = new ModelRandomList();
modelRandomList.setUserName(cursor.getString(0));
modelRandomList.setUserCity(cursor.getString(1));
modelRandomListArrayListLists.add(modelRandomList);
}
Random random = new Random();
Collections.shuffle(modelRandomListArrayListLists,random);
}
Log.d(TAG, "Get Random Totla No of Recode Found "+cursor.getCount());
return modelRandomListArrayListLists;
}
and in Your Activity
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private SqlLiteDataBaseHelper sqlLiteDataBaseHelper;
private Button btGetRandomData;
private ArrayList<ModelRandomList> modelRandomLists;
private Button btSingleRandomData;
private int position = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
modelRandomLists = new ArrayList<ModelRandomList>();
sqlLiteDataBaseHelper = new SqlLiteDataBaseHelper(MainActivity.this);
try{
if(sqlLiteDataBaseHelper.checkDataBase()){
Log.e(TAG, "Data Base Already Exists");
}else {
sqlLiteDataBaseHelper.CopyDataBaseFromAsset();
}
sqlLiteDataBaseHelper.openDataBase();
try {
Log.e(TAG, "No Of Racode In DataBase " + sqlLiteDataBaseHelper.getDataCount());
}catch (Exception e){
e.printStackTrace();
}
}catch (Exception e){
e.printStackTrace();
}
init();
}
private void init() {
btGetRandomData = (Button)findViewById(R.id.btRandomData);
btSingleRandomData = (Button)findViewById(R.id.btSingleRandomData);
}
#Override
protected void onResume() {
super.onResume();
btGetRandomData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
modelRandomLists = sqlLiteDataBaseHelper.getRandomData("A");
for (ModelRandomList crt : modelRandomLists) {
Log.e(TAG, " " + crt.getUserName());
Log.e(TAG, " " + crt.getUserCity());
}
}
});
btSingleRandomData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try{
Log.d(TAG,modelRandomLists.get(position).getUserName());
position ++;
}catch (Exception e){
e.printStackTrace();
position =0;
}
}
});
}
}
You would have to keep a track of what questions have been seen before. What about this:
private List<Integer> seenQuestions = new ArrayList<>();
public Cursor getUseenQuizQuestion(String cat,String level,String questionNo) {
Cursor cursor = getQuizQuiestion(cat, level, questionNo);
while(cursor == null) { // This could be an infinite loop!!
cursor = getQuizQuiestion(cat, level, questionNo);
}
return cursor;
}
private Cursor getQuizQuiestion(String cat,String level,String questionNo){
String QUERY_SELECT_QUIESTION = "SELECT * FROM " +TABLE_QUIESTION +" WHERE "+COL_CAT+ " = '" +cat+"' AND "
+COL_LEVEL+ " = " +level+" ORDER BY RANDOM() LIMIT 1";
Cursor cursor = db.rawQuery(QUERY_SELECT_QUIESTION, null);
if(cursor.moveToFirst()) {
int resultId = cursor.getString(cursor.getColumnIndexOrThrow("_id"));
if(seenQuestions.contains(resultId)) {
cursor.close();
return null;
}
seenQuestions.add(resultId);
}
return cursor;
}
^ the code sample above has many flaws and could loop infinitely.
But the point is you need to keep track of what has been returned and query again if you have seen it.
Alternatively, you could allow your DB query to return all data and then use a Random value to select one of the items.
There is no way to create unique results if you are querying over and over again.
Since you are not using the questionNo parameter and return a cursor anyway: why not just remove the limit 1 and add the distinct clause to your statement?
If you do it that way, you can use your cursor to iterate over the unique questions:
String QUERY_SELECT_QUIESTION = "SELECT DISTINCT * FROM " +TABLE_QUIESTION +" WHERE "+COL_CAT+ " = '" +cat+"' AND "
+COL_LEVEL+ " = " +level+" ORDER BY RANDOM()";
Then you can iterate over the questions until you have none or whatever your conditions are:
Cursor cursor = db.rawQuery(QUERY_SELECT_QUIESTION, null);
try {
while (cursor.moveToNext()) {
//display question or copy them to a member or whatever
}
} finally {
cursor.close();
}
I have a sqlite database which I want to query and retrieve some values if they exist, in case they don't exist make a toast.
For this porpoise I have the next code which will call a function to create the cursor and in case this is not empty it will retrieve the information in the edittext otherwise it will make a toast:
private String lookforelementID() {
// TODO Auto-generated method stub
int ElementRequest=0;
ElementRequest = Integer.parseInt(eTElementNumb.getText().toString());
try{
database db = new database(this);
db.open();
String passedReg = getIntent().getStringExtra(MainActivity.ID_STUDY);
String returnedInfo1 = db.elementID(ElementRequest, passedReg, 0);
if (returnedInfo1.matches("")){
Toast.makeText(getApplicationContext(), "No Exist this Element", Toast.LENGTH_SHORT).show();
}else{
eTIDElement.setText(returnedInfo1);
String returnedInfo2 = db.elementID(ElementRequest, passedReg, 2);
eTElementCode.setText(returnedInfo2);
String returnedInfo3 = db.elementID(ElementRequest, passedReg, 3);
eTElementDecription.setText(returnedInfo3);
}
db.close();
}catch(ClassCastException e){
e.printStackTrace();
}
eTElementNumb.setText("");
return null;
}
The function with the cursor is as follows:
public String elementID(int elementRequest, String idStudy, int i) {
// TODO Auto-generated method stub
String[] columns = new String[]{ KEY_ROWELEMENTID, KEY_STUDYID, KEY_ELEMENTCODE, KEY_ELEMENTNAME};
Cursor c = ourDatabase.query(DATABASE_TABLEELEMENTS, columns, KEY_ELEMENTCODE + "=" + elementRequest + " AND " + KEY_STUDYID + "=" + idStudy, null, null, null, null);
String ElementInformation = null;
if(c != null){
c.moveToFirst();
ElementInformation = c.getString(i);
}
return ElementInformation;
}
I need to update a row of my database, but I get an error with SQLiteDatabase update method not resolved.
I think this is because of the custom class I had to create:
public class DBAccess {
public static String LOG_TAG = "DBAccess";
private SQLiteDatabase database;
private DBHelper dbHelper;
public DBAccess(Context context) {
dbHelper = new DBHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public Integer saveVehicle (Vehicle v){
Integer id = null;
if(this.database.isOpen() && !this.database.isReadOnly()){
String queryInsert = DBHelper.QueryAccessoAlDato.INSERT_VEHICLE;
try{
this.database.execSQL(queryInsert, new Object[]{v.getManufacturer(), v.getModel(), v.getPlate(),
v.getKmAmount(), v.getPrezzoGiorno(), v.getPrezzoSettimana(), v.getPrezzoMese(), v.getFuel(),
v.getGruppoMacchina()});
}catch (SQLException e){
Log.e(LOG_TAG, "Si รจ verificato un errore in inserimento " + e.getMessage());
e.printStackTrace();
return null;
}
Then this is the OnClick method of the main activity:
#Override
public void onClick(View v) {
Log.d(LOG_TAG, "Marca: "+this.etManufacturer.getText());
Log.d(LOG_TAG, "Modello: "+this.etModel.getText());
Log.d(LOG_TAG, "Targa: "+this.etPlate.getText());
Log.d(LOG_TAG, "Kilometraggio: "+this.etKmAmount.getText());
Log.d(LOG_TAG, "PrezzoGiorno: "+this.etPrezzoGiorno.getText());
Log.d(LOG_TAG, "PrezzoSettimana: "+this.etPrezzoSettimana.getText());
Log.d(LOG_TAG, "PrezzoMese: "+this.etPrezzoMese.getText());
Log.d(LOG_TAG, "Fuel: "+this.etFuel.getText());
Log.d(LOG_TAG, "Gruppo Macchina: "+this.etGruppoMacchina.getText());
if(!(this.etManufacturer.getText().length() == 0) &&
!(this.etModel.getText().length() == 0) &&
!(this.etPlate.getText().length() == 0) &&
!(this.etKmAmount.getText().length() == 0) &&
!(this.etPrezzoGiorno.getText().length() == 0) &&
!(this.etPrezzoSettimana.getText().length() == 0) &&
!(this.etPrezzoMese.getText().length() == 0) &&
!(this.etFuel.getText().length() == 0) &&
!(this.etGruppoMacchina.getText().length() == 0)
){
Vehicle myVehicle = null;
String manufacturer = this.etManufacturer.getText().toString();
String model = this.etModel.getText().toString();
String plate = this.etPlate.getText().toString();
long KmAmount = Long.parseLong(this.etKmAmount.getText().toString());
int prezzoGiorno = Integer.parseInt(etPrezzoGiorno.getText().toString());
int prezzoSettimana = Integer.parseInt(etPrezzoSettimana.getText().toString());
int prezzoMese = Integer.parseInt(etPrezzoMese.getText().toString());
String fuel = this.etFuel.getText().toString();
String gruppoMacchina = this.etGruppoMacchina.getText().toString();
myVehicle = new Vehicle (manufacturer, model, plate, KmAmount, prezzoGiorno,
prezzoSettimana, prezzoMese,fuel, gruppoMacchina);
Log.d(LOG_TAG, "Hai aggiunto " + myVehicle + "con id" + vehicleID);
DBAccess dba = new DBAccess(this.getApplicationContext());
dba.open();
Log.d(LOG_TAG, "Avvio lettura da db");
if (this.getIntent().hasExtra(Const.ID_VEHICLE)) {
//http://stackoverflow.com/questions/9798473/sqlite-in-android-how-to-update-a-specific-row
ContentValues values= new ContentValues();
values.put(DBHelper.VEHICLE_MANUFACTORER_COLUMN, manufacturer);
values.put(DBHelper.VEHICLE_MODEL_COLUMN, model);
//values.put(DBHelper.KEY_PEDLOCATION, ped_location);
// values.put(DBHelper.KEY_PEDEMAIL, ped_emailid);
// etc etc
dba.update(DBHelper.TABLE_VEHICLE, values, DBHelper.VEHICLE_ID_COLUMN + "=" + vehicleID, null);
finish();
}else{
dba.saveVehicle(myVehicle);
Log.d(LOG_TAG, "Ho salvato" + myVehicle);
dba.close();
finish();
So i get the error at line :
dba.update(DBHelper.TABLE_VEHICLE, values, DBHelper.VEHICLE_ID_COLUMN + "=" + vehicleID, null);
How can I resolve this? Thank you!
First your DBAccess class should extend SQLiteOpenHelper class.
The Update method declaration is as below, the last parameter should be a String array,
public int update (String table, ContentValues values, String whereClause, String[] whereArgs)
Change your code to as shown below:
dba.update(DBHelper.TABLE_VEHICLE, values, DBHelper.VEHICLE_ID_COLUMN + "=?", new String[]{String.valueOf(vehicleID));
I write a simple application to get phone number in Contacts. However, the phone number return "null".
Here is my code:
private void queryContactPhoneNumber() {
// TODO Auto-generated method stub
String[] cols = new String[] {People.NAME, People.NUMBER};
Uri myContacts = People.CONTENT_URI;
Cursor mqCur = managedQuery(myContacts, cols, null, null, null);
if(mqCur.moveToFirst())
{
String myname = null;
String mynumber = null;
do
{
myname = mqCur.getString(mqCur.getColumnIndex(People.NAME));
mynumber = mqCur.getString(mqCur.getColumnIndex(People.NUMBER));
Toast.makeText(this, myname + " " + mynumber, Toast.LENGTH_SHORT).show();
}
while(mqCur.moveToNext());
}
}
Try this,
Uri myContacts = ContactsContract.CommonDataKinds.Phone.CONTENT_URI ;//People.CONTENT_URI;
Cursor mqCur = managedQuery(myContacts, null, null, null, null);
if(mqCur.moveToFirst())
{
String myname = null;
String mynumber = null;
do
{
myname = mqCur.getString(mqCur.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
mynumber = mqCur.getString(mqCur
.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(this, myname + " " + mynumber, Toast.LENGTH_SHORT).show();
}
while(mqCur.moveToNext());
}
I think this will help you.