Android 如何從一個APP中開啟另外一個APP,androidapp

來源:互聯網
上載者:User

Android 如何從一個APP中開啟另外一個APP,androidapp

眾所周知,在一個APP內部,從一個頁面跳轉到另外一個頁面是使用startactivity函數來實現的。

同樣的,對於應用之間的跳轉也是如此的。應用直接的跳轉分為三種情況,為了方便描述,我加上有兩個應用A和B,需求是要從A點擊一個按鈕,可以跳轉到B,同時A要傳資料給B,B能夠接收並且處理:

1.從A跳轉到B的主Activity,代碼如下:

     

Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); ComponentName comp = new ComponentName("com.nbg.baby", "com.nbg.baby.MainActivity"); intent.setComponent(comp);int launchFlags = Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED; intent.setFlags(launchFlags); intent.setAction("android.intent.action.VIEW"); Bundle bundle = new Bundle(); bundle.putString("from", "來自測試應用"); intent.putExtras(bundle); startActivity(intent);

    1.1如果B是應用是關閉的,那麼在onCreate()函數中增加如下語句:

 Bundle bundle = this.getIntent().getExtras();          if(bundle!=null && bundle.getString("from")!=null){          Util.showToast(s_instance, bundle.getString("from"), Toast.LENGTH_LONG);          }

    1.2 如何B應用是開啟狀態,那麼在onRestart()函數中增加如下語句:

 Bundle bundle = this.getIntent().getExtras();          if(bundle!=null && bundle.getString("from")!=null){          Util.showToast(s_instance, bundle.getString("from"), Toast.LENGTH_LONG);          }

2.從A跳轉到B的某個被設定為exported的activity,B應用的狀態無關緊要,代碼如下:

<span style="white-space:pre"></span>ComponentName comp = new ComponentName("com.nbg.baby", "com.nbg.baby.ApiLoginActivity"); Intent intent = new Intent();intent.setComponent(comp); intent.setAction(Intent.ACTION_VIEW); Bundle bundle = new Bundle(); bundle.putString("extra", "我來自星星"); intent.putExtras(bundle);context.startActivityForResult(intent, 1);



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.