首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計疑難解答

Android 多模塊項目整合:廣播實現組件間跳轉

     編輯:程式設計疑難解答

需求

兩個獨立 Android 項目(so、TestRotateAnim)合併為一個 APK,在 so 中點擊按鈕跳轉到 TestRotateAnim 功能頁。

方案:隱式廣播

  1. 接收方 在 TestRotateAnim 模塊註冊廣播接收器:
    <receiver android:name=".MyReceiver">
        <intent-filter>
            <action android:name="com.example.ACTION_START_ROTATE"/>
        </intent-filter>
    </receiver>
    
  2. 發送方 在 so 模塊按鈕點擊發送廣播:
    Intent intent = new Intent("com.example.ACTION_START_ROTATE");
    sendBroadcast(intent);
    
  3. 接收器中啟動目標 Activity:
    public class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Intent i = new Intent(context, TargetActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }
    }
    

優勢

模塊解耦,無需直接依賴對方類,支持動態擴展。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved