Skip to content

Instantly share code, notes, and snippets.

View Red-Folder's full-sized avatar

Mark Taylor Red-Folder

View GitHub Profile
@Red-Folder
Red-Folder / TwitterService.java
Created September 24, 2012 19:55
Phonegap Service Tutorial - Part 3 - TwitterService.java - import snippet
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;
@Red-Folder
Red-Folder / TwitterService.java
Created September 24, 2012 20:25
Phonegap Service Tutorial - Part 3 - TwitterService.java - MaxID field snippet
private String getMaxID() {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
return sp.getString("MaxID", "");
}
private void setMaxID(String maxID) {
if (maxID != null) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sp.edit();
editor.putString("MaxID", maxID);
@Red-Folder
Red-Folder / TwitterService.java
Created September 24, 2012 20:28
Phonegap Service Tutorial - Part 3 - TwitterService.java - newTweet
private Boolean newTweet() {
Boolean result = false;
String oldMaxID = getMaxID();
String newMaxID = oldMaxID;
HttpClient httpClient;
HttpGet getMethod;
HttpResponse response;
InputStream responseStream;
@Red-Folder
Red-Folder / TwitterService.java
Created September 24, 2012 20:29
Phonegap Service Tutorial - Part 3 - TwitterService.java - doWork1
@Override
protected JSONObject doWork() {
if (newTweet())
Log.d(TAG, "New Tweet - I'll need to send a notification");
else
Log.d(TAG, "No new Tweets, back to sleep I go");
return null;
}
@Red-Folder
Red-Folder / TwitterService.java
Created September 24, 2012 20:30
Phonegap Service Tutorial - Part 3 - TwitterService.java - showNotification
public void showNotification( String contentTitle, String contentText ) {
int icon = R.drawable.ic_stat_notification;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, contentTitle, when);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(this, TwitterExampleActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
@Red-Folder
Red-Folder / TwitterService.java
Created September 24, 2012 20:31
Phonegap Service Tutorial - Part 3 - TwitterService.java - doWork2
@Override
protected JSONObject doWork() {
if (newTweet()) {
Log.d(TAG, "New Tweet - I'll need to send a notification");
showNotification("New Tweet","Click to open");
} else
Log.d(TAG, "No new Tweets, back to sleep I go");
return null;
}
@Red-Folder
Red-Folder / TwitterService.java
Created September 24, 2012 20:34
Phonegap Service Tutorial - Part 2 - TwitterService.java - Basic class
package com.red_folder.sample;
import org.json.JSONObject;
import com.red_folder.phonegap.plugin.backgroundservice.BackgroundService;
public class TwitterService extends BackgroundService {
@Override
protected JSONObject doWork() {
@Red-Folder
Red-Folder / TwitterService.java
Created September 24, 2012 20:36
Phonegap Service Tutorial - Part2 - TwitterService.java - TAG line
private static final String TAG = TwitterService.class.getSimpleName();
@Red-Folder
Red-Folder / TwitterService.java
Created September 24, 2012 20:37
Phonegap Service Tutorial - Part2 - TwitterService.java - doWork
@Override
protected JSONObject doWork() {
// TODO Auto-generated method stub
Log.d(TAG, "This is doWork()");
return null;
}
@Red-Folder
Red-Folder / AndroidManifest.xml
Created September 24, 2012 20:38
Phonegap Service Tutorial - Part2 - AndroidManifest.xml - Service entry
<service android:name="com.red_folder.sample.TwitterService">
<intent-filter>
<action android:name="com.red_folder.sample.TwitterService"/>
</intent-filter>
</service>