I would like to automatically update the Twitter app development timeline.

Asked 2 years ago, Updated 2 years ago, 68 views

I am creating my own Android app on Twitter.
I'm a beginner.
I use twitte4j.

I managed to create a time line to be displayed.
Therefore, instead of manually updating it, I would like to add a setting that automatically updates the timeline.
I'm studying Streaming API, but I'm not sure.
I will post the MainActivity code, so I would appreciate it if you could advise me on adding automatic updates, such as where and what kind of code I should add.
Thank you for your cooperation.

public class MainActivity extensions ListActivity{

    private TweetAdapter mAdapter;
    private Twitter mTwitter;

    @ Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        if(!TwitterUtils.hasAccessToken(this)){
            Intent=new Intent(this,TwitterOAuthActivity.class);
            startActivity (intent);
            finish();
        } else{
            mAdapter=newTweetAdapter(this);
            setListAdapter (mAdapter);

            mTwitter=TwitterUtils.getTwitterInstance(this);
            reloadTimeLine();

        }
    }

    @ Override
    public boolean onCreateOptionsMenu(Menu){
        // Inflate the menu; this add items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    private class TweetAdapter extensions ArrayAdapter <twitter4j.Status>{
        private LayoutInflater mInflater;

        public TweetAdapter (Context context) {
            super(context,android.R.layout.simple_list_item_1);
            mInflater=(LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        }

        @ Override
        publicView getView(int position, View convertView, ViewGroup parent) {
            if(convertView==null){
                convertView=mInflater.inflate(R.layout.list_item_tweet, null);
            }
            Status item=getItem(position);
            TextView name=(TextView) convertView.findViewById(R.id.name);
            name.setText(item.getUser().getName());
            TextView screenName=(TextView) convertView.findViewById(R.id.screen_name);
            screenName.setText("@"+item.getUser().getScreenName());
            TextView text=(TextView) convertView.findViewById(R.id.text);
            text.setText(item.getText());
            SmartImageView= (SmartImageView) convertView.findViewById (R.id.icon);
            icon.setImageUrl(item.getUser().getProfileImageURL());
            return convertView;
        }
    }

    private void reloadTimeLine(){
        AsyncTask<Void,Void,List<Twitter4j.Status>>task=new AsyncTask<Void,Void,List<Twitter4j.Status>>(){
            @ Override
            Protected List <twitter4j.Status>doInBackground(Void...params){
                try{

                    return mTwitter.getHomeTimeline();
                } catch(TwitterExceptione){
                    e.printStackTrace();
                }
                return null;
            }

            @ Override
            protected void onPostExecute(List<twitter4j.Status>result){
                if(result!=null){
                    mAdapter.clear();
                    for (twitter4j.Status status:result) {
                        mAdapter.add(status);
                    }
                    getListView().setSelection(0);
                } else{
                    showToast("Failed to get timeline...");
                }
            }
        };
        task.execute();
    }

    private void showToast (String text) {
        Toast.makeText(this,text,Toast.LENGTH_SHORT).show();
    }
}

android java twitter

2022-09-30 19:02

1 Answers

Twitter 4J has a class called TwitterStream, so I think it would be easier to use it.
Example:

TwitterStream twitterStream=newTwitterStreamFactory().getInstance();
        StatusListener listener = new StatusListener() {
            @ Override
            public void onStatus (Status status) {
                System.out.println("@"+status.getUser().getScreenName()+"-"+status.getText());
            }

            @ Override
            public void onDeletionNotice (StatusDeletionNotice) {
                System.out.println("Gota status deletion notice id:" + statusDeletionNotice.getStatusId());
            }

            @ Override
            public void onTrackLimitationNotice(int numberOfLimitedStatus) {
                System.out.println("Got track limitation notice:" + numberOfLimitedStatus);
            }

            @ Override
            public void onScrubGeo(long userId,long upToStatusId){
                System.out.println("Gotscrub_geo event userId:"+userId+"upToStatusId:"+upToStatusId);
            }

            @ Override
            public void onStallWarning (StallWarning warning) {
                System.out.println("Got stall warning:" + warning);
            }

            @ Override
            public void onException (Exceptionex) {
                ex.printStackTrace();
            }
        };
        twitterStream.addListener(listener);
        twitterStream.sample();


2022-09-30 19:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.