动态更换应用Icon?没有搞错吧,没有啦,你要是仔细的话,你会发现淘宝已有节日都会进行更换图标?不信的话下一年的11.11或12.12,你观察一下。这样来说,那是可以换啦?
那问题来了怎么换?
1、想到这个<activity-alias>你点击可以查看官方表述,简单说:为目标Activity指定别名,可以实现多个入口图标。相信做过系统Setting、Launcher和插件开发的开发者在系统的源码中应该见过很多。
2、PackageManager中有个setComponentEnabledSetting方法,这个做插件的时候会用到的,例如隐藏插件的桌面图标,它还可以禁用/开启Activity、Service、BroadcastReceiver。
public void setComponentEnabledSetting (ComponentName componentName, int newState, int flags)
componentName:组件名称
newState:组件新的状态,可以设置三个值,分别是如下:
不可用状态:COMPONENT_ENABLED_STATE_DISABLED
可用状态:COMPONENT_ENABLED_STATE_ENABLED
默认状态:COMPONENT_ENABLED_STATE_DEFAULT
flags:行为标签,值可以是DONT_KILL_APP或者0。 0说明杀死包含该组件的app
有这两个知识点,那接下来我们进行实现动态换桌面图标吧。
动态Launcher Icon
1、在AndroidMainifest中实现
<activity android:name=".ActivityMain" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity-alias android:name=".ABenchMark" android:enabled="false" android:icon="@mipmap/app_icon" android:label="安兔兔完整版" android:targetActivity=".ActivityMain"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity-alias>
2、Activity中的代码实现
public class ActivityMain extends AppCompatActivity{
private static final String TAG = ActivityMain.class.getSimpleName();
private ComponentName mDefault;
private ComponentName mABenchMark;
private PackageManager mPm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
initView();
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void initView(){
mPm = getApplicationContext().getPackageManager();
//mDefault = getComponentName();
mDefault = new ComponentName(getBaseContext(), "com.aoaoyi.chanageicon.ActivityMain");
Log.i(TAG, mDefault.toString());
mABenchMark = new ComponentName(getBaseContext(), "com.aoaoyi.chanageicon.ABenchMark");
findViewById(R.id.main_change).setOnClickListener((v)->{
v.getId();
Log.i(TAG, "change icon");
changeIcon();
});
findViewById(R.id.main_reset).setOnClickListener((v)->{
v.getId();
Log.i(TAG, "rest icon");
resetIcon();
});
new Thread(()->{
Log.e("","");
} ).start();
/*List _List = Arrays.asList("1", "2", "3", "4", "5", "6", "7");
_List.forEach(s ->{
});*/
}
private void changeIcon() {
disableComponent(mDefault);
enableComponent(mABenchMark);
}
private void resetIcon() {
disableComponent(mABenchMark);
enableComponent(mDefault);
}
private void enableComponent(ComponentName componentName) {
mPm.setComponentEnabledSetting(componentName,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
private void disableComponent(ComponentName componentName) {
mPm.setComponentEnabledSetting(componentName,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
}
OK了,禁用默认的Activity后,启用想启用的activity-alias,结果不变还是指向了默认的Activity,但图标已经发生了改变。
根据ROM的不同,在禁用了组件之后,会等一会,Launcher会自动刷新图标
如下效果图:
移动端gif转换还是个短板,我再研究怎么高效转换成Gif。
文章评论
Heya i am for the primary time here. I found this board and I find It really useful & it
helped me out a lot. I am hoping to give one thing back
and help others such as you aided me.