在活动开始/停止时启动/停止TwitterStream

时间:2015-02-01 06:23:55

标签: android android-activity twitter twitter4j android-lifecycle

我正在我的应用程序中运行一个TwitterStream,运行正常,但即使关闭应用程序时也会继续运行(即应用程序生命周期进入暂停或停止时)。我认为这样做不是一个好公民,所以我试图让它基于生命周期开始/停止。

以下是启动该应用的代码:

 @Override
public void onStart(){
    super.onStart();
    Log.i("ONSTART IS CALLED", String.valueOf(twitterConfig));
    initiateTwitterStream();

}

private void initiateTwitterStream() {
    FilterQuery query = new FilterQuery();
    String keywords[] = {"#Ferguson"};
    query.track(keywords);
    twitterStream = new TwitterStreamFactory(twitterConfig).getInstance();
    UserStreamListener listener = new UserStreamListener() {
        @Override
        public void onStatus(final Status status) {
            Log.i("STATUS", status.getUser().getName() + " : " + status.getText());
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Log.i("STATUS ON UI THREAD", String.valueOf(Looper.getMainLooper().getThread() == Thread.currentThread()));
                    mAdapter.insert(status.getText(), 0);
                    mAdapter.notifyDataSetChanged();
                }
            });

        }

        @Override
        public void onDeletionNotice(long directMessageId, long userId) {

        }

        @Override
        public void onFriendList(long[] friendIds) {

        }

        @Override
        public void onFavorite(User source, User target, Status favoritedStatus) {

        }

        @Override
        public void onUnfavorite(User source, User target, Status unfavoritedStatus) {

        }

        @Override
        public void onFollow(User source, User followedUser) {

        }

        @Override
        public void onUnfollow(User source, User unfollowedUser) {

        }

        @Override
        public void onDirectMessage(DirectMessage directMessage) {

        }

        @Override
        public void onUserListMemberAddition(User addedMember, User listOwner, UserList list) {

        }

        @Override
        public void onUserListMemberDeletion(User deletedMember, User listOwner, UserList list) {

        }

        @Override
        public void onUserListSubscription(User subscriber, User listOwner, UserList list) {

        }

        @Override
        public void onUserListUnsubscription(User subscriber, User listOwner, UserList list) {

        }

        @Override
        public void onUserListCreation(User listOwner, UserList list) {

        }

        @Override
        public void onUserListUpdate(User listOwner, UserList list) {

        }

        @Override
        public void onUserListDeletion(User listOwner, UserList list) {

        }

        @Override
        public void onUserProfileUpdate(User updatedUser) {

        }

        @Override
        public void onBlock(User source, User blockedUser) {

        }

        @Override
        public void onUnblock(User source, User unblockedUser) {

        }


        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {

        }

        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {

        }

        @Override
        public void onScrubGeo(long userId, long upToStatusId) {

        }

        @Override
        public void onStallWarning(StallWarning warning) {

        }

        @Override
        public void onException(Exception ex) {

        }
    };
    twitterStream.addListener(listener);
    twitterStream.filter(query);
}

以下是停止应用程序的代码(由于我之后再次调用twitterstream,因此我没有必要注释掉twitterstream.shutdown行):

 @Override
public void onStop(){
    Log.i("ONSTOP IS CALLED", String.valueOf(twitterConfig));
  twitterStream.cleanUp();
  //  twitterStream.shutdown();
    super.onStop();
}

注意:Twitter身份验证在onCreate方法中发生如下:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_tweet, container, false);

    // Set the adapter
    mListView = (AbsListView) view.findViewById(android.R.id.list);
    mTweetCompose = (EditText) view.findViewById(R.id.comptwitter);
    ((AdapterView<ListAdapter>) mListView).setAdapter(mAdapter);

    Log.i("THE VIEW IS CREATED", "BUT WHEN?");
    TwitterSession session =
            Twitter.getSessionManager().getActiveSession();
    TwitterAuthToken authToken = session.getAuthToken();
    mTtoken = authToken.token;
    mSecret = authToken.secret;
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true)
            .setOAuthConsumerKey(TWITTER_KEY)
            .setOAuthConsumerSecret(TWITTER_SECRET)
            .setOAuthAccessToken(mTtoken)
            .setOAuthAccessTokenSecret(mSecret);
    twitterConfig = cb.build();
    //Set up Twitter Stream

    //Set up Tweet Composing
    initiateTwitterCompose();
    return view;
}

请注意,如果我将initiateTwitterStream方法放在oncreate方法中,代码运行正常 - 所以我猜测我得到的错误是由于异步代码在活动停止和启动之间未对齐造成的。

以下是错误代码:

01-31 22:19:04.186  26728-26737/com.example.lgorse.cineios_test A/libc﹕ Fatal signal 11 (SIGSEGV) at 0x00000008 (code=1), thread 26737 (FinalizerDaemon)

需要更多信息 - 请询问。期待收到你的回复。

0 个答案:

没有答案