In Android, start another application in one application

Source: Internet
Author: User

In Android, when an application starts another application, the package name of the started application is known and unknown:
 
* Case 1 = "a known package name usually has two methods:
 
Method 1:
 
[Java]
 
/** Code for starting another application. The package name of the started application is org. hy, the application entry is org. hy. test222Activity, AndroidManifest of the two applications of this method. xml files do not need to be modified **/
Intent intent = new Intent ();
Intent. setComponent (new ComponentName ("org. hy", "org. hy. Test222Activity "));
Intent. setAction (Intent. ACTION_VIEW );
StartActivity (intent );
 
Method 2:
 
[Java]
 
/** Code for starting another application. The action of the started application is org. hy. test222 (defined by yourself), AndroidManifest of the started application of this method. xml to be modified **/
Intent intent = new Intent ();
Intent. setAction ("org. hy. test222 ");
StartActivity (intent );
 
[Html]
 
/** AndroidManifest. xml of the started application **/
<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "org. hy"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
<Uses-sdk android: minSdkVersion = "10"/>

<Application android: icon = "@ drawable/icon" android: label = "@ string/app_name">
<Activity android: name = ". Test222Activity"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "org. hy. test222"/>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. DEFAULT"/>
</Intent-filter>
</Activity>

</Application>
</Manifest>
 
In method 2, the category in AndroidManifest. xml is set to android. intent. category. DEFAULT. This setting is required when Intent is not explicitly specified to start antivit.
 
* Case 2 = unknown package name:
In this case, the package name of the application is unknown, but the package name is used to start the application. Therefore, you need to obtain the package name of the app on your mobile phone in one way.
Pre-installed apps and apps not installed by the system on your mobile phone. You can use the following method to determine whether to return a List set of package names:
[Java]
 
/** Get the package name of the app in the mobile phone and return a List set **/
Public List <PackageInfo> getAllApps (){
List <PackageInfo> apps = new ArrayList <PackageInfo> ();
PackageManager packageManager = this. getPackageManager ();
// Obtain all applications on the mobile phone
List <PackageInfo> paklist = pManager. getInstalledPackages (0 );
For (int I = 0; I <paklist. size (); I ++ ){
PackageInfo pak = (PackageInfo) paklist. get (I );
// Determine whether an application is pre-installed for a non-system (Applications pre-installed for the system if the value is greater than 0, and applications pre-installed for a non-system if the value is less than or equal to 0)
If (pak. applicationInfo. flags & pak. applicationInfo. FLAG_SYSTEM) <= 0 ){
Apps. add (pak );
}
}
Return apps;
}
 
After obtaining the package name, you can start the application by obtaining the package name you want to start.
[Java]
 
Public void launchApp (){
PackageManager packageManager = this. getPackageManager ();
List <PackageInfo> packages = getAllApps ();
PackageInfo pa = null;
For (int I = 0; I <packages. size (); I ++ ){
Pa = packages. get (I );
// Obtain the application name
String appLabel = packageManager. getApplicationLabel (pa. applicationInfo). toString ();
// Obtain the package name
String appPackage = pa. packageName;
Log. d ("" + I, appLabel + "" + appPackage );
}
Intent intent = packageManager. getLaunchIntentForPackage ("jp. co. johospace. jorte"); // "jp. co. johospace. jorte" indicates the name of the package to be started.
StartActivity (intent );
}
 
From heart fly

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.