码迷,mamicode.com
首页 > 移动开发 > 详细

Android应用间跳转

时间:2015-06-09 16:41:11      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

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

 1 /**
 2  * 
 3 * @ClassName: MainActivity
 4 * @Description: 从一个应用跳转到另一个应用
 5 * @author guoyizhe
 6 * @email gyzboy@126.com
 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 }

 

Android应用间跳转

标签:

原文地址:http://www.cnblogs.com/gyzboy/p/4563774.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!