Android 自定义应用抽屉Launcher

import android.widget.AdapterView.AdapterContextMenuInfo;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.EditText;

import android.widget.GridView;

import android.widget.Toast;

public class Main extends Activity {

static DbOperation db = null;

static List appInfo = null;

PackageManager packageManager = null;

int orderFlag = 0; // 排序方式标志,0-默认排序,1-名字排序,2-时间排序

static boolean isEqual = false;

List apps = null;

List apps2 = null;

private static final String TAG = “ScrollLayoutTest”;

private ScrollLayout mScrollLayout;

private static final float APP_PAGE_SIZE = 36.0f;

private Context mContext;

int upCount;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

mContext = this;

setContentView(R.layout.main);

mScrollLayout = (ScrollLayout) findViewById(R.id.ScrollLayoutTest);

initViews();

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// TODO Auto-generated method stub

menu.add(0, 1, 1, “● 默认排序[当前]”);

menu.add(0, 2, 1, “○ 名称排序”);

menu.add(0, 3, 1, “○ 时间排序”);

menu.add(0, 4, 1, " 关于应用");

menu.add(0, 5, 1, " 更新桌面");

return super.onCreateOptionsMenu(menu);

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {

case 1: // 默认排序[当前]

break;

case 2: // 名称排序

Intent intent1 = new Intent(this, DeskByName.class);

startActivity(intent1);

break;

case 3: // 按安装时间排序

Intent intent2 = new Intent(this, DeskByTime.class);

startActivity(intent2);

break;

case 4:// 关于

Intent intent3 = new Intent(this, About.class);

startActivity(intent3);

break;

case 5:// 更新桌面

onCreate(null);

break;

default:

}

return super.onOptionsItemSelected(item);

}

public void initViews() {

packageManager = getPackageManager();

final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);

mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

List<HashMap<String, Object>> items = new ArrayList<HashMap<String, Object>>();

// get all apps

apps = packageManager.queryIntentActivities(mainIntent, 0);

for (ResolveInfo pi : apps) {

HashMap<String, Object> map = new HashMap<String, Object>();

PackageManager manager = this.getPackageManager();

map.put(“appIcon”, pi.loadIcon(packageManager));

map.put(“appName”, pi.loadLabel(packageManager).toString());

map.put(“appPack”, pi.activityInfo.packageName);

try {

map.put(“appTime”, Long.toString(manager.getPackageInfo(

pi.activityInfo.packageName, 0).firstInstallTime));

} catch (NameNotFoundException e) {

e.printStackTrace();

}

items.add(map);

}

// ToDB

db = new DbOperation(this);

long countlong = db.getCount(); // 获取数据库中记录数目

int countint = (int) countlong;

upCount = items.size();// +++++++++++++++++++++++++++++++++++++++upCount

if (countint == items.size()) {

isEqual = true;

// 若记录数目与当前items.size()相等,则不插入应用信息,只插入时间

for (int m = 0; m < items.size(); m++) {

// 插入时间:

String appPack2 = (String) items.get(m).get(“appPack”);

try {

PackageManager manager = this.getPackageManager();

String appTime2 = Long.toString(manager.getPackageInfo(

appPack2, 0).firstInstallTime);

db.insertTime(appTime2, appPack2);

} catch (NameNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

} else {

// 若不等,则先清空表数据,然后再插入应用信息,然后插入时间

db.deleteTable();

for (int i = 0; i < items.size(); i++) {

String appName = (String) items.get(i).get(“appName”);

String appPack = (String) items.get(i).get(“appPack”);

String appTime = (String) items.get(i).get(“Time”);

App appInfo = new App(appName, appPack, appTime);

DbOperation db = new DbOperation(this);

db.insertApp(appInfo);

// 插入时间:

String appPack2 = (String) items.get(i).get(“appPack”);

try {

PackageManager manager = this.getPackageManager();

String appTime2 = Long.toString(manager.getPackageInfo(

appPack2, 0).firstInstallTime);

db.insertTime(appTime2, appPack2);

} catch (NameNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

// 读取数据库信息

db = new DbOperation(this);

if (orderFlag == 0) {

appInfo = db.query(“appTable”);

} else if (orderFlag == 1) {

appInfo = db.queryByName(“appTable”);

} else if (orderFlag == 2) {

appInfo = db.queryByTime(“appTable”);

}

final int PageCount = (int) Math.ceil(apps.size() / APP_PAGE_SIZE);

Log.e(TAG, “size:” + apps.size() + " page:" + PageCount);

for (int i = 0; i < PageCount; i++) {

GridView appPage = new GridView(this);

this.registerForContextMenu(appPage);

appPage.setAdapter(new AppAdapter(this, appInfo, i, apps, items));

appPage.setNumColumns(6);

appPage.setOnItemClickListener(listener);

mScrollLayout.addView(appPage);

}

}

public OnItemClickListener listener = new OnItemClickListener() {

public void onItemClick(AdapterView<?> parent, View view, int position,

long id) {

GridView listView = (GridView) parent;

App appInfo = (App) (listView.getItemAtPosition(position));

String packName = appInfo.getPack();

Intent mainIntent = mContext.getPackageManager()

.getLaunchIntentForPackage(packName);

mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

try {

mContext.startActivity(mainIntent);

} catch (ActivityNotFoundException noFound) {

Toast.makeText(mContext, “未发现该应用!”, Toast.LENGTH_SHORT).show();

}

}

};

public void onCreateContextMenu(ContextMenu menu, View v,

ContextMenuInfo menuInfo) {

menu.setHeaderTitle(“操作”);

menu.add(0, 1, Menu.NONE, “更名”);

menu.add(0, 2, Menu.NONE, “卸载”);

}

public boolean onContextItemSelected(MenuItem item) {

// 得到当前被选中的item信息

AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item

.getMenuInfo();

final int a = menuInfo.position;

switch (item.getItemId()) {

case 1:

final EditText e = new EditText(this);

new AlertDialog.Builder(this).setTitle(“更名”)

// .setIcon(R.drawable.logo2)

.setView(e)

.setPositiveButton(“确定”,

new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog,

int i) {

int b1 = mScrollLayout.getCurScreen();

int num1 = b1 * 36 + a;

String appName = appInfo.get(num1)

.getName();

System.out.println(appName);

String str = e.getText().toString();

System.out.println(str);

db.changeName(str, appName);

// 更新界面

onCreate(null);

}

}).setNegativeButton(“取消”, null).show();

break;

case 2:

int b1 = mScrollLayout.getCurScreen();

int num1 = b1 * 36 + a;

String appPack = appInfo.get(num1).getPack();

Uri uri = Uri.parse(“package:” + appPack);

System.out.println(appPack);

Intent intent = new Intent(Intent.ACTION_DELETE, uri);

intent.setData(uri);// 设置获取到的URI

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

最后是今天给大家分享的一些独家干货:

【Android开发核心知识点笔记】

【Android思维脑图(技能树)】

【Android核心高级技术PDF文档,BAT大厂面试真题解析】

【Android高级架构视频学习资源】

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)**

[外链图片转存中…(img-CFntbfAq-1713767943160)]

最后是今天给大家分享的一些独家干货:

【Android开发核心知识点笔记】

[外链图片转存中…(img-DVQvnoTT-1713767943162)]

【Android思维脑图(技能树)】

[外链图片转存中…(img-UbULodKJ-1713767943163)]

【Android核心高级技术PDF文档,BAT大厂面试真题解析】

[外链图片转存中…(img-iUJTwexI-1713767943163)]

【Android高级架构视频学习资源】

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值