Android Studio / IntelliJ IDEA集成SDK

1.aar依赖集成

将下载的SDK中的android_studio/libs/iappease_v_xxx.aar复制到项目工程libs目录下。


      
├── build.gradle
├── libs
│   └── iappease_v_xxx.aar
└── src
    └── main
 

编辑项目的build.gradle文件,加入本地aar依赖支持:

repositories {
    flatDir {
        dirs 'libs'
    }
}
dependencies {
    compile(name: 'iappease_v_xxx', ext: 'aar')
}

如果使用level 23+编译打包,为了保持低版本Android系统兼容性,需要加入apache支持:

android {
  useLibrary  'org.apache.http.legacy'
}

在res下新建xml目录,按以下目录结构创建资源文件,并将相应代码复制到文件中,注意替换yourpackage


      
├── res
    └── xml
        ├── ixintui_deamon.xml
        └── ixintui_sync.xml
 

ixintui_sync.xml


      
1
2
3
4
5
6
7
8
9
10
<!-- 请把**yourpackage**替换成您的应用包名 -->
<?xml version="1.0" encoding="utf-8"?>
<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
    android:accountType="com.ixintui.push.account"
    android:allowParallelSyncs="true"
    android:contentAuthority="**yourpackage**.provider"
    android:isAlwaysSyncable="true"
    android:supportsUploading="false"
    android:userVisible="true" />
 

ixintui_deamon.xml


      
1
2
3
4
5
<?xml version="1.0" encoding="utf-8"?>
<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
    android:accountType="com.ixintui.push.account"
    android:label="@string/app_name" />
 

2.配置AndroidManifest.xml文件


      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<!-- 请确保您的应用具有以下Permission权限 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.REORDER_TASKS" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>
 
<!-- 自定义一个Receiver,用来接收透传、api结果以及点击事件 -->
 
<!-- 请把**xxx.xxx**替换成您PushReceiver所对应的包名 -->
<receiver android:name="**xxx.xxx**.PushReceiver">
    <intent-filter>
        <!-- 接收透传消息 -->
        <action android:name="com.ixintui.action.MESSAGE"/>
        <!-- 接收register, add tag等反馈消息 -->
        <action android:name="com.ixintui.action.RESULT"/>
        <!-- 接收notification click消息 -->
        <action android:name="com.ixintui.action.notification.CLICK"/>
    </intent-filter>
</receiver>
 
<!-- 以下为push使用的活动,服务和接收器 -->
<activity android:name="com.ixintui.push.PushActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:configChanges="orientation|keyboardHidden">
    <intent-filter>
        <action android:name="com.ixintui.push.PushActivity" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>
 
<service
    android:name="com.ixintui.push.PushService"
    android:exported="true"
    android:process=":ixintui_service_v1">
    <intent-filter>
        <action android:name="android.content.SyncAdapter" />
    </intent-filter>
    <meta-data
        android:name="android.content.SyncAdapter"
        android:resource="@xml/ixintui_sync" />
</service>
 
<service android:name="com.ixintui.push.MediateService" android:exported="true"></service>
 
<receiver android:name="com.ixintui.push.Receiver">
    <intent-filter>
        <action android:name="com.ixintui.action.BROADCAST" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_REMOVED"/>
        <data android:scheme="package" />
    </intent-filter>
    <intent-filter >
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
</receiver>
 
<activity
    android:name="com.ixintui.push.RichpushActivity"
    android:theme="@android:style/Theme.Translucent">
</activity>
<service android:name="com.ixintui.push.DeamonService">
    <intent-filter>
        <action android:name="android.accounts.AccountAuthenticator" />
    </intent-filter>
    <meta-data
        android:name="android.accounts.AccountAuthenticator"
        android:resource="@xml/ixintui_deamon" />
</service>
 
<!-- 请把**yourpackage**替换成您的应用包名 -->
<provider
    android:name="com.ixintui.push.Provider"
    android:exported="false"
    android:multiprocess="true"
    android:authorities="**yourpackage**.ixintui.push.provider"/>
<!-- 请把**yourpackage**替换成您的应用包名 -->
<provider
    android:name="com.ixintui.push.SyncProvider"
    android:authorities="**yourpackage**.provider"
    android:exported="false"
    android:syncable="true" />
 

3.代码集成

方法详解见API介绍

3.1 初始化

在Application的onCreate()中调用初始化接口:


      
1
2
3
4
5
6
7
/*
 *context: this
 *appkey: 在云管平台申请的Appkey
 *channel: 渠道
 */
BundleManager.getInstance().init(context, appkey, channel);
 

3.2 创建本地PushReceiver

该receiver的功能如下:

1.拦截热更新消息并处理。


      
1
2
boolean BundleLiteHelper.isBundleInfo(msg);
 

      
1
2
BundleLite BundleLiteHelper.toBundleLite(msg);
 

      
1
2
void BundleManager.getInstance().downloadBundle(lite);
 

2.并接收推送消息(详见推送开发API)。 代码示例:


      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
 * 监听push消息
 */
public class PushReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(SdkConstants.MESSAGE_ACTION)) {
            String msg = intent.getStringExtra(SdkConstants.MESSAGE);
            String extra = intent.getStringExtra(SdkConstants.ADDITION);
 
            Log.d("demo", "message received, msg is: " + msg + ", extra: " + extra);
            //判断是否为热更新push消息
            if (BundleLiteHelper.isBundleInfo(msg)) {
                //热更新透传消息处理
                BundleLite lite = null;
                try {
                    lite = BundleLiteHelper.toBundleLite(msg);
                    BundleManager.getInstance().downloadBundle(lite);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                //TODO 非热更新消息处理
            }
        }
    }
}
 

3.3 创建初始化InitReceiver

需要创建一个用于接收初始化插件广播的Receiver,在onCreate()方法中注册该Receiver给BundleManager


      
1
2
BundleManager.getInstance().registerReceiver(initReceiver, intenFilter);
 

退出时在onDestroy()方法中注销:


      
1
2
BundleManager.getInstance().unregisterReceiver(initReceiver);
 

代码示例:


      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
BroadcastReceiver initReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action == Constant.ACTION_BUNDLE_LOAD_SUCCESS) {
            List<BundleLite> list = (List<BundleLite>) intent.getSerializableExtra(Constant.BUNDLE_KEY_VALUES);
            if (list != null && !list.isEmpty()) {
                /*
                可以根据BundleLite判断加载不同插件
                 */
                BundleManager.getInstance().startBundle(MainActivity.this, list.get(0));
                finish();
            }
        } else if (action == Constant.ACTION_BUNDLE_IS_EMPTY) {
            Toast.makeText(MainActivity.this, "当前无可用插件", Toast.LENGTH_SHORT).show();
        }
    }
};
 

3.4 启动BundleManager

步骤3.3完成后调用该方法启动BundleManager:


      
1
2
BundleManager.getInstance().launch(context);
 

步骤3.3和3.4完整代码:


      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class MainActivity extends AppCompatActivity {
 
    private BundleLite newGame;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        BundleManager.getInstance().registerReceiver(initReceiver, createInitFilter());
        BundleManager.getInstance().launch(this);
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        BundleManager.getInstance().unregisterReceiver(initReceiver);
    }
 
    BroadcastReceiver initReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action == Constant.ACTION_BUNDLE_LOAD_SUCCESS) {
                List<BundleLite> list = (List<BundleLite>) intent.getSerializableExtra(Constant.BUNDLE_KEY_VALUES);
                if (list != null && !list.isEmpty()) {
                    /*
                    可以根据BundleLite判断加载不同插件
                     */
                    BundleManager.getInstance().startBundle(MainActivity.this, list.get(0));
                    finish();
                }
            } else if (action == Constant.ACTION_BUNDLE_IS_EMPTY) {
                Toast.makeText(MainActivity.this, "当前无可用插件", Toast.LENGTH_SHORT).show();
            }
        }
    };
 
    private IntentFilter createInitFilter() {
        IntentFilter iff = new IntentFilter();
        iff.addAction(Constant.ACTION_BUNDLE_LOAD_SUCCESS);
        iff.addAction(Constant.ACTION_BUNDLE_IS_EMPTY);
        return iff;
    }
}
 

3.5 测试

到这一步骤,集成已完成,您可以到云管理平台上传插件并发布,再次打开App插件加载成功,具体步骤参见开发者后台使用指南

附:监听下载进度

您可以本地新建一个downloadreceiver用于监听下载进度:

示例代码: 注册:


      
1
2
BundleManager.getInstance().registerReceiver(downloadreceiver, createDownloadFilter());
 

注销:


      
1
2
BundleManager.getInstance().unregisterReceiver(downloadreceiver);
 

receiver:


      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
BroadcastReceiver downloadreceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(Constant.ACTION_BUNDLE_DOWNLOAD_COMPLETE)) {
           // 可以获取下载到的BundleLite
           BundleLite lite = (BundleLite) intent.getExtras().get(Constant.BUNDLE_KEY_VALUE);
           Toast.makeText(context, "插件下载开始", Toast.LENGTH_SHORT).show();
        } else if (action.equals(Constant.ACTION_BUNDLE_DOWNLOAD_START)) {
            Toast.makeText(context, "插件下载开始", Toast.LENGTH_SHORT).show();
        } else if (action.equals(Constant.ACTION_BUNDLE_DOWNLOAD_ING)) {
            Toast.makeText(context, "插件正在下载", Toast.LENGTH_SHORT).show();
        } else if (action.equals(Constant.ACTION_BUNDLE_DOWNLOAD_ERROR)) {
            Toast.makeText(context, "插件下载异常", Toast.LENGTH_SHORT).show();
        }
    }
};
 

createDownloadFilter():


      
1
2
3
4
5
6
7
8
9
10
private IntentFilter createDownloadFilter() {
    IntentFilter iff = new IntentFilter();
    iff.addAction(Constant.ACTION_BUNDLE_DOWNLOAD_COMPLETE);
    iff.addAction(Constant.ACTION_BUNDLE_DOWNLOAD_ERROR);
    iff.addAction(Constant.ACTION_BUNDLE_DOWNLOAD_ING);
    iff.addAction(Constant.ACTION_BUNDLE_DOWNLOAD_START);
    iff.addAction(Constant.ACTION_BUNDLE_EXIST);
    return iff;
}
 

说明:详细指南见插件开发API