Basic sharing methods:
Intent intent = new intent (intent. action_send); // starts the sharing and sending attributes.
Intent. settype ("text/plain"); // share the data type sent
Intent. setpackage (packagename );
Intent. putextra (intent. extra_subject, "XX share"); // share the topic
Intent. putextra (intent. extra_text, "share content"); // share content
Mactivity. startactivity (intent. createchooser (intent, "Select Share"); // the title of the target application selection dialog box
If you need to filter and display some applications, you can use this method:
This method is used to obtain all apps on Android that meet your needs:
Public list <resolveinfo> getsharetargets (){
List <resolveinfo> mapps = new arraylist <resolveinfo> ();
Intent intent = new intent (intent. action_send, null );
Intent. addcategory (intent. category_default );
Intent. settype ("text/plain ");
Packagemanager PM = mactivity. getpackagemanager ();
Mapps = PM. queryintentactivities (intent, packagemanager. component_enabled_state_default );
Return mapps;
}
If you want to share via text message, but you have written a text message software and want to send information via your text message software, you can judge COM. android. click this option to jump to the page you need.
An example is as follows:
List <resolveinfo> applist = getsharetargets ();
Items = new string [applist. Size ()];
If (applist. Size ()> 0 ){
For (INT I = 0; I <applist. Size (); I ++ ){
Resolveinfo tmp_ri = (resolveinfo) applist. Get (I );
Activityinfo tmp_ai = tmp_ri.activityinfo;
Applicationinfo apinfo = tmp_ai.applicationinfo;
String tmp_appname = apinfo. loadlabel (mactivity. getpackagemanager (). tostring ();
Appinfo. Put (tmp_appname, tmp_ai.packagename );
Items [I] = tmp_appname;
}
}
Else {
Toast. maketext (mactivity. getapplicationcontext (), "No sharing application", Toast. length_short). Show ();
}
New alertdialog. Builder (mactivity). settitle ("Select Share ")
. Setsinglechoiceitems (items, 0, new dialoginterface. onclicklistener (){
Public void onclick (dialoginterface dialog, int item ){
If (appinfo. Size ()> 0 ){
String packagename = appinfo. Get (items [item]. tostring (). tostring ();
If (packagename. Equals ("com. Android. MMS ")){
Intent GSMs = new intent (mactivity, sendsmsactivity. Class );
Bundle B = new bundle ();
B. putstring ("content", "shared content ");
GSMs. putextras (B );
Mactivity. startactivity (GSMs );
}
Else {
Intent intent = new intent (intent. action_send); // starts the sharing and sending attributes.
Intent. settype ("text/plain"); // share the data type sent
Intent. setpackage (packagename );
Intent. putextra (intent. extra_subject, "XX share"); // share the topic
Intent. putextra (intent. extra_text, "share content"); // share content
Mactivity. startactivity (intent. createchooser (intent, "Select Share"); // the title of the target application selection dialog box
}
}
Else {
Toast. maketext (mactivity. getapplicationcontext (), "No sharing application", Toast. length_short). Show ();
}
Dialog. Cancel ();
}
}). Show (); // display the dialog box