I am new to Android development. I want to make a call to a number but I don't want to store the number in my Call log. How can I delete the number from call log after the call ends?
First you have to set up a broadcast receiver to detect the phone state. Here's the exact same question: Stackoverflow - Intent to be fired when a call ends?
And now for deleting the call log entry, here is the first link on google: Call log deletion in Android
In the example he deletes all call entry for a specific number, but you can change the query to delete the entry for a specific call log id.
Hope this helps.
Cheers
Im using 4.2.2 anyway i had to modify the aftab's code as it was not working for me. It could be a asych issue giving what i was trying to do is update the call log right after an incoming call is ended. I think i have to give O/S enough time to update the table before i delete the entry or it wont exist :
private void deleteNumber(String phoneNumber) {
try {
Thread.sleep(4000);
String strNumberOne[] = { phoneNumber };
Cursor cursor = context.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));
context.getContentResolver().delete(
CallLog.Calls.CONTENT_URI,
CallLog.Calls._ID + "= ? ",
new String[] { String.valueOf(idOfRowToDelete) });
} while (cursor.moveToNext());
}
} catch (Exception ex) {
Log.v("deleteNumber",
"Exception, unable to remove # from call log: "
+ ex.toString());
}
}
and to call the function i run on another thread (since im sleeping) :
new Thread() {
public void run() {
deleteNumber(incomingNumber);
}
}.start();
after adding the sleep it seems to work when trying to delete right after a call is ended.
UPDATE: after last comment realized we can set up a contentobserver on the android provider call log uri:
public class BlockerContentObserver extends ContentObserver{
private Context context;
private String phoneNumber;
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public BlockerContentObserver(Handler handler,Context context) {
super(handler);
this.context=context;
}
#Override
public boolean deliverSelfNotifications() {
return true;
}
#Override
public void onChange(boolean selfChange) {
// TODO Auto-generated method stub
super.onChange(selfChange);
Log.v(Consts.TAG,"has call log changed:"+selfChange);
deleteNumber(phoneNumber);
}
private void deleteNumber(String phoneNumber) {
try {
String strNumberOne[] = { phoneNumber };
Cursor cursor = context.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));
context.getContentResolver().delete(
CallLog.Calls.CONTENT_URI,
CallLog.Calls._ID + "= ? ",
new String[] { String.valueOf(idOfRowToDelete) });
} while (cursor.moveToNext());
}
} catch (Exception ex) {
Log.v(Consts.TAG,
"Exception, unable to remove # from call log: "
+ ex.toString());
}
}
}
Now we register to listen to changes in the call log DB using this:
mContentObserver = new BlockerContentObserver(new Handler(), context);
then we make a method to either register for events or unreigster:
/*handles the registration of our content observer used for monitoring the call log*/
private void RegisterContentObserver(boolean shouldRegister){
if(shouldRegister)
{
context.getContentResolver().registerContentObserver(
android.provider.CallLog.Calls.CONTENT_URI,
true,
mContentObserver);
}
else {
try {
context.getContentResolver().unregisterContentObserver(mContentObserver);
} catch (IllegalStateException ise) {
// Do Nothing. Observer has already been unregistered.
}
}
}
I just try this method its working great on my HTC with 4.0.3
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 ");
}
}
modified version of earlier answsers. you don't really need a while loop. Also you don't need Thread.sleep(4000), register a ContentObserver for CallLog.Calls.CONTENT_URI and call the following method in onChange. but just before calling that make sure you unregister that ContentObserver
public static void deleteLastCallLog(Context context, String phoneNumber) {
try {
//Thread.sleep(4000);
String strNumberOne[] = { phoneNumber };
Cursor cursor = context.getContentResolver().query(
CallLog.Calls.CONTENT_URI, null,
CallLog.Calls.NUMBER + " = ? ", strNumberOne, CallLog.Calls.DATE + " DESC");
if (cursor.moveToFirst()) {
int idOfRowToDelete = cursor.getInt(cursor.getColumnIndex(CallLog.Calls._ID));
int foo = context.getContentResolver().delete(
CallLog.Calls.CONTENT_URI,
CallLog.Calls._ID + " = ? ",
new String[] { String.valueOf(idOfRowToDelete) });
}
} catch (Exception ex) {
Log.v("deleteNumber",
"Exception, unable to remove # from call log: "
+ ex.toString());
}
}
Related
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 want to get the Contact name of the selected contact in android while working with default contacts of phone.
I tried like this but it show all the contact names in the phone but i want only selected contact name
Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phNumber));
// query time
Cursor cursor = this.getContentResolver().query(contactUri, projection, null, null, null);
if(cursor != null) {
if (cursor.moveToFirst()) {
name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
Log.v("sai", "Started uploadcontactphoto: Contact Found # " + phNumber);
Log.v("sai", "Started uploadcontactphoto: Contact name = " + name);
} else {
Log.v("sai", "Contact Not Found # " + phNumber);
}
cursor.close();
}
So, you're using the right approach, but you aren't telling Android what specific contact to look up.
You don't state what a "selected contact" is, but I assume you should know its phone number or some such by which to actually query the contact list.
According to documentation, you're just not using the selection parameters.
You can write selection param just as you would an SQL query's WHERE statement. As in
ContactsContract.PhoneLookup + " = 123456789"
See this answer for a more detailed explanation.
Here is the code in which i am storing the email address and contact person name in arraylist
public ArrayList<ContactVo> getNameEmailDetails() {
ArrayList<ContactVo> m_arrList = new ArrayList<ContactVo>();
ArrayList<String> emlRecs = new ArrayList<String>();
HashSet<String> emlRecsHS = new HashSet<String>();
ContentResolver cr = m_context.getContentResolver();
String[] PROJECTION = new String[] { ContactsContract.RawContacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_ID,
ContactsContract.CommonDataKinds.Email.DATA,
ContactsContract.CommonDataKinds.Photo.CONTACT_ID };
String order = "CASE WHEN " + ContactsContract.Contacts.DISPLAY_NAME
+ " NOT LIKE '%#%' THEN 1 ELSE 2 END, "
+ ContactsContract.Contacts.DISPLAY_NAME + ", "
+ ContactsContract.CommonDataKinds.Email.DATA
+ " COLLATE NOCASE";
String filter = ContactsContract.CommonDataKinds.Email.DATA
+ " NOT LIKE ''";
Cursor cur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, PROJECTION,
filter, null, order);
ContactVo m_vo;
if (cur.moveToFirst()) {
do {
m_vo = new ContactVo();
// names comes in hand sometimes
String name = cur.getString(1);
String emlAddr = cur.getString(3);
System.err.println("Value Is---------->" + cur.getString(2)
+ "Name--" + name + "Email---" + emlAddr);
m_vo.setM_sName(name);
m_vo.setM_sEmail(emlAddr);
// keep unique only
if (emlRecsHS.add(emlAddr.toLowerCase())) {
emlRecs.add(emlAddr);
}
m_arrList.add(m_vo);
} while (cur.moveToNext());
}
cur.close();
return m_arrList;
}
EDITED:
Write the below code in your onItemClick event of your listview where you are showing all the contact list.
m_lvList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(m_context, m_arrContact.get(arg2).getM_sEmail(), Toast.LENGTH_LONG).show();
Intent in = new Intent(m_context, Test.class);
in.putExtra("pos", arg2);
in.putParcelableArrayListExtra("parcel", m_arrContact);
startActivity(in);
}
});
Make your vo parcelable to pass the details into another activity and access it.
ContactVo
public class ContactVo implements Parcelable {
private String m_sName, m_sEmail;
public ContactVo() {
}
public ContactVo(Parcel in) {
readFromParcel(in);
}
public String getM_sName() {
return m_sName;
}
public void setM_sName(String m_sName) {
this.m_sName = m_sName;
}
public String getM_sEmail() {
return m_sEmail;
}
public void setM_sEmail(String m_sEmail) {
this.m_sEmail = m_sEmail;
}
#Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(m_sName);
dest.writeString(m_sEmail);
}
public void readFromParcel(Parcel p_in) {
m_sName = p_in.readString();
m_sEmail = p_in.readString();
}
public static final Parcelable.Creator<ContactVo> CREATOR = new Parcelable.Creator<ContactVo>() {
public ContactVo createFromParcel(Parcel s) {
return new ContactVo(s);
}
public ContactVo[] newArray(int size) {
return new ContactVo[size];
}
};
}
In your another activity write as below:
public class Test extends Activity {
ArrayList<ContactVo> m_arr;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
m_arr = getIntent().getParcelableArrayListExtra("parcel");
int pos = getIntent().getIntExtra("pos", 0);
System.out.println("Detailsss------>" + m_arr.get(pos).getM_sEmail()
+ m_arr.get(pos).getM_sName());
}
}
You can add as many details as much you want into the ArrayList<ContactVo> and pass it into another activity.
To get other contact details Check out HERE
I have a activity which is brought in front whenever there are any outgoing call. I need the outgoing call duration in real time and show it in my activity textview.
Is this possible to get the current duration if call is in offhook?
This can be done. You just have to Spy on Android Call Log Content Provider. To use a content provider, you just need to call the getContentResolver() method. Once you have a content resolver object, you can query it using SQL or any of the built in query functions. For example, in my Android App, I use a piece of code that looks like this:
String[] strFields = {
android.provider.CallLog.Calls.NUMBER,
android.provider.CallLog.Calls.TYPE,
android.provider.CallLog.Calls.CACHED_NAME,
android.provider.CallLog.Calls.CACHED_NUMBER_TYPE,
android.provider.CallLog.Calls.DURATION
};
String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
Cursor mCallCursor = getContentResolver().query(
android.provider.CallLog.Calls.CONTENT_URI,
strFields,
null,
null,
strOrder
);
if (callType == OUTGOING_CALL_END || callType == INCOMING_CALL_END) {
Cursor cursorCallLogs = getRecentCallLogs(context.getContentResolver());
if (cursorCallLogs != null) {
cursorCallLogs.moveToLast();
do {
try {
String callNumber = cursorCallLogs.getString(cursorCallLogs.getColumnIndex("number"));
callNumber = utilS.getPhoneNumber(callNumber);
utilS.log("Phone NUmber", callNumber);
duration = (cursorCallLogs.getInt(cursorCallLogs.getColumnIndex("duration")) * 1000);
utilS.log("durationCall", "" + duration);
} catch (Exception e) {
e.printStackTrace();
}
}
while (cursorCallLogs.moveToPrevious());
}
}
public static Cursor getRecentCallLogs(ContentResolver cr) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
return null;
} else {
return cr.query(CallLog.Calls.CONTENT_URI, null, null, null, android.provider.CallLog.Calls.DATE + " DESC limit 1");
}
}
I'm trying to extract the Contacts when the contact table is changed. It was working fine but now doesn't work. It only works a few times. I'm using ContentObserver to get the event.
Here's the code to register the ContentObserver. I'm registering it on a Service to be constantly listening.
public void onCreate() {
super.onCreate();
context = this;
db = new Database(context);
Log.i("**********", "Started Service!");
Contacts contacts = new Contacts(context);
context.getContentResolver().registerContentObserver (ContactsContract.CommonDataKinds.Phone.CONTENT_URI, true, contacts);
}
And here's the code when I extract the contacts name and this stuf:
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.i("********","*****");
db = new Database(co);
db.open();
if (db.Preferences(3) && new CheckConexion().isOnline(co)) {
Cursor phones = co.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
null, null, null);
while (phones.moveToNext()) {
try {
name = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
JSONObject jobject = db.select_contact(name, phoneNumber);
String type1 = jobject.getString("type");
if (type1.equals("upload")) {
if (!(name.equals("") && phoneNumber.equals(""))) {
JSONObject jobject2 = new JSONObject();
jobject2.put("name", name);
jobject2.put("phone", phoneNumber);
String value_json = String.valueOf(jobject2);
upload(value_json, co);
Log.i("********", "Upload on the db" + name + " "
+ phoneNumber);
}
} else {
return;
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
db.close();
} else {
db.close();
}
}
Oncreate() is called only once for a service when it is started for the first time. If you are starting the service again and again then onStartCommand() will be called directly. Therefore I feel its the observer registration is not being retained and is getting lost.
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).