Android One app launches another app

Source: Internet
Author: User

Recently, an app launched another app, this play is very fire, have tried to update QQ to 5.1 version, QQ health inside can add other apps, from QQ to other app application. This is very fun, and suddenly brought a lot of traffic ah.

First, a few lines of code, one app to launch another app

Get the app to jump through the package name, create the intent object Intent Intent = Getpackagemanager (). Getlaunchintentforpackage ("Com.example.liuxiang"); /If intent is empty, say the name does not install the application to jump if (intent! = NULL) {//here is the same as the activity pass parameter, do not worry about how to pass parameters, Also the receiving parameters are the same as the activity and activity parameters Intent.putextra ("name", "Liu Xiang"), Intent.putextra ("Birthday", "1983-7-13"); StartActivity (intent);} else {//Do not install app to jump, remind Toast.maketext (Getapplicationcontext (), "Yo, download install this app now", Toast.length_long). Show ();

  

There may be a lot of similar code here, or mention it, let everyone know the process.

Second, how to get the parameters of the app being launched?

public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Intent Intent = Getintent (); Bundle bundle = Intent.getextras (); if (bundle! = null) {String name = bundle.getstring ("name"); String birthday = bundle.getstring ("Birthday"); if (name! = NULL && Birthday! = null) {Toast.maketext (Getapplicati Oncontext (), "name:" + name + "Birthday:" + birthday, Toast.length_short). Show ();}}}

In the app that is opened, it is also simply a few lines of code that can receive the passed-over parameter information. If the bundle here is empty, it means that it is not a jump, it may be that the user receives a click on the desktop icon to open the app, so there is definitely no parameter value. Let's play.

Wait,,, don't think, this is over, there is more important in the back: how to ensure that no time to jump can receive parameters, this should be considered.

Three, encountered the problem: can not each jump to get parameters, this is why?

After research, it is necessary to open the activated activity each time to receive, because the passed parameter is sent to the activated activity (Android.intent.action.MAIN), In other activity how to get the parameter data. So it's a workaround to have to first open the activated activity every time you jump.

To ensure that the app is open every time you start the activity, write a welcome page here (wellcomeactivity)

public class Wellcomeactivity extends Activity {private final static int msg_200 = 200; Handler Mhandler = new Handler () {@Overridepublic void Handlemessage (Message msg) {switch (msg.what) {case Msg_200:intent Intent = new Intent (wellcomeactivity.this, Mainactivity.class); startactivity (intent); finish (); break;default:break;}}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_wellcome); Intent Intent = Getintent ();        Bundle bundle = Intent.getextras ();            if (bundle! = null) {String name = bundle.getstring ("name");            String birthday = bundle.getstring ("Birthday");    if (name = null && birthday! = null) {Toast.maketext (Getapplicationcontext (), "name:" + name + "                Birthday: "+ birthday, Toast.length_short). Show ();                TextView t = (TextView) Findviewbyid (r.id.tv_wellcome);    T.settext ("Name:" + name + "Birthday:" + birthday);        }} mhandler.sendemptymessagedelayed (msg_200, 3000);} @Overridepublic void onbackpressed () {}}

Here every time you can get parameters, you can do what you want to do, you can save to Sharemamager or database, do the corresponding operation.

And then into activity (mainactivity)

public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);}}

demo:http://download.csdn.net/detail/lqw770737185/7903937

This address: http://www.cnblogs.com/liqw/p/3968340.html

Android One app launches another app

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.