2013-02-02 27 views
10

Jak mogę udostępnić wiadomość tekstową na Twitterze? Chciałbym pozwolić użytkownikowi wybrać między wieloma wiadomościami tekstowymi i kliknąć jeden, aby udostępnić go za pośrednictwem Twittera.Udostępnianie Androida na Twitterze

+0

Możliwe duplikaty http://stackoverflow.com/questions/14564805/how-can-i-get-a-tweet-for-a-specific-url/14565661#14565661 – TN888

Odpowiedz

14

Istnieje szereg sposobów realizacji this.One można po prostu otworzyć http://twitter.com i udostępniać tweety w jednym zamiarem, takich jak ..

Intent tweet = new Intent(Intent.ACTION_VIEW); 
tweet.setData(Uri.parse("http://twitter.com/?status=" + Uri.encode(message)));//where message is your string message 
startActivity(tweet); 

Albo

Intent tweet = new Intent(Intent.ACTION_SEND); 
tweet.putExtra(Intent.EXTRA_TEXT, "Sample test for Twitter."); 
startActivity(Intent.createChooser(share, "Share this via")); 

Lub

String tweetUrl = "https://twitter.com/intent/tweet?text=WRITE YOUR MESSAGE HERE &url=" 
        + "https://www.google.com"; 
Uri uri = Uri.parse(tweetUrl); 
startActivity(new Intent(Intent.ACTION_VIEW, uri)); 
2

Udostępnianie przez Twitter Korzystam z własnej niestandardowej funkcji statycznej w klasie Util:

public class Util 
{ 
    public static Intent getTwitterIntent(Context ctx, String shareText) 
    { 
     Intent shareIntent; 

     if(doesPackageExist(ctx, "com.twitter.android")) 
     {   
      shareIntent = new Intent(Intent.ACTION_SEND); 
      shareIntent.setClassName("com.twitter.android", 
       "com.twitter.android.PostActivity"); 
      shareIntent.setType("text/*"); 
      shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareText); 
      return shareIntent; 
     } 
     else 
     { 
      String tweetUrl = "https://twitter.com/intent/tweet?text=" + shareText; 
      Uri uri = Uri.parse(tweetUrl); 
      shareIntent = new Intent(Intent.ACTION_VIEW, uri); 
      return shareIntent; 
     } 
    } 
} 

Zaletą tej funkcji jest to, że jeśli zainstalowana jest aplikacja Twitter, korzysta z niej, w przeciwnym razie korzysta ze strony internetowej twitter. Tekst, który będzie tweetowany, zostanie przekazany do funkcji.

W swoim scenariuszu, gdy użytkownik wybierze spośród różnych wiadomości, należy przekazać wybraną wiadomość do funkcji. Będzie on następnie oddać zamiarem który można podłączyć do startActivity function() tak:

startActivity(Util.getTwitterIntent(context, "Text that will be tweeted")); 
+0

, ale gdy otwiera się przeglądarka, to Nie wróciłem do Twojej aplikacji. –

+0

Czy mimo to użytkownik korzysta z tego podejścia, ale wysyła zdjęcia i tekst? – iGoDa

1

Można użyć biblioteki Twitter4J. Pobierz i dodaj ścieżkę do ścieżki java. próbki Code:

  • dodawania nowych tweet:

    Twitter twitter = TwitterFactory.getSingleton();

    Status status = twitter.updateStatus(latestStatus);

  • zalogować się przy użyciu protokołu OAuth (kod Java, edytować go):

    Twitter twitter = TwitterFactory.getSingleton(); 
    twitter.setOAuthConsumer("[consumer key]", "[consumer secret]"); 
    RequestToken requestToken = twitter.getOAuthRequestToken(); 
    AccessToken accessToken = null; 
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    while (null == accessToken) { 
        System.out.println("Open the following URL and grant access to your account:"); 
        System.out.println(requestToken.getAuthorizationURL()); 
        System.out.print("Enter the PIN(if aviailable) or just hit enter.[PIN]:"); 
        String pin = br.readLine(); 
        try{ 
        if(pin.length() > 0){ 
         accessToken = twitter.getOAuthAccessToken(requestToken, pin); 
        }else{ 
         accessToken = twitter.getOAuthAccessToken(); 
        } 
        } catch (TwitterException te) { 
        if(401 == te.getStatusCode()){ 
         System.out.println("Unable to get the access token."); 
        }else{ 
         te.printStackTrace(); 
        } 
        } 
    } 
    //persist to the accessToken for future reference. 
    storeAccessToken(twitter.verifyCredentials().getId() , accessToken); 
    Status status = twitter.updateStatus(args[0]); 
    System.out.println("Successfully updated the status to [" + status.getText() + "]."); 
    System.exit(0); 
    

    }

    private static void storeAccessToken(int useId, AccessToken accessToken){ //store accessToken.getToken() //store accessToken.getTokenSecret() }

    • coraz tweety:

      Twitter twitter = TwitterFactory.getSingleton(); 
      Query query = new Query("source:twitter4j yusukey"); 
      QueryResult result = twitter.search(query); 
      for (Status status : result.getStatuses()) { 
          System.out.println("@" + status.getUser().getScreenName() + ":" + status.getText()); 
      } 
      
  • coraz Czas:

    Twitter twitter = TwitterFactory.getSingleton(); 
        List<Status> statuses = twitter.getHomeTimeline(); 
        System.out.println("Showing home timeline."); 
        for (Status status : statuses) { 
         System.out.println(status.getUser().getName() + ":" + 
             status.getText()); 
    } 
    

Mam nadzieję, że pomogłem

1

Jesli potrzebujesz odpowiedzi sukces i tweet id U można używać twitter SDK stanu Services w celu publikowania tweet na Twitterze. możesz pisać tekst za pomocą tego.

StatusesService statusesService = TwitterCore.getInstance().getApiClient().getStatusesService(); 
    Call<Tweet> tweetCall = statusesService.update(text, null, false, null, null, null, false, false, null); 
    tweetCall.enqueue(new Callback<Tweet>() { 
     @Override 
     public void success(Result<Tweet> result) { 
      Log.d("result", result.data.idStr); 
     } 



     @Override 
     public void failure(TwitterException exception) { 
      hideProgressDialogResult(); 
      exception.printStackTrace(); 

     } 
    }); 

u można również przesłać zdjęcie z opisem, ale przed tym u trzeba utworzyć identyfikator nośnika obrazu przy użyciu twitter usług medialnych i przekazać tę mediów id służbie stanu.

Powiązane problemy