Android starts another APP Based on the package name in the APP

Source: Internet
Author: User

The TestIntentData project is as follows:

MainActivity is as follows:

[Java]
Package cn. testintentdata;
Import java. util. List;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. app. Activity;
Import android. content. ComponentName;
Import android. content. Intent;
Import android. content. pm. PackageInfo;
Import android. content. pm. PackageManager. NameNotFoundException;
Import android. content. pm. ResolveInfo;
/**
* Demo description:
* Start another APP Based on the package name in the Application
*
*/
Public class MainActivity extends Activity {
Private Button mButton;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Init ();
}
Private void init (){
MButton = (Button) findViewById (R. id. button );
MButton. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View view ){
StartAnotherApp ("cc. testintent ");
}
});
}

Private void startAnotherApp (String packageName ){
PackageInfo packageInfo = null;
Try {
PackageInfo = getPackageManager (). getPackageInfo (packageName, 0 );
If (packageInfo = null ){
System. out. println ("packageInfo = null ");
} Else {
System. out. println ("packageInfo! = Null ");
}
} Catch (NameNotFoundException e ){
E. printStackTrace ();
}


// <Data android: scheme = "app" android: host = "jp. co. cybird. barcodefootballer/"/>


Intent resolveIntent = new Intent (Intent. ACTION_MAIN, null );
ResolveIntent. setData (Uri. parse ("app: // jp. co. cybird. barcodefootballer /"));
ResolveIntent. addCategory (Intent. CATEGORY_LAUNCHER );
ResolveIntent. setPackage (packageInfo. packageName );
System. out. println ("packageInfo. packageName =" + packageInfo. packageName );

List <ResolveInfo> resolveInfoList =
GetPackageManager (). queryIntentActivities (resolveIntent, 0 );

System. out. println ("resolveInfoList. size () =" + resolveInfoList. size ());

ResolveInfo resolveInfo = resolveInfoList. iterator (). next ();
If (resolveInfo! = Null ){
String activityPackageName = resolveInfo. activityInfo. packageName;
String className = resolveInfo. activityInfo. name;

Intent intent = new Intent (Intent. ACTION_MAIN );
Intent. addCategory (Intent. CATEGORY_LAUNCHER );
ComponentName componentName = new ComponentName (activityPackageName, className );

Intent. setComponent (componentName );
StartActivity (intent );
}

}
 
}

Package cn. testintentdata;
Import java. util. List;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. app. Activity;
Import android. content. ComponentName;
Import android. content. Intent;
Import android. content. pm. PackageInfo;
Import android. content. pm. PackageManager. NameNotFoundException;
Import android. content. pm. ResolveInfo;
/**
* Demo description:
* Start another APP Based on the package name in the Application
*
*/
Public class MainActivity extends Activity {
Private Button mButton;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Init ();
}
Private void init (){
MButton = (Button) findViewById (R. id. button );
MButton. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View view ){
StartAnotherApp ("cc. testintent ");
}
});
}
 
Private void startAnotherApp (String packageName ){
PackageInfo packageInfo = null;
Try {
PackageInfo = getPackageManager (). getPackageInfo (packageName, 0 );
If (packageInfo = null ){
System. out. println ("packageInfo = null ");
} Else {
System. out. println ("packageInfo! = Null ");
}
} Catch (NameNotFoundException e ){
E. printStackTrace ();
}


// <Data android: scheme = "app" android: host = "jp. co. cybird. barcodefootballer/"/>


Intent resolveIntent = new Intent (Intent. ACTION_MAIN, null );
ResolveIntent. setData (Uri. parse ("app: // jp. co. cybird. barcodefootballer /"));
ResolveIntent. addCategory (Intent. CATEGORY_LAUNCHER );
ResolveIntent. setPackage (packageInfo. packageName );
System. out. println ("packageInfo. packageName =" + packageInfo. packageName );

List <ResolveInfo> resolveInfoList =
GetPackageManager (). queryIntentActivities (resolveIntent, 0 );

System. out. println ("resolveInfoList. size () =" + resolveInfoList. size ());

ResolveInfo resolveInfo = resolveInfoList. iterator (). next ();
If (resolveInfo! = Null ){
String activityPackageName = resolveInfo. activityInfo. packageName;
String className = resolveInfo. activityInfo. name;

Intent intent = new Intent (Intent. ACTION_MAIN );
Intent. addCategory (Intent. CATEGORY_LAUNCHER );
ComponentName componentName = new ComponentName (activityPackageName, className );

Intent. setComponent (componentName );
StartActivity (intent );
}

}

}

 

Main. xml is as follows:

[Html]
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
>
 
<Button
Android: id = "@ + id/button"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "click me"
Android: layout_centerInParent = "true"
/>
 
</RelativeLayout>

<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
>

<Button
Android: id = "@ + id/button"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "click me"
Android: layout_centerInParent = "true"
/>

</RelativeLayout>
The TestIntent project is as follows:

MainActivity is as follows:

[Java]
Package cc. testintent;
Import android. OS. Bundle;
Import android. app. Activity;
 
Public class MainActivity extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
}
 
}

Package cc. testintent;
Import android. OS. Bundle;
Import android. app. Activity;

Public class MainActivity extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
}

}

 

Main. xml is as follows:

[Html]
<RelativeLayout
Xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
>
 
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "Hello everyone"
Android: layout_centerInParent = "true"
/>
 
</RelativeLayout>

<RelativeLayout
Xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
>

<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "Hello everyone"
Android: layout_centerInParent = "true"
/>

</RelativeLayout>
AndroidManifest. xml is as follows:

[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "cc. testintent"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
 
<Uses-sdk
Android: minSdkVersion = "8"
Android: targetSdkVersion = "8"/>
 
<Application
Android: allowBackup = "true"
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name"
Android: theme = "@ style/AppTheme">
<Activity
Android: name = "cc. testintent. MainActivity"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Data android: scheme = "app"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
</Application>
 
</Manifest>

<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "cc. testintent"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">

<Uses-sdk
Android: minSdkVersion = "8"
Android: targetSdkVersion = "8"/>

<Application
Android: allowBackup = "true"
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name"
Android: theme = "@ style/AppTheme">
<Activity
Android: name = "cc. testintent. MainActivity"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Data android: scheme = "app"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
</Application>

</Manifest>
 

 

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.