您的位置:首页 > 产品设计 > UI/UE

关于重新系统下载管理器中使用枚举出现的问题

2017-01-11 17:22 387 查看
最近在项目上遇到一个很特别的问题,当我们把测试通过sdk提供给第三方接入以后出现的问题。

他们的sdk通过代理的方式来调用我们sdk里边的services,provider,broadcaset。

具体调用方式是这样:

1、他们创建自己的service,provider,broadcast相关的类。然后实现里边必要的方法,在必要的方法中通过new 一个我们相应的service等的类来调用我们的方法

2、在清单文件中注册他们自己的service,provider,broadcast

3、然后通过隐式启动实现,代理service,provider,broadcast的实现。

具体代码:

service:

package com.baidu.tbadk.plugins.gameCenter;

import android.app.Service;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.IBinder;

import com.duoku.platform.download.DownloadService;

public class GameCenterDownloadService extends Service {

@Override
public IBinder onBind(Intent intent) {
if (GameCenterPlugin.getDownloadService() != null) {
return GameCenterPlugin.getDownloadService().onBind(intent);
}
return null;
}

@Override
public void onCreate() {
super.onCreate();
Service proxyProvider = this;
GameCenterPlugin.setDownloadService(new DownloadService(proxyProvider));
if (GameCenterPlugin.getDownloadService() != null) {
GameCenterPlugin.getDownloadService().onCreate();
}
}

@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
if (GameCenterPlugin.getDownloadService() != null) {
GameCenterPlugin.getDownloadService().onStart(intent, startId);
}
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (GameCenterPlugin.getDownloadService() != null) {
return GameCenterPlugin.getDownloadService().onStartCommand(intent, flags, startId);
}
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onRebind(Intent intent) {
super.onRebind(intent);
if (GameCenterPlugin.getDownloadService() != null) {
GameCenterPlugin.getDownloadService().onRebind(intent);
}
}

@Override
public boolean onUnbind(Intent intent) {
if (GameCenterPlugin.getDownloadService() != null) {
return GameCenterPlugin.getDownloadService().onUnbind(intent);
}
return super.onUnbind(intent);
}

@Override
public void onDestroy() {
super.onDestroy();
if (GameCenterPlugin.getDownloadService() != null) {
GameCenterPlugin.getDownloadService().onDestroy();
}
}

@Override
public void onLowMemory() {
super.onLowMemory();
if (GameCenterPlugin.getDownloadService() != null) {
GameCenterPlugin.getDownloadService().onLowMemory();
}
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (GameCenterPlugin.getDownloadService() != null) {
GameCenterPlugin.getDownloadService().onConfigurationChanged(newConfig);
}
}
}


provider :

package com.baidu.tbadk.plugins.gameCenter;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;

import com.duoku.platform.download.DownloadProvider;

public class GameCenterDownloadProvider extends ContentProvider {

private boolean isPluginSetted = false;

@Override
public boolean onCreate() {
return false;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
setPlugin();
if (GameCenterPlugin.getDownloadProvider() != null) {
return GameCenterPlugin.getDownloadProvider().query(uri, projection, selection, selectionArgs, sortOrder);
}
return null;
}

@Override
public String getType(Uri uri) {
setPlugin();
if (GameCenterPlugin.getDownloadProvider() != null) {
return GameCenterPlugin.getDownloadProvider().getType(uri);
}
return null;
}

@Override
public Uri insert(Uri uri, ContentValues values) {
setPlugin();
if (GameCenterPlugin.getDownloadProvider() != null) {
return GameCenterPlugin.getDownloadProvider().insert(uri, values);
}
return null;
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
setPlugin();
if (GameCenterPlugin.getDownloadProvider() != null) {
return GameCenterPlugin.getDownloadProvider().delete(uri, selection, selectionArgs);
}
return 0;
}

@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
setPlugin();
if (GameCenterPlugin.getDownloadProvider() != null) {
return GameCenterPlugin.getDownloadProvider().update(uri, values, selection, selectionArgs);
}
return 0;
}

