Android app的多个快捷方式

多个入口进入MainActivity.java中,需要在清单文件中定义activity-alias属性;


    

    
        
            
                
                
            
        

        
            
                
                
                
            
        
        
            
                
                
                
            
        
    

Activity中的代码:

public class MainActivity extends Activity {

    private final String ACTIVITY_ALIAS_1 = "com.example.dynamicchangeicon.ActivityAlias1";
    private final String ACTIVITY_ALIAS_2 = "com.example.dynamicchangeicon.TestAlias";

    private SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        preferences = getSharedPreferences("DynamicChangeIcon",
                Context.MODE_PRIVATE);
        preferences.edit().putInt("ikey", 1).commit();

    }

    private void changeIcon(String msg) {
        //msg 就是在清单文件给activity-alias定制的name属性:
        // android:name="com.example.dynamicchangeicon.ActivityAlias1"
        
        PackageManager pm = getApplicationContext().getPackageManager();
        ActivityManager am = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);

        System.out.println(getComponentName());
        // 去除旧图标,不去除的话会出现2个App图标
        pm.setComponentEnabledSetting(getComponentName(),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
        if(msg.equals(ACTIVITY_ALIAS_1)){
            // 显示新图标
            pm.setComponentEnabledSetting(new ComponentName(getBaseContext(),
                    ACTIVITY_ALIAS_1),
                    PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                    PackageManager.DONT_KILL_APP);
            pm.setComponentEnabledSetting(new ComponentName(getBaseContext(),
                    ACTIVITY_ALIAS_2),
                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                    PackageManager.DONT_KILL_APP);
        }else{
            pm.setComponentEnabledSetting(new ComponentName(getBaseContext(),
                    ACTIVITY_ALIAS_2),
                    PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                    PackageManager.DONT_KILL_APP);
            pm.setComponentEnabledSetting(new ComponentName(getBaseContext(),
                    ACTIVITY_ALIAS_1),
                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                    PackageManager.DONT_KILL_APP);
        }
        

        Intent i = new Intent(Intent.ACTION_MAIN);
        i.addCategory(Intent.CATEGORY_HOME);
        i.addCategory(Intent.CATEGORY_DEFAULT);
        List resolves = pm.queryIntentActivities(i, 0);
        for (ResolveInfo res : resolves) {
            if (res.activityInfo != null) {
                am.killBackgroundProcesses(res.activityInfo.packageName);
            }
        }
    }

    public void changeIcon(View v) {
        int key = preferences.getInt("ikey", 0);
        switch (key) {
        case 1:
            changeIcon(ACTIVITY_ALIAS_1);
            preferences.edit().putInt("ikey", 2).commit();
            break;
        case 2:
            changeIcon(ACTIVITY_ALIAS_2);
            preferences.edit().putInt("ikey", 1).commit();
            break;
        }

    }
}

你可能感兴趣的:(Android app的多个快捷方式)