Good afternoon. There is a task to develop a client for android news site. For notification of new posts to be used gcm.
Estimated algorithm:
1. Run the application, the database is empty, make a request to the api get the last 20 records, id 1000 to 980, write to the database.
3. Listview is scrolled to the last record, make a request for records whose id is less than 980, we record from 979 to 959 write to the database.
3. Comes gcm message or clicked update, make a request to the api, get the records whose id> 1000, we record with id 1001 to 1021 write to the database.
The question arises - how to behave in a situation where we assume the device was offline for a week, during which added another 1000 records.
The device connects to the network, gets send-to-sync message, makes a request to the api to get records whose id> 1021, in response to the 1000 records to come.
Intuition suggests that load all 1000 records are not correct, you need to download and say the last 20, but then it turns out that the database will be news for today, last week they will not, and then go for the news before last week
And question number 2 - how to clean old records, to avoid infinite growth of the DB?
What about this: make another table that have only 20 records and when a news comes, it stores it on the first record and deletes last record. Of course records 2-19 iterate.
When you get news on device, get them from this table. If you want to get older news, get the last id from this table and get next 20 news from table that stores all of the records.
---------- Post added at 10:29 AM ---------- Previous post was at 10:20 AM ----------
Answering question 2:
The best solution for me is to make model class News . It makes everything easier with DB. If you have this, you can do something like this:
public void deleteAllNews(){
String selectQuery = "SELECT * FROM " + TABLE_NEWS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
// looping through all rows
if (c.moveToFirst()) {
do {
db.delete(TABLE_NEWS, KEY_ID + " = ?", new String[] { String.valueOf(c.getInt(c.getColumnIndex(KEY_ID)) });
} while (c.moveToNext());
}
db.close();
c.close();
}
Related
Hi
I want to write an app that goes through all my stored SMS, lets me pick one and then would display the SMSC address which was involved in sending said SMS to my phone.
Question: Is this SMSC address stored somewhere? And just to be totally clear: I am not asking for the SMSC address of my operator which would be used for sending SMS from my phone to other phones, okay?
I tried to find something in the deprecated SMS content provider. But either I did not look hard enough or it's simply not there.
Has anybody got some tip for me regarding this?
thanks a lot!
gsm-man
Try the following code:
ContentResolver cr = context.getContentResolver();
final Cursor c = cr.query(Uri.parse("content://sms/inbox"), new String[]{"_id", "address", "date", "read", "body", "service_center"}, null, null, null);
I'd like to get a list of the email addresses associated with all the online gtalk contacts. I'm using it to generate a pick list to launch a gtalk chat session with that person from my app.
Code:
private Cursor getGtalkEmails()
{
ContentResolver cr = getContentResolver();
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[] { ContactsContract.Data._ID,
ContactsContract.Data.CONTACT_PRESENCE,
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Im.PROTOCOL,
ContactsContract.CommonDataKinds.Im.DATA };
String selection = ContactsContract.Data.CONTACT_PRESENCE+"!='0' and " + ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Data.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
return cr.query(uri, projection, selection, selectionArgs, sortOrder);
Unfortunately, this only returns a single contact... And I have 50 people online right now.
So I dumped the entire table and it looks like out of every contact on my phone, only one has a vnd.android.cursor.item/im entry.
So is there an alternative query I could use to filter these out? I can't just use email, because a lot of my contacts have more than one email. If they do, it could pick the wrong one and when I fire the intent to launch a gtalk session it would open up the "add contact" dialog instead of the chat activity.
Thoughts?
SMS2PC API
Introduction
It is with great pleasure that today I am releasing the first public beta of SMS2PC API which is designed to allow developers and hackers to quickly and easily send and receive messages via an Android (and soon to be BlackBerry and WinPhone) device as well as loading contact information and address credentials.
The API is designed in an async like manner allowing you to create a request, forget about it and then easily handle any return to the original request while not having to worry about threads, connections and protocol management. As an example, you can send a message with only a few lines of code and then handle delivery notifications and send notifications when they occur by providing a callback that is fired when a the message's status has changed.
You are free to use the API as you wish. You will be able to do things like send messages from a server to notify of events on the server, you could make an application than can receive commands such as ‘shutdown_pc’ via SMS and then shut down the current PC, run in on embedded systems, create alarm system that notify via SMS of intruder etc!
Features
Current
Send message
Handle Delivery Notifications
Handle Send Notifications
Load Contact Details
Receive incoming message from device.
Loading message list for a contact
Upcoming
MMS Support
Inbox Support
Load advanced contact details
Example of Handling Incoming Messages
As an example to highlight how simple it is to receive new messages when the arrive on device the actual code used is provided below.
Code:
class MyHandler implements IMessageReceiveHandler
{
@Override
public void messageReceived(Message message)
{
System.out.println("New message received: " + message.getBody());
}
}
// Create instance of your handler and provide it to the API
MyHandler myMessageHandler = new MyHandler();
sms2pc.setMessageReceiveHandler(myMessageHandler);
Requirements
Current the API is Java based (requiring JRE). We have nearly finished the C# version and the mobile apps for BlackBerry and WinPhone will be available shortly.
Terms of Use
Please be aware that although you are totally free to use the API in any software as you choose (commerial or personal) , the Android/BlackBerry & WinPhone app is currently paid for at around £1.99. Also, if you use SMS2PC API in your software you must provide a mention of this fact in your about/info dialog or help if console application. This may be updated in the future although the price will never increase. If you are an XDA user you can download SMS2PC API for free for Android but must not re-distribute the apk.
Conclusion
Thank you for reading. If you have any questions feel free to email me at [email protected] or PM me.
Can't wait until the c# version is done ^_^
Any chance you plan on fixing the desktop app to work with Lion or Mountain Lion?
It's only $3, but you've got it (literally tried to refund moments later and couldn't), and the app flat out does not work on Mac OSX.
Great work, can you show an example on how to send sms?
i have this code for adding event:
Code:
Intent calendarIntent = new Intent(Intent.ACTION_INSERT, Events.CONTENT_URI);
Calendar beginTime = Calendar.getInstance();
beginTime.set(2013,04,14,21,00,00);
Calendar endTime = Calendar.getInstance();
endTime.set(2013,04,14,21,10,00);
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("title", "Some title");
intent.putExtra("description", "Some description");
intent.putExtra("beginTime", beginTime.getTimeInMillis());
intent.putExtra("endTime", endTime.getTimeInMillis());
startActivity(intent);
but how to insert event without open the calendar ?
thanks
You'll need to use the CalendarContract API, and add a certain permission. This is only possible in 4.0 and up, however.
thank
thanks for the help,
can i get any sample code that works ?
i try some - but no one works for me
Regards,
Hello everyone,
I've queried Android's CallLog.Calls and populated an ArrayList with my call model object however when I display these objects in a RecyclerView, certain phone-numbers appear multiple times, once for each call entry that was made from or to that number.
I'd like to group the calls from the same number together.
Is there a built-in way in Android to group these call entries so that I can display them as a single entry in my call log RecyclerView?
I tried to add "GROUP BY" to the query but contentResolver.query() does not seem to accept it in any of its parameters.
Thanks!
You can remove the duplications after you get the data using a HashSet, for example:
ArrayList<String> items = getItems();
HashSet<String> hashset = new HashSet<>();
hashset.addAll(items);
items = new ArrayList<String>(hashset);