Weekend 1 (An Android app )

Hello guys, I know it has being a long time since i wrote a blogpost or a story (more than 3 years). But i am trying to start writing about the stuff i am doing these days.

Today’s story is about a small app i made last weekend.

First of all, every app has a “why”. And for this case it’s that in order to receive the announcements from the Computer Department at Ege University you always have to check the website ( bilmuh.ege.edu.tr ) and see if there is anything new there. Which is a thing i never remembered to do and I always end up missing something there. So the app is made to solve this for me (and maybe other students who suffer the same thing ).

The general idea of the app is like the following. First it parses the html page of the website (There is no api to do this in an appropriate way). Jsoup library made this really easy for us . All what i had to do is :

Elements elements = document.select(".article");
for (Element element : elements) {
String title = element.select(".node-title").text();
String content = element.select(".field-item > p").html();
String date = element.select(".date-display-single").text();
}

The code above is doing the following :

  1. Loop through all the elements with an “article” class.
  2. set the title to be the text value of the element with a class “node-title”.
  3. set the content to be the direct child “p” of an element with a “field-item” class name.
  4. set the date to be the text of the element with a class “date-display-single”.

Populate these items to your RecyclerView.Adapter and you have your announcements ready.

The second part of the app is for sending me a notification whenever there is a new announcement.

To do this, i made a service that run every 10 minutes and check the first announcement at the website (the same code above just without a loop) and compare it with a SharedReference i stored it in my last update for the RecyclerView.

SharedPreferences sharedPref = getApplicationContext().getSharedPreferences(getApplicationContext().getResources().getString(R.string.last_post), Context.MODE_PRIVATE);

String last_stored_title = sharedPref.getString(getApplicationContext().getResources().getString(R.string.last_post), "");

if (!last_stored_title.equals(title) && !title.equals("")) {
sharedPref.edit()
.putString(getApplicationContext().getResources().getString(R.string.last_post), last_title)
.apply();
return true;
}

And if thee result is true, send a notification to the user with the new announcement

Intent resultIntent = new Intent(getApplicationContext(), PostActivity.class);
resultIntent.putExtra("title", mTitle);
resultIntent.putExtra("date", mDate);
resultIntent.putExtra("content", mContent);

PendingIntent pendingIntent = PendingIntent.getActivity(
getApplicationContext(),
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(getApplicationContext())
.setContentTitle(getResources().getString(R.string.new_post_notification))
.setContentText(mTitle)
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true)
.setContentIntent(pendingIntent);

// Sets an ID for the notification
int mNotificationId = 1;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());

This is in general how the app is working. I hope you enjoyed reading it. And if you have any questions i would be happy to answer.

See you the next app ;)

Full Stack Developer| Loading…