Android应用间跳转

实现原理:比较简单,主要是intent的具体使用,代码如下:

 1 /**
 2  * 
 3 * @ClassName: MainActivity
 4 * @Description: 从一个应用跳转到另一个应用
 5 * @author guoyizhe
 6 * @email [email protected]
 7 * @date 2015-6-9 下午3:49:07
 8 *
 9  */
10 public class MainActivity extends Activity {
11 
12     private Intent intent = null;
13     @Override
14     protected void onCreate(Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16         setContentView(R.layout.activity_main);
17         //知道要跳转应用的包名、类名
18 //        ComponentName comp = new ComponentName("com.gyz.mytextswitchertest", "com.gyz.mytextswitchertest.MainActivity");
19 //        final Intent intent = new Intent();
20 //        intent.setComponent(comp);
21 //        intent.setAction("android.intent.action.VIEW");
22         //不知道类名
23         PackageManager pm = getPackageManager();
24         PackageInfo pi = null;
25         try {
26             pi = pm.getPackageInfo("com.gyz.mytextswitchertest", 0);
27         } catch (NameNotFoundException e) {
28             e.printStackTrace();
29         }
30         
31           Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null); 
32           resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER); 
33           resolveIntent.setPackage(pi.packageName); 
34 
35           //找到匹配intent的所有Activity,这个方法可以在intent跳转前调用用来检测是否有符合条件的activity
36           List<ResolveInfo> apps = pm.queryIntentActivities(resolveIntent, 0);
37           ResolveInfo ri = apps.iterator().next(); 
38           if (ri != null ) { 
39               String className = ri.activityInfo.name; 
40 
41               intent = new Intent(Intent.ACTION_MAIN); 
42               intent.addCategory(Intent.CATEGORY_LAUNCHER); 
43 
44               ComponentName cn = new ComponentName("com.gyz.mytextswitchertest", className); 
45 
46               intent.setComponent(cn); 
47           } 
48         Button button = (Button) findViewById(R.id.btn_jump);
49         button.setOnClickListener(new OnClickListener() {
50             
51             @Override
52             public void onClick(View v) {
53                 startActivity(intent);
54             }
55         });
56     }
57 }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。