Sharing to Facebook, Twitter, etcx

kingfishblacksmithΚινητά – Ασύρματες Τεχνολογίες

14 Δεκ 2013 (πριν από 3 χρόνια και 6 μήνες)

158 εμφανίσεις

Sharing to Facebook, Twitter, etc.


The process is simple.


Intent

shareIntent

=

new

Intent
(
Intent
.ACTION_SEND);


shareIntent.setType(
"text/plain"
);


shareIntent.putExtra(
Intent
.EXTRA_TEXT,

"URLyouWantToShare"
);


startActivity(
Intent
.createChooser(
shareIntent,

"Share..."
));


Then you will be given a list of apps that are available on your device to share to which should include
Twitter and Facebook if you have the app.

For sending specifically to Twitter (note .setType
()
)

Intent

sendIntent

=

new

Intent
(
Intent
.
ACTION_SEND
);


sendIntent
.
putExtra
(
Intent
.
EXTRA_TEXT
,

"This

is

a

sample

message

via

Public

Intent"
);


sendIntent
.
setType
(
"application/twitter"
);


startActivity
(
Intent
.
createChooser
(
sendIntent
,

null
));



I would assume for Facebook, it would

be

Intent

sendIntent

=

new

Intent
(
Intent
.
ACTION_SEND
);


sendIntent
.
putExtra
(
Intent
.
EXTRA_TEXT
,

"This

is

a

sample

message

via

Public

Intent"
);


sendIntent
.
setType
(
"application/
facebook
"
);


startActivity
(
Intent
.
createChooser
(
sendIntent
,

null
));



although

I can’t find any proof of that.


I’ve also found for sharing text:

Intent

sharingIntent

=

new

Intent
(
Intent
.
ACTION_SEND
);

sharingIntent
.
setType
(
"plain/text"
);

sharingIntent
.
putExtra
(
android
.
content
.
Intent
.
EXTRA_TEXT
,

"This

is

the

text

that

will

be

shared."
));

startActivity
(
Intent
.
createChooser
(
sharingIntent
,
"Share

using"
));


and for sharing other things likes video, images, etc.

Intent

sharingIntent

=

new

Intent
(
Intent
.
ACTION_SEND
);

Uri

screenshotUri

=

Uri
.
parse
(
path
);


sharingIntent
.
setType
(
"image/
png"
);

sharingIntent
.
putExtra
(
Intent
.
EXTRA_STREAM
,

screenshotUri
);

startActivity
(
Intent
.
createChooser
(
sharingIntent
,

"Share

image

using"
));


where “image/png” is just the appropriate MIME type for whatever type you
wanna share.


This code can be used if you want to share to Twitter exclusively or Facebook
exclusively. I guess we could use this after a user decides which they want
to share with, but that sounds messy:


private

void

initShareIntent
(
String

type
)

{


boolean

found

=

false
;


Intent

share

=

new

Intent
(
android
.
content
.
Intent
.
ACTION_SEND
);


share
.
setType
(
"image/jpeg"
);



//

gets

the

list

of

intents

that

can

be

loaded.


List
<
ResolveInfo
>

resInfo

=

getPackageManager
().
queryIntentActivities
(
share
,

0
);


if

(!
resInfo
.
isEmpty
()){


for

(
ResolveInfo

info

:

resInfo
)

{


if

(
info
.
activityInfo
.
packageName
.
toLowerCase
().
contains
(
type
)

||



info
.
activityInfo
.
name
.
toLowerCase
().
contains
(
type
)

)

{


share
.
putExtra
(
Intent
.
EXTRA_SUBJECT
,

"subject"
);


share
.
putExtra
(
Intent
.
EXTRA_TEXT
,

"your

text"
);


share
.
putExtra
(
Intent
.
EXTRA_STREAM
,

Uri
.
fromFile
(
new

File
(
myPath
))

);

//

Optional,

just

if

you

wanna

share

an

image.


share
.
setPackage
(
info
.
activityInfo
.
packageName
);


found

=

true
;


break
;


}


}


if

(!
found
)


return
;



startActivity
(
Intent
.
createChooser
(
share
,

"Select"
));


}

}


Calls to this method
would be done with

initShareIntent
(
"twi"
);

initShareIntent
(
"face"
);

initShareIntent
(
"mail"
);

//

or

"gmail"

however, I think you need to register the app to the respective services in order to get their api and use
the buttons.

There’s a few ways to share with the SocialAuth Android SDK and this one is a global share button that
opens up the possible apps that are already installed on the device:

“You also need to provide a

ResponseListener

to do the handling once the
authentication is
complete, for

example, to post updates.”


private

final

class

ResponseListener
implements

DialogListener

{


public

void

onComplete(Bundle values) {


Log.d(
"ShareButton"

,
"Authentication

Successful"
);




adapter.updateStatus(
"SocialAuth

Android"
);

}


“Following is the code for creating the adapter and attaching it

with the button.”


@Override

public

void

onCreate(Bundle savedInstanceState) {


super
.onCreate(savedInstanceState);


setContentV
iew(R.layout.main);




TextView textview = (TextView)findViewById(R.id.text);


textview.setText(
"Welcome

to

SocialAuth

Demo.

We

are

sharing

"

+


"text

SocialAuth

Android

by

share

button."
);


Button share = (Button
)findViewById(R.id.sharebutton);


share.setText(
"Share"
);


share.setTextColor(Color.WHITE);


share.setBackgroundResource(R.drawable.button_gradient);


adapter =
new

SocialAuthAdapter(
new

ResponseListener());


adapter.addProvider(
Provider.FACEBOOK, R.drawable.facebook);


adapter.addProvider(Provider.TWITTER, R.drawable.twitter);


adapter.addProvider(Provider.LINKEDIN, R.drawable.linkedin);


adapter.addProvider(Provider.MYSPACE, R.drawable.myspace);


adapter.enable(
share);

}

http://www.codeproject.com/Tips/457153/How
-
to
-
integrate
-
Facebook
-
Twitter
-
Linkedin
-
in
-
Andr