private void setPlugin() {
if (!isPluginSetted) {
ContentProvider proxyProvider = this;
GameCenterPlugin.setDownloadProvider(new DownloadProvider(proxyProvider));
if (GameCenterPlugin.getDownloadProvider() != null) {
GameCenterPlugin.getDownloadProvider().onCreate();
isPluginSetted = true;
}
}
}
}


broadcast:

package com.baidu.tbadk.plugins.gameCenter;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class GameCenterDownloadReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
if (GameCenterPlugin.getDownloadReceiver() != null) {
GameCenterPlugin.getDownloadReceiver().onReceive(context, intent);
}
}
}


还有一个代理类:

package com.baidu.tbadk.plugins.gameCenter;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ContentProvider;

/**
* Created by zhangmingzhe01 on 16/12/29.
*/

public class GameCenterPlugin {
private static Service mDownloadService;
private static ContentProvider mDownloadProvider;
private static BroadcastReceiver mDownloadReceiver;
private static BroadcastReceiver mAppMonitorReceiver;

public static Service getDownloadService() {
return mDownloadService;
}

public static void setDownloadService(Service downloadService) {
mDownloadService = downloadService;
}

public static ContentProvider getDownloadProvider() {
return mDownloadProvider;
}

public static void setDownloadProvider(ContentProvider downloadProvider) {
mDownloadProvider = downloadProvider;
}

public static BroadcastReceiver getDownloadReceiver() {
return mDownloadReceiver;
}

public static void setDownloadReceiver(BroadcastReceiver downloadReceiver) {
mDownloadReceiver = downloadReceiver;
}

public static BroadcastReceiver getAppMonitorReceiver() {
return mAppMonitorReceiver;
}

public static void setAppMonitorReceiver(BroadcastReceiver appMonitorReceiver) {
mAppMonitorReceiver = appMonitorReceiver;
}
}


在清单文件的注册:

<!-- 游戏下载service -->
<service android:name="com.baidu.tbadk.plugins.gameCenter.GameCenterDownloadService">
<intent-filter>
<action android:name="com.baidu.tieba.gameCenter.DownloadService"/>
</intent-filter>
</service>

<!-- 监听安装完成以后更新数据库中状态 -->
<receiver android:name="com.baidu.tbadk.plugins.gameCenter.GameCenterAppMonitorReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<action android:name="android.intent.action.PACKAGE_CHANGED"/>

<data android:scheme="package"/>
</intent-filter>
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MEDIA_MOUNTED"/>
<action android:name="android.intent.action.MEDIA_UNMOUNTED"/>

<data android:scheme="file"/>
</intent-filter>
</receiver>

<receiver
android:name="com.baidu.tbadk.plugins.gameCenter.GameCenterDownloadReceiver"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MEDIA_MOUNTED"/>

<data android:scheme="file"/>
</intent-filter>
</receiver>

<!-- 需修改android:authorities="packagename"替换packagename为应用包名, 如:android:authorities="com.baidu.test" -->
<provider
android:name="com.baidu.tbadk.plugins.gameCenter.GameCenterDownloadProvider"
android:authorities="com.example.demo"
android:exported="false"/>


这样就实现了不需要注册我们的Service或者provider等的组件,注册他们的就可以。

现在说下另外一个问题就是,关于枚举的,如果不是十分了解的话,可以看下这个地址:http://blog.csdn.net/ucxiii/article/details/48708455

因为枚举里边的一个函数 values()

这个方法可以返回当前枚举的类的一个数组。

这个方法是在编译器编译以后插入进去的,默认是没有的,如果打包平台或者打包工具没有对枚举的类编译以及插入values()函数

那么在使用这个函数的时候,返回值是null,会导致很多问题。

现在使用热修复。或者动态加载的应用越来越多,也需要关注这个问题。

如果有人在使用代理有问题,可以留言。

下边是只供参考的Demo:http://download.csdn.net/detail/u012808234/9735043
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