Is this the right implementation for receiving status notifications from twitter as using as background service? The service starts properly.
First I had started it “START_NOT_STICKY” by Intent.ACTION_BOOT_COMPLETED and Intent.ACTION_USER_PRESENT and I received rate limting exceptions.
Now I changed the onStartCommand to “START_STICKY”, but I’m not sure if the following implementation is right, because some users are on an old version of the app.
public final class TwitterService extends Service implements UserStreamListener {
private static String TWITTER_SERVICE_CONSUMER_KEY = "TWITTER_SERVICE_CONSUMER_KEY";
private static String TWITTER_SERVICE_CONSUMER_SECRET = "TWITTER_SERVICE_CONSUMER_SECRET";
private static String TWITTER_SERVICE_ACCESS_TOKEN = "TWITTER_SERVICE_ACCESS_TOKEN";
private static String TWITTER_SERVICE_ACCESS_TOKEN_SECRET = "TWITTER_SERVICE_ACCESS_TOKEN_SECRET";
private static final String TAG = TwitterService.class.getSimpleName();
private static NotificationManager notificationManager = null;
private static SharedPreferences prefs = null;
private static AudioManager audioManager = null;
private static TwitterStream streamTwitter = null;
private final Binder localBinder = new LocalBinder();
private static ConfigurationBuilder cb = null;
static {
cb = new ConfigurationBuilder();
cb.setUseSSL(true);
cb.setOAuthConsumerKey(TWITTER_SERVICE_CONSUMER_KEY);
cb.setOAuthConsumerSecret(TWITTER_SERVICE_CONSUMER_SECRET);
cb.setOAuthAccessToken(TWITTER_SERVICE_ACCESS_TOKEN);
cb.setOAuthAccessTokenSecret(TWITTER_SERVICE_ACCESS_TOKEN_SECRET);
}
public TwitterService() {
super();
// Connecting
connectToTwitter();
}
@Override
public void onException(Exception e) {
// Log.e(TAG, "Error in twitter user stream listener: "
// +e.getMessage());
}
@Override
public void onStatus(Status status) {
if (status.getUser().getScreenName().equalsIgnoreCase(TweetTask.TWITTER_USER)) {
// Intent tweetMessage = new Intent(TweetTask.NEW_TWEET);
// tweetMessage.putExtra("statusText", status.getText());
// sendBroadcast(tweetMessage);
Log.i(TAG, "onStatus()");
setNotification(status.getText());
}
}
... //some other overrides - deleted
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "Closing twitter service");
streamTwitter.cleanUp();
streamTwitter.shutdown();
streamTwitter = null;
}
private void connectToTwitter() {
//Connect to the twitter api
try {
if (streamTwitter == null) {
Log.i(TAG, "Start streaming user");
streamTwitter = new TwitterStreamFactory(cb.build()).getInstance();
streamTwitter.addListener(this);
streamTwitter.user();
}
}
catch (Exception e) {
Log.e(TAG, "Error connecting twitter: " + e.getMessage());
e.printStackTrace();
}
}
public class LocalBinder extends Binder {
public TwitterService getService() {
return TwitterService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return localBinder;
}
@SuppressLint("InlinedApi")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "Starting twitter service");
return Service.START_STICKY;
}
@Override
public void onCreate() {
super.onCreate();
// ***** ONLY for debugging *****//
// android.os.Debug.waitForDebugger();
// Get the system parameters
prefs = PreferenceManager.getDefaultSharedPreferences(this);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
}
@SuppressLint("NewApi")
private void setNotification(String txt) {
}
private void playSound() {
}
}