Can any one please tell me how to delete call log from android phone,
I am using following line of code
getActivity().getContentResolver().delete(CallLog.Calls.CONTENT_URI, null, null);
but, this is not working for me I want to delete whole call log history.
Following code work for me..
private void deleteNumber() {
try {
String strNumberOne[] = { "00577698160" };
Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, CallLog.Calls.NUMBER + " = ? ", strNumberOne, "");
boolean bol = cursor.moveToFirst();
if (bol) {
do {
int idOfRowToDelete = cursor.getInt(cursor.getColumnIndex(CallLog.Calls._ID));
getContentResolver().delete(Uri.withAppendedPath(CallLog.Calls.CONTENT_URI, String.valueOf(idOfRowToDelete)), "", null);
} while (cursor.moveToNext());
}
} catch (Exception ex) {
System.out.print("Exception here ");
}
}
Edit:
following code is also work for me In API 23
make sure u have following permission in manifast.xml:
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.WRITE_CALL_LOG" />
for Deleting Calllogs for particular number try this way:
public void DeleteCallLogByNumber(String number) {
String queryString="NUMBER="+number;
this.getContentResolver().delete(CallLog.Calls.CONTENT_URI,queryString,null);
}
}
Yeah this is working perfectly for me in android Lollipop :)
private void deleteCall(String number) {
Uri CALLLOG_URI = Uri.parse("content://call_log/calls");
getActivity().getContentResolver().delete(CALLLOG_URI, CallLog.Calls.NUMBER +"=?",new String[]{number});
Toast.makeText(getActivity(), "Call Log Deleted", Toast.LENGTH_SHORT).show();
}
This code is working for me.I am deleting call log by its log id please check below code for it.
private void deleteCall(String idd) {
context.getContentResolver().delete(CallLog.Calls.CONTENT_URI, CallLog.Calls._ID + " = ? ",new String[]{String.valueOf(idd)});
}
Related
I use this to call a number when button is pressed:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:79950695"));
startActivity(callIntent);
Is there a way to hide the number ??
What I want to get is a pop with new call but showing only the name without the number ?
Would I need to create some custom Outgoing Call Broadcaster ?
It is not possible to handle In Call UI.
you can save that contact with some name once user clicks on call button and display the name, once done with call you might delete that contact.
You need to handle the settings to display only name but not number in call.
You can't hide. Instead delete the number.
Use this method
private void deleteNumber() {
try {
String strNumberOne[] = { "79950695" };
Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, CallLog.Calls.NUMBER + " = ? ", strNumberOne, "");
boolean bol = cursor.moveToFirst();
if (bol) {
do {
int idOfRowToDelete = cursor.getInt(cursor.getColumnIndex(CallLog.Calls._ID));
getContentResolver().delete(Uri.withAppendedPath(CallLog.Calls.CONTENT_URI, String.valueOf(idOfRowToDelete)), "", null);
} while (cursor.moveToNext());
}
} catch (Exception ex) {
System.out.print("Exception ");
}
}
I have created a table and trying to fetch data from it using a cursor as follow:
public Cursor getcontent() {
Cursor d = database.query(DatabaseHandler.Table_Name2,allColumns,selection, null, null,null,null);
return d;
}
Cursor r = X.getcontent();
if (r.getCount() > 0) {
r.moveToFirst();
do {
String id = r.getString(r.getColumnIndex("content_id"));
al.add(id);
MainActivity.tt1.append("\n");
MainActivity.tt1.append(id);
} while (r.moveToNext()==true);
r.close();
} else {
Log.i("TAG"," No value found");
}
}
I am showing the result in the TextView to see what data it is fetched. My problem is when I run this code sometimes it shows the data in the TextView, whatever it has fetched and sometimes it doesn't. Its a 50:50 ratio, according to me it should show fetched values every time as data is fetched every time I don't know what is wrong here, can someone tell me what's the issue here?
Check Whether Cursor you are getting is Null or not . and if yes then What is the Count of Cursor. you can Do it by Below Way.
Cursor r = X.getcontent();
if ((r != null) && (r.getCount() > 0)) {
r.moveToFirst();
do {
String id = r.getString(r.getColumnIndex("content_id"));
al.add(id);
MainActivity.tt1.append("\n");
MainActivity.tt1.append(id);
} while (r.moveToNext());
r.close();
} else {
Log.i("TAG"," No value found inside Cursor");
}
try like this
Cursor r = X.getcontent();
try {
if (r.moveToFirst()) {
do {
String id = r.getString(r.getColumnIndex("content_id"));
al.add(id);
MainActivity.tt1.append("\n");
MainActivity.tt1.append(id);
} while (r.moveToNext());
}
} finally {
if(r!=null) {
r.close();
}
}
i am looking to make game of questions and answers.i had taken a textview and 4 radio group buttons.and i am fetching data from the external data file from the assets folder.when i installs the app into the emulater it works fine.when i reopens the app in the emulater it is just showing question not showing any text in the radio buttons.here is my code in the data base file
public String makeatext(String My_database_table,int i) {
SQLiteDatabase myDB = getDatabase();
String results = new String();
try {
String firstColumn = "questions";
// final String KEY_ROWID = "id";
// Cursor c = myDB.rawQuery("SELECT questions FROM " +
// My_database_table+ ";",null);
Cursor c = myDB.query(true, My_database_table,
new String[] { firstColumn },null, null, null, null, null,
null);
int iquestion = c.getColumnIndex(firstColumn);
if(c.moveToPosition(i)){
results = c.getString(iquestion)+"\n";
}
//while (c.moveToPosition(1)) {
//String firstName = c.getString(iquestion);
//results =(" "+ firstName + " ");
//}
return results;
} catch (Exception e) {
Log.e("ERROR","ERROR in Make test file :"+e.toString());
e.printStackTrace();
// TODO: handle exception
}
return results;
}
and in the Activity file i am just calling it as
String shoow = myDb.makeatext("question", Qno);
showQuestion.setText(shoow);
and on the top of the oncreate methode i initilized the data base asprivate final DataBaseHelper myDb = new DataBaseHelper(this);
can any one say me why this is happenig.do i need to write the for loop in the activity file also or shall i take a cursor in the activity class .
plz help me out
thanks in advance
for radio buttons the code in the database file is as follows as i have 4 buttons the code for 4 buttons will be as same as this
public String makeExtra1(String My_database_table ,int positions) {
String results = new String();
try {
String secondColumn = "Extra1";
Cursor c = myDataBase.query(true, My_database_table,
new String[] { secondColumn }, null, null, null, null, null,
null);
int iExtra1 = c.getColumnIndex(secondColumn);
if(c.moveToPosition(positions)){
results = results+c.getString(iExtra1)+"\n";
}
return results;
} catch (Exception e) {
Log.e("ERROR","ERROR in Make test file :"+e.toString());
e.printStackTrace();
// TODO: handle exception
}
return results;}
and in the Activity file
String showextra1 = myDb.makeExtra1("question", Qno);
r0.setText(showextra1);
i repeted this thing for 4 times as changing the makeExtra2,3,4 and in the assinged to r1,r2,r3 as above.
You should call Cursor.close() after reading data from it. Better to do it finally{} block.
If you want to raise only one answer per query - fill where param of myDataBase.query().
The unreachable code error is because you are writing finally{} block after return results;
Move this line after the finally{} block, eclipse will not give you any error.
Also do use myDB.close(); in this finally block.
I have written a simple code for Database updatation, but it is sometime updating and sometimes not... i have written LOG for conformation but the log is giving correct output. Here is what i am trying :=
public void updateDownloadedAssetNumberOfStartingBytesEncrypted(int id, int startingBytesEncrypted)
{
SQLiteDatabase database = null;
int numOfRowsUpdated = 0;
try
{
database = getWritableDatabase();
ContentValues values = new ContentValues();
values.put("StartingBytesEncrypted", startingBytesEncrypted);
if(database.isOpen())
{
Log.v("updating in db","doc id - "+id + " encrypted bytes - "+startingBytesEncrypted);
numOfRowsUpdated = database.update("_assets", values, "Id = "+id, null);
}
else
{
Log.v("Database","the database is not open thus starting encrypted bytes were not updated");
}
Log.v("muber of rows updated - ",""+numOfRowsUpdated);
}
catch(Exception ex)
{
}
finally
{
if(database != null)
{
database.close();
}
}
}
What is the problem?? Any help would be Appreciable.
Ya i got ur code...
Finally i resolved the issue.... actually it is beacuse of threading....
the thread creating the row was executed later and that updating the row was executed first
i have resolved it.Have fun :)
This happened due to connection of database is not open. Pls keep ex.printstacktrace(); in catch statement.
is there anyway to remove a missed call notification by code? And somehow remove the last missed call from call history?
yes, it is possible.Try this:
Uri UriCalls = Uri.parse("content://call_log/calls");
Cursor cursor = getApplicationContext().getContentResolver().query(UriCalls, null, null, null, null);
Reading call log entries...
if(cursor.getCount() > 0){
cursor.moveToFirst();
while(!cursor.isAfterLast()){
String number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER)); // for number
String name = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));// for name
String duration = cursor.getString(cursor.getColumnIndex(CallLog.Calls.DURATION));// for duration
int type = Integer.parseInt(cursor.getString(cursor.getColumnIndex(CallLog.Calls.TYPE)));// for call type, Incoming or out going
cursor.moveToNext();
}
}
Deleting entry in call log...
String queryString= "NUMBER='" + number + "'";
if (cursor.getCount() > 0){
getApplicationContext().getContentResolver().delete(UriCalls, queryString, null);
}
Permission:
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
Note: Please refer this doc over call log for more clearity.
Using the above code you can get the desired result.
Refer to this
You can clear your missed call by calling cancel(ID) or calcelAll() to clear your notifications bar.
For the part where you want to remove the last call from the log you need to move the method that is deleting the entry into a class which is a subclass of the Thread class. This allows you to then set it to sleep for a short period to allow Android to actually write to the call log BEFORE you you run the delete query. I had the same problem, but manage to resolve it with the code below:
public class DelayClearCallLog extends Thread {
public Context context;
public String phoneNumber;
public DelayClearCallLog(Context ctx, String pNumber){
context = ctx;
phoneNumber = pNumber;
}
public void run() {
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
clearCallLog(context, phoneNumber);
}
public void clearCallLog(Context context, String phoneNumber) {
// implement delete query here
}
}
Then call the the method as follows:
DelayClearCallLog DelayClear = new DelayClearCallLog(context, phoneNumber);
DelayClear.start();