Android Tv Launcher中碰到安装或者卸载应用需要刷新界面:
1.Receiver
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
/**
* Created by zxf on 2017/7/12 0012.
*/
public class PackageChangeReceiver extends BroadcastReceiver {
private PackageChangeListener listener;
@Override
public void onReceive(Context context, Intent intent) {
if (listener != null){
listener.packageChange();
}
}
//接口 方便操作
interface PackageChangeListener {
void packageChange();
}
public void OnPackageChangeListener(PackageChangeListener listener){
this.listener = listener;
}
}
有接口的广播接收器,只能动态注册,不能静态注册!!!
mReceiver = new PackageChangeReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(Utils.PACKAGE_ADD);
filter.addAction(Utils.PACKAGE_REMOVE);
filter.addAction(Utils.PACKAGE_REPLACE);
//隐式意图 需匹配Data
filter.addDataScheme("package");
registerReceiver(mReceiver,filter);
//广播Action
//安装应用
public static final String PACKAGE_ADD ="android.intent.action.PACKAGE_ADDED";
//卸载应用
public static final String PACKAGE_REMOVE = "android.intent.action.PACKAGE_REMOVED";
//更新应用
public static final String PACKAGE_REPLACE = "android.intent.action.PACKAGE_REPLACED";
//接口的回调
mReceiver.OnPackageChangeListener(new PackageChangeReceiver.PackageChangeListener() {
@Override
public void packageChange() {
//当应用程序包发生变化时 刷新应用列表
refreshData();
}
});
最后不要忘记
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mReceiver);
}
转载请注明出处:
http://blog.csdn.net/hulk_zhang/article/details/75004913
版权声明:本文为Hulk_zhang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。