Android : how to create componentName in code using <activity-alias>

若如初见. 提交于 2019-12-04 16:42:46
vallllll

I have found the solution using this code

String packageName = getPackageName();
try {
    PackageInfo p = getApplicationContext().getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
    for (ActivityInfo activityInfo : p.activities) {
        if(log.d()) log.d("ACT " + activityInfo.name+" "+activityInfo.packageName);
    }
} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
}

if your alias is "alias"

ComponentName componentWithoutTextFiles = new ComponentName(packageName, packageName".alias");

Solution given by @vallllll worked fine, i tried to upvote or to comment , but i'm not able to do as still needs reputation points.

I tried a lot of other solutions in other questions and didn't work. Solution given here was the only one worked for me

Problem i was facing that after migrating code from sdk 18 to sdk 26, i was getting this error Component class x does not exist in Y , while calling setComponentEnabledSetting when switching from Activity to Alias-Activity Old code

      getPackageManager().setComponentEnabledSetting(
   new ComponentName(pkg name, alias name),
   PackageManager.COMPONENT_ENABLED_STATE_ENABLED,    PackageManager.DONT_KILL_APP);

New code

getPackageManager().setComponentEnabledSetting(
        new ComponentName(this, pkg name + alias name,
        PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

Two changes were made, 1- change ComponentName constructor to use version that accepts context and class name instead of the constructor version using pkg name , class name 2- use fully qualified alias name + To get fully qualified alias name, i used the following code

String packageName = getPackageName();
    try {
        PackageInfo p =         getApplicationContext().getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
        for (ActivityInfo activityInfo : p.activities) {
            Log.d(LOG_TAG, "ACT " + activityInfo.name+" "+activityInfo.packageName );
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!