android:获取年
在我正在开发的应用程序中,我创建了一个服务,该服务在后台运行,并且不使用主Twitter线程,而是使用twitter4j消耗Twitter流API。
看起来像这样:
public class TweetService extends IntentService {
String consumerKey = "TwitterConsumerKey";
String consumerSecret = "TwitterConsumerSecret";
public TweetService() {
super("Tweet Service");
}
@Override
protected void onHandleIntent(Intent intent) {
AccessToken accessToken = createAccessToken();
StatusListener listener = new UserStreamListener() {
// override a whole load of methods - removed for brevity
public void onStatus(Status status) {
String theTweet = status.getText();
if (status.getText().contains("http://")) {
// do something with the tweet
}
}
};
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.setOAuthConsumerKey(consumerKey);
configurationBuilder.setOAuthConsumerSecret(consumerSecret);
TwitterStream twitterStream = new TwitterStreamFactory(configurationBuilder.build()).getInstance(accessToken);
twitterStream.addListener(listener);
twitterStream.user();
}
}
从MyActivity调用, 如下所示:
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
...
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, TweetService.class);
startService(intent);
}
}
我希望能够在每次有一条包含链接的推文时通知UI,以便可以在UI上显示该链接。
我在StackOverflow上发现了一篇帖子,该帖子建议实现此目的的一种方法是引发广播消息,然后该消息可以被活动中的BroadcastReceiver收听。
如果其他任何应用程序愿意,也可以收听广播消息,但是在这种情况下,信息不是很重要,因此我认为采用这种方法很好。
我首先必须将服务更改为如下形式:
public class TweetTask {
public static final String NEW_TWEET = "tweet_task.new_tweet";
}
public class TweetService extends IntentService {
String consumerKey = "TwitterConsumerKey";
String consumerSecret = "TwitterConsumerSecret";
public TweetService() {
super("Tweet Service");
}
@Override
protected void onHandleIntent(Intent intent) {
AccessToken accessToken = createAccessToken();
StatusListener listener = new UserStreamListener() {
// override a whole load of methods - removed for brevity
public void onStatus(Status status) {
String theTweet = status.getText();
if (status.getText().contains("http://")) {
Intent tweetMessage = new Intent(TweetTask.NEW_TWEET);
tweetMessage.putExtra(android.content.Intent.EXTRA_TEXT, document);
sendBroadcast(tweetMessage);
}
}
};
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.setOAuthConsumerKey(consumerKey);
configurationBuilder.setOAuthConsumerSecret(consumerSecret);
TwitterStream twitterStream = new TwitterStreamFactory(configurationBuilder.build()).getInstance(accessToken);
twitterStream.addListener(listener);
twitterStream.user();
}
}
然后,我必须在MyActivity中定义以下代码:
public class MyActivity extends Activity {
protected void onResume() {
super.onResume();
if (dataUpdateReceiver == null) dataUpdateReceiver = new DataUpdateReceiver(textExtractionService);
IntentFilter intentFilter = new IntentFilter(TweetTask.NEW_TWEET);
registerReceiver(dataUpdateReceiver, intentFilter);
}
protected void onPause() {
super.onPause();
if (dataUpdateReceiver != null) unregisterReceiver(dataUpdateReceiver);
}
private class DataUpdateReceiver extends BroadcastReceiver {
private CachedTextExtractionService textExtractionService;
public DataUpdateReceiver(CachedTextExtractionService textExtractionService) {
this.textExtractionService = textExtractionService;
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(TweetTask.NEW_TWEET)) {
// do something with the tweet
}
}
}
}
现在,只要有一条带有链接的推文,我的BroadcastReceiver都会收到通知,并且我可以使用该推文做任何我想做的事情。
这似乎是解决该问题的相当简单的方法,所以我很想知道是否有除我上面提到的缺点以外的其他缺点。
参考: 学习Android:在Mark Needham博客上从我们的JCG合作伙伴 Markh Needham 获得与活动进行通信的服务 。
相关文章 :
翻译自: https://www.javacodegeeks.com/2012/01/learning-android-getting-service-to.html
android:获取年