Advertisement
Guest User

Untitled

a guest
Sep 6th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. package com.example.programmer.twitter;
  2.  
  3. import android.content.Intent;
  4. import android.content.pm.PackageManager;
  5. import android.content.pm.ResolveInfo;
  6. import android.net.Uri;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import android.widget.Toast;
  11.  
  12. import java.io.UnsupportedEncodingException;
  13. import java.net.URLEncoder;
  14. import java.util.List;
  15.  
  16. public class MainActivity extends AppCompatActivity {
  17.  
  18. private static final String TAG = "" ;
  19.  
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_main);
  24. shareTwitter("tes");
  25. }
  26.  
  27. private void shareTwitter(String message) {
  28. Intent tweetIntent = new Intent(Intent.ACTION_SEND);
  29. tweetIntent.putExtra(Intent.EXTRA_TEXT, "This is a Test.");
  30. tweetIntent.setType("text/plain");
  31.  
  32. PackageManager packManager = getPackageManager();
  33. List<ResolveInfo> resolvedInfoList = packManager.queryIntentActivities(tweetIntent, PackageManager.MATCH_DEFAULT_ONLY);
  34.  
  35. boolean resolved = false;
  36. for (ResolveInfo resolveInfo : resolvedInfoList) {
  37. if (resolveInfo.activityInfo.packageName.startsWith("com.twitter.android")) {
  38. tweetIntent.setClassName(
  39. resolveInfo.activityInfo.packageName,
  40. resolveInfo.activityInfo.name);
  41. resolved = true;
  42. break;
  43. }
  44. }
  45. if (resolved) {
  46. startActivity(tweetIntent);
  47. } else {
  48. Intent i = new Intent();
  49. i.putExtra(Intent.EXTRA_TEXT, message);
  50. i.setAction(Intent.ACTION_VIEW);
  51. i.setData(Uri.parse("https://twitter.com/intent/tweet?text=" + urlEncode(message)));
  52. startActivity(i);
  53. Toast.makeText(this, "Twitter app isn't found", Toast.LENGTH_LONG).show();
  54. }
  55. }
  56.  
  57. private String urlEncode(String s) {
  58. try {
  59. return URLEncoder.encode(s, "UTF-8");
  60. } catch (UnsupportedEncodingException e) {
  61. Log.wtf(TAG, "UTF-8 should always be supported", e);
  62. return "";
  63. }
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement