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>