android:获取年_学习Android:获取与活动进行通信的服务

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:获取年

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值