Android應用間跳轉

來源:互聯網
上載者:User

標籤:

實現原理:比較簡單,主要是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     @Override14     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跳轉前調用用來檢測是否有合格activity36           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             @Override52             public void onClick(View v) {53                 startActivity(intent);54             }55         });56     }57 }

 

Android應用間跳轉

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.