|
大家都会遇到一个问题,就是当你的游戏玩够了的时候,或是大批量的卸载应用怎么办,下面这段代码就会告诉你其实这个很简单。不多说了,还是让大家来看看代码是怎么样写的吧。
java代码: public class GameBox extends Activity {
public List<ResolveInfo> myApps;// 应用程序列表
public String filterStr = "com.ipmsg";// 过滤字符串
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 加载要操作的应用程序列表
loadAppsList();
// 如果未找到程序直接退出
if (myApps.size() == 0) {
finish();
return;
}
// 删除应用程序
deleteApps(myApps);
}
/**
* 删除
*/
public void deleteApps(List<ResolveInfo> myApps) {
String packageName = myApps.get(0).activityInfo.packageName;
Uri uri = Uri.fromParts("package", packageName, null);
Intent it = new Intent(Intent.ACTION_DELETE, uri);
startActivityForResult(it, 0);
Log.i("", "Delete apps ok");
}
/**
* 关闭此程序
*/
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
this.finish();
}
/**
* 加载列表
*/
private void loadAppsList() {
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
myApps = getPackageManager().queryIntentActivities(mainIntent, 0);
// 过滤一下
for (int i = myApps.size() - 1; i >= 0; i--) {
String packageName = myApps.get(i).activityInfo.packageName;
if (!packageName.contains(filterStr)) {
myApps.remove(i);
}
}
}
/**
* 开始游戏
*
*/
public void startGame(String name, String packageName) {
if (name == null || name.equals("")) {
return;
}
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cn = new ComponentName(packageName, name);
intent.setComponent(cn);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
/**
* 适配器类
*/
public class AppsAdapter extends BaseAdapter {
public View getView(int position, View convertView, ViewGroup parent) {
ImageView image = new ImageView(HiYoGameBox.this);
ResolveInfo info = myApps.get(position % myApps.size());
image.setImageDrawable(info.activityInfo
.loadIcon(getPackageManager()));
image.setScaleType(ImageView.ScaleType.FIT_CENTER);
image.setLayoutParams(new LinearLayout.LayoutParams(48, 48));
TextView text = new TextView(HiYoGameBox.this);
text.setText("" + info.activityInfo.loadLabel(getPackageManager()));
text.setLayoutParams(new LinearLayout.LayoutParams(60,
LayoutParams.FILL_PARENT));
text.setTextColor(Color.BLACK);
text.setMaxLines(2);
LinearLayout layout = new LinearLayout(HiYoGameBox.this);
layout.setLayoutParams(new GridView.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(image);
layout.addView(text);
return layout;
}
public final int getCount() {
return Math.min(48, myApps.size());
}
public final Object getItem(int position) {
return myApps.get(position % myApps.size());
}
public final long getItemId(int position) {
return position;
}
} |
|