Android launches another app

Source: Internet
Author: User

Http://www.2cto.com/kf/201203/122910.html

There is an API in the Android SDK:

Public abstract Intent Getlaunchintentforpackage (String packagename)

If the PackageName exists to return to the Ingress Intent, StartActivity (Intent) can start the application, otherwise return nullimport android.app.Activity;

Here's a small example:

[Java]View Plaincopy
  1. Import  android.content.Intent;
  2. Import  Android.content.pm.PackageInfo;
  3. Import  Android.content.pm.PackageManager;
  4. Import  Android.os.Bundle;
  5. Import  Android.view.View;
  6. Public class Main extends Activity {
  7. /** Called when the activity is first created. * /   
  8. @Override   
  9. Public void onCreate (Bundle savedinstancestate) {
  10. Super  . OnCreate (savedinstancestate);
  11. Setcontentview (R.layout.main);
  12. Packagemanager Packagemanager = Getpackagemanager ();
  13. Intent intent=New Intent ();
  14. Intent =packagemanager.getlaunchintentforpackage ("Pkgname");
  15. if (intent==null) {
  16. System.out.println ("APP not found!"  );
  17. }
  18. StartActivity (Intent);
  19. }
  20. }

  1. //-----Start the program we wrote------------------   
  2. Mbutton01.setonclicklistener (new Button.onclicklistener () {
  3. Public void OnClick (View v) {
  4. //-----Core-----First Name a parameter is the package name of the application, and the latter is the main activity name of the application   
  5. Intent intent=New Intent ();
  6. Intent.setcomponent (new componentname ("Com.droidnova.android.games.vortex",
  7. "Com.droidnova.android.games.vortex.  Vortex "));
  8. StartActivity (Intent);
  9. }
  10. });
  11. //-----Start the application that comes with the system------------------   
  12. Mbutton02.setonclicklistener (new Button.onclicklistener () {
  13. Public void OnClick (View v) {
  14. Intent intent=New Intent ();
  15. Intent.setcomponent (new componentname ("Com.android.calendar", "  Com.android.calendar.LaunchActivity "));
  16. StartActivity (Intent);
  17. }
  18. });

This code is the main activity to start another application, and of course you can start any activity
ComponentName componetname = new ComponentName (
This is the package name of another application
"Com.poynt.weibo",
This parameter is the activity to start
"Com.poynt.weibo.ui.IndexActivity");

Android launches another app's activity in one app

Add a intent-filter definition for the activity.
The test finds that the activity can be called normally as long as the following statement is added:
<intent-filter>
<action android:name= ""/>
</intent-filter>

The corresponding startup code is:
Intent Intent = new Intent ();
Intent.setclassname (<package name>, <class name>);
StartActivity (Intent);

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.