Android shares information with Intent

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.