安卓(Android) 怎么检查备份(backup)代理(Agent)

2019年10月19日11:57:10安卓(Android) 怎么检查备份(backup)代理(Agent)已关闭评论 523 2305字阅读7分41秒

1.简介

安卓(Android) 怎么检查备份(backup)代理(Agent)?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */



import android.content.Context;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.util.Log;

public class Main {
    private static final String TAG = "kk-Backup-Helper";
    private static Context mContext = null;

    private static void checkBackupAgent() {
        if (null == mContext) {
            return;
        }

        boolean haveBackupAgent = false;
        {
            final ApplicationInfo appInfo = mContext.getApplicationInfo();
            final String backupAgentName = appInfo.backupAgentName; 
            Log.d(TAG, "backupAgentName=" + backupAgentName);
            if (null != appInfo.backupAgentName) {
                {
                    Class<?> clazz = null;
                    try {
                        clazz = Class.forName(appInfo.backupAgentName);
                        haveBackupAgent = true;
                    } catch (ClassNotFoundException ex) {
                        Log.d(TAG, "" + ex);
                    }
                    if (false == haveBackupAgent) {
                        try {
                            if (appInfo.backupAgentName.startsWith(".")) {
                                clazz = Class.forName(appInfo.packageName
                                        + appInfo.backupAgentName);
                            } else {
                                clazz = Class.forName(appInfo.packageName
                                        + "." + appInfo.backupAgentName);
                            }
                            haveBackupAgent = true;
                        } catch (ClassNotFoundException ex) {
                            Log.d(TAG, "" + ex);
                        }
                    }
                }
            }
        }

        if (false == haveBackupAgent) {
            final ApplicationInfo appInfoFromPM = getApplicationInfoFromPackageManager(0);
            if (null != appInfoFromPM) {
                Log.d(TAG, "backupAgentName from PackageManager="
                        + appInfoFromPM.backupAgentName);
                if (null != appInfoFromPM.backupAgentName) {
                    {
                        Class<?> clazz = null;
                        try {
                            clazz = Class
                                    .forName(appInfoFromPM.backupAgentName);
                            haveBackupAgent = true;
                        } catch (ClassNotFoundException ex) {
                            Log.d(TAG, "" + ex);
                        }
                        if (false == haveBackupAgent) {
                            try {
                                if (appInfoFromPM.backupAgentName
                                        .startsWith(".")) {
                                    clazz = Class
                                            .forName(appInfoFromPM.packageName
                                                    + appInfoFromPM.backupAgentName);
                                } else {
                                    clazz = Class
                                            .forName(appInfoFromPM.packageName
                                                    + "."
                                                    + appInfoFromPM.backupAgentName);
                                }
                                haveBackupAgent = true;
                            } catch (ClassNotFoundException ex) {
                                Log.d(TAG, "" + ex);
                            }
                        }
                    }
                }
            }
        }

        if (false == haveBackupAgent) {
            Log.e(TAG,
                    "AndroidManifest.xml doesn't contain\n"
                            + "<application android:backupAgent=\"xxxxx\"/>\n"
                            + "format of class-name similar to '<application><activity android:name>'");
        }

    }

    private static ApplicationInfo getApplicationInfoFromPackageManager(
            final int flags) {
        assert null != mContext;

        if (null == mContext) {
            return null;
        }

        final PackageManager pm = mContext.getPackageManager();
        if (null == pm) {
            return null;
        }

        ApplicationInfo appInfo = null;
        try {
            appInfo = pm.getApplicationInfo(mContext.getPackageName(),
                    flags);
        } catch (PackageManager.NameNotFoundException e) {

        }

        return appInfo;
    }
}

 
继续阅读
编程十万个怎么办