use of twitter4j.DirectMessage in project twitter4j by yusuke.
the class ShowDirectMessage method main.
/**
* Usage: java twitter4j.examples.directmessage.ShowDirectMessage [message id]
*
* @param args String[]
*/
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("Usage: java twitter4j.examples.directmessage.ShowDirectMessage [message id]");
System.exit(-1);
}
Twitter twitter = new TwitterFactory().getInstance();
try {
DirectMessage message = twitter.showDirectMessage(Long.parseLong(args[0]));
System.out.println("From: id:" + message.getId() + " - " + message.getText());
System.exit(0);
} catch (TwitterException te) {
te.printStackTrace();
System.out.println("Failed to get message: " + te.getMessage());
System.exit(-1);
}
}
use of twitter4j.DirectMessage in project Talon-for-Twitter by klinker24.
the class DMFragment method onRefreshStarted.
@Override
public void onRefreshStarted() {
new AsyncTask<Void, Void, Void>() {
private boolean update;
private int numberNew;
@Override
protected void onPreExecute() {
DrawerActivity.canSwitch = false;
}
@Override
protected Void doInBackground(Void... params) {
try {
twitter = Utils.getTwitter(context, DrawerActivity.settings);
User user = twitter.verifyCredentials();
long lastId = sharedPrefs.getLong("last_direct_message_id_" + currentAccount, 0);
Paging paging;
if (lastId != 0) {
paging = new Paging(1).sinceId(lastId);
} else {
paging = new Paging(1, 500);
}
List<DirectMessage> dm = twitter.getDirectMessages(paging);
List<DirectMessage> sent = twitter.getSentDirectMessages(paging);
if (dm.size() != 0) {
sharedPrefs.edit().putLong("last_direct_message_id_" + currentAccount, dm.get(0).getId()).commit();
update = true;
numberNew = dm.size();
} else {
update = false;
numberNew = 0;
}
DMDataSource dataSource = DMDataSource.getInstance(context);
for (DirectMessage directMessage : dm) {
try {
dataSource.createDirectMessage(directMessage, currentAccount);
} catch (IllegalStateException e) {
dataSource = DMDataSource.getInstance(context);
dataSource.createDirectMessage(directMessage, currentAccount);
}
}
for (DirectMessage directMessage : sent) {
try {
dataSource.createDirectMessage(directMessage, currentAccount);
} catch (Exception e) {
dataSource = DMDataSource.getInstance(context);
dataSource.createDirectMessage(directMessage, currentAccount);
}
}
} catch (TwitterException e) {
// Error in updating status
Log.d("Twitter Update Error", e.getMessage());
}
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
long now = new Date().getTime();
long alarm = now + DrawerActivity.settings.dmRefresh;
Log.v("alarm_date", "direct message " + new Date(alarm).toString());
PendingIntent pendingIntent = PendingIntent.getService(context, DM_REFRESH_ID, new Intent(context, DirectMessageRefreshService.class), 0);
if (DrawerActivity.settings.dmRefresh != 0)
am.setRepeating(AlarmManager.RTC_WAKEUP, alarm, DrawerActivity.settings.dmRefresh, pendingIntent);
else
am.cancel(pendingIntent);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
try {
if (update) {
getCursorAdapter(false);
CharSequence text = numberNew == 1 ? numberNew + " " + getResources().getString(R.string.new_direct_message) : numberNew + " " + getResources().getString(R.string.new_direct_messages);
showToastBar(text + "", jumpToTop, 400, true, toTopListener);
int size = toDP(5) + mActionBarSize + (DrawerActivity.translucent ? DrawerActivity.statusBarHeight : 0);
listView.setSelectionFromTop(numberNew + (MainActivity.isPopup || landscape || MainActivity.settings.jumpingWorkaround ? 1 : 2), size);
} else {
getCursorAdapter(false);
CharSequence text = getResources().getString(R.string.no_new_direct_messages);
showToastBar(text + "", allRead, 400, true, toTopListener);
}
refreshLayout.setRefreshing(false);
} catch (IllegalStateException e) {
// fragment not attached to activity
}
DrawerActivity.canSwitch = true;
}
}.execute();
}
use of twitter4j.DirectMessage in project Talon-for-Twitter by klinker24.
the class DirectMessageRefreshService method onHandleIntent.
@Override
public void onHandleIntent(Intent intent) {
sharedPrefs = getSharedPreferences("com.klinker.android.twitter_world_preferences", 0);
Context context = getApplicationContext();
AppSettings settings = AppSettings.getInstance(context);
// if they have mobile data on and don't want to sync over mobile data
if (Utils.getConnectionStatus(context) && !settings.syncMobile) {
return;
}
boolean update = false;
int numberNew = 0;
try {
Twitter twitter = Utils.getTwitter(context, settings);
int currentAccount = sharedPrefs.getInt("current_account", 1);
User user = twitter.verifyCredentials();
long lastId = sharedPrefs.getLong("last_direct_message_id_" + currentAccount, 0);
Paging paging;
if (lastId != 0) {
paging = new Paging(1).sinceId(lastId);
} else {
paging = new Paging(1, 500);
}
List<DirectMessage> dm = twitter.getDirectMessages(paging);
List<DirectMessage> sent = twitter.getSentDirectMessages(paging);
if (dm.size() != 0) {
sharedPrefs.edit().putLong("last_direct_message_id_" + currentAccount, dm.get(0).getId()).commit();
numberNew = dm.size();
} else {
numberNew = 0;
}
DMDataSource dataSource = DMDataSource.getInstance(context);
int inserted = 0;
for (DirectMessage directMessage : dm) {
try {
dataSource.createDirectMessage(directMessage, currentAccount);
} catch (Exception e) {
dataSource = DMDataSource.getInstance(context);
dataSource.createDirectMessage(directMessage, currentAccount);
}
inserted++;
}
for (DirectMessage directMessage : sent) {
try {
dataSource.createDirectMessage(directMessage, currentAccount);
} catch (Exception e) {
dataSource = DMDataSource.getInstance(context);
dataSource.createDirectMessage(directMessage, currentAccount);
}
}
sharedPrefs.edit().putBoolean("refresh_me", true).commit();
sharedPrefs.edit().putBoolean("refresh_me_dm", true).commit();
if (settings.notifications && settings.dmsNot && inserted > 0) {
int currentUnread = sharedPrefs.getInt("dm_unread_" + currentAccount, 0);
sharedPrefs.edit().putInt("dm_unread_" + currentAccount, numberNew + currentUnread).commit();
NotificationUtils.refreshNotification(context);
}
if (settings.syncSecondMentions) {
startService(new Intent(context, SecondDMRefreshService.class));
}
sendBroadcast(new Intent("com.klinker.android.twitter.NEW_DIRECT_MESSAGE"));
} catch (TwitterException e) {
// Error in updating status
Log.d("Twitter Update Error", e.getMessage());
}
}
Aggregations