安卓开发
最近在开发一个新项目,需要把应用改成桌面,并引导用户设置为默认桌面,完成后的效果如下图:
1.添加XML
<activity Android:name=".activitys.DeskTop" android:launchmode="singleTop">
<intent-filter>
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.MaiN"></action>
<category android:name="android.intent.category.LAUNCHER"></category>
</intent-filter>
</activity>
在主Activity里 添加
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
这样用户在点按Home键的时候,就会弹出对话框,提示选择新的默认桌面
2.主Activity布局文件
布局比较简单,线性布局嵌套gridview,下面留了一点空袭是为了腾出空间放置拨号、短信常用APP
<?xml version="1.0" encoding="utf-8"?>
<Linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/desktop_linear"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2">
<GridView
android:id="@+id/mgv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="4"
></GridView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="8"></LinearLayout>
</LinearLayout>
3.获取APP列表
public static List<AppInfo> GetAppList1(context context){
List<AppInfo> list=new ArrayList<>();
PackageManager pm = context.getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<resolveInfo> activities = pm.queryIntentActivities(mainIntent, 0);
for(ResolveInfo info : activities){
String packName = info.activityInfo.packageName;
if(packName.equals(context.getPackageName())){
continue;
}
AppInfo mInfo = new AppInfo();
mInfo.setIco(info.activityInfo.applicationInfo.loadIcon(pm));
mInfo.setName(info.activityInfo.applicationInfo.loadLabel(pm).toString());
mInfo.setPackageName(packName);
// 为应用程序的启动Activity 准备Intent
Intent launchIntent = new Intent();
launchIntent.setcomponent(new componentname(packName,
info.activityInfo.name));
mInfo.setIntent(launchIntent);
list.add(mInfo);
}
return list;
}
AppInfo的类也放出来
public class AppInfo {
private String packageName; //包名
private drawable ico; //图标
private String Name; //应用标签
private Intent intent; //启动应用程序的Intent ,一般是Action为Main和Category为Lancher的Activity
public Intent getIntent() {
return intent;
}
public void setIntent(Intent intent) {
this.intent = intent;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public Drawable getIco() {
return ico;
}
public void setIco(Drawable ico) {
this.ico = ico;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
}
4.自定义BaseAdapter填充GridView数据
public class DeskTopGridViewBaseAdapter extends BaseAdapter {
Context context;
List<AppInfo> appInfos=new ArrayList<>();
public DeskTopGridViewBaseAdapter(List<AppInfo> appInfos,Context context){
this.appInfos=appInfos;
this.context=context;
}
@Override
public int getCount() {
return appInfos.size();
}
@Override
public Object getItem(int position) {
return appInfos.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder=null;
if (convertView==null){
convertView = LayoutInflater.from(context).inflate(R.layout.desktop_gridview_item,null);
holder=new Holder();
holder.ico=(ImageView) convertView.findViewById(R.id.iv);
holder.Name=(TextView) convertView.findViewById(R.id.tv);
convertView.setTag(holder);
}else {
holder= (Holder) convertView.getTag();
}
holder.ico.setImageDrawable(appInfos.get(position).getIco());
holder.Name.setText(appInfos.get(position).getName());
return convertView;
}
static class Holder{
ImageView ico;
TextView Name;
}
R.layout.desktop_gridview_item的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_margin="5dp"
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxHeight="60dp"
/>
<TextView
android:layout_margin="5dp"
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:maxLines="1"
android:text="Lable"/>
</LinearLayout>
5.在主Activity里填充数据,并给GridView的Item设置点击事件,点击图标之后打开该App
private void initAppList() {
appInfos=GetApps.GetAppList1(this);
DeskTopGridViewBaseAdapter deskTopGridViewBaseAdapter=new DeskTopGridViewBaseAdapter(appInfos,this);
mGridView.setAdapter(deskTopGridViewBaseAdapter);
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Intent intent=appInfos.get(position).getIntent();
// startActivity(intent);
Intent intent = getPackageManager().getLaunchIntentForPackage(appInfos.get(position).getPackageName());
if (intent != null) {
intent.putExtra("type", "110");
intent.setFlags(Intent.flag_activity_new_task);
startActivity(intent);
}
}
});
}
到这里基本就打工搞成了,我们继续设置一下主Activity的背景为当前用户的桌面壁纸
//设置背景
WallpaperManager manager =WallpaperManager.getinstance(this);
Drawable drawable=manager.getDrawable();
linearLayout.setBackground(drawable);
这里需要注意,需要动态申请这两个权限,注意 是动态申请
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
继续,弹出对话框,让用户设置为默认桌面,这里针对小米、华为、普通安卓都有处理,我直接把代码贴出来
//桌面相关操作
public class Desktop {
private Context context;
public Desktop(Context context) {
this.context = context;
}
/**
* 清除默认桌面(采用先设置一个空的桌面为默认然后在将该空桌面禁用的方式来实现)
*
*/
@SuppressLint("Wrongconstant")
public void clearDefaultLauncher() {
PackageManager pm = context.getPackageManager();
String pn = context.getPackageName();
String hn = MockHome.class.getName();
ComponentName mhCN = new ComponentName(pn, hn);
Intent homeIntent = new Intent("android.intent.action.MAIN");
homeIntent.addCategory("android.intent.category.HOME");
homeIntent.addCategory("android.intent.category.DEFAULT");
homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
pm.setComponentEnabledSetting(mhCN, 1, 1);
context.startActivity(homeIntent);
pm.setComponentEnabledSetting(mhCN, 0, 1);
}
//弹出选择默认桌面
public void SetDefaultLauncher(){
try {
Intent paramIntent = new Intent("android.intent.action.MAIN");
paramIntent.setComponent(new ComponentName("android", "com.android.internal.app.ResolverActivity"));
paramIntent.addCategory("android.intent.category.DEFAULT");
paramIntent.addCategory("android.intent.category.HOME");
context.startActivity(paramIntent);
}catch (Exception e){
e.printstacktrace();
}
try {
Intent paramIntent1 = new Intent("android.intent.action.MAIN");
paramIntent1.setComponent(new ComponentName("com.huawei.android.internal.app", "com.huawei.android.internal.app.HwResolverActivity"));
paramIntent1.addCategory("android.intent.category.DEFAULT");
paramIntent1.addCategory("android.intent.category.HOME");
context.startActivity(paramIntent1);
}catch (Exception e){
e.printStackTrace();
startHuaweiSettingActOfDefLauncher();
}
}
//打开华为设置页面,让用户选择默认桌面
public void startHuaweiSettingActOfDefLauncher() {
IntentFilter localIntentFilter = new IntentFilter();
localIntentFilter.addAction(Intent.ACTION_MAIN);//"android.intent.action.MAIN"
localIntentFilter.addCategory(Intent.CATEGORY_HOME);//"android.intent.category.HOME"
Intent localIntent3 = new Intent(localIntentFilter.getAction(0));
localIntent3.addCategory(localIntentFilter.getCategory(0));
Intent localIntent4 = new Intent();
localIntent4.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
localIntent4.setClassName("com.android.settings", "com.android.settings.Settings$PreferredSettingsActivity");
localIntent4.putExtra("preferred_app_package_name", context.getPackageName());
localIntent4.putExtra("preferred_app_class_name", context.getClass().getName());
localIntent4.putExtra("is_user_confirmed", true);
localIntent4.putExtra("preferred_app_intent", localIntent3);
localIntent4.putExtra("preferred_app_intent_filter", localIntentFilter);
localIntent4.putExtra("preferred_app_label", "默认桌面设置");
context.startActivity(localIntent4);
}
//获取当前默认桌面,如果不是本App则弹出设置默认桌面对话框
public void getDefaultHome() {
final Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
final ResolveInfo res = context.getPackageManager().resolveActivity(intent, 0);
if (res.activityInfo == null) {
//获取失败
} else if (res.activityInfo.packageName.equals("android")) {
// No default selected
clearDefaultLauncher();
SetDefaultLauncher();
} else {
if (!res.activityInfo.packageName.equals("net.xiaomy.dxsjb.client")){
clearDefaultLauncher();
SetDefaultLauncher();
}
}
}
}
这里注意:清除默认桌面,需要一个空的Activity,这里贴上xml文件代码,再自行添加一个Activity类就好
<activity android:name=".activitys.MockHome" android:enabled="false"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</activity>
我这里在主Activity里调用:
//设置默认桌面
Desktop desktop=new Desktop(this);
desktop.getDefaultHome();
好了,到这里就算完成了,欢迎留言评论交流,转载请注明出处!
自己新开发的免费内网穿透项目小蚂蚁内网穿透,支持HTTP、TCP全端口映射,戳我进内网穿透官网www.xiaomy.net!
相关阅读
做了好多年Winform的程序的开发,主窗口的界面设计一般都要求做的更好一些,可以根据不同的系统功
android studio ndk-build 编译C生成.so文件(ndk基础篇
一、概要 最近项目需要,要把代码中加密的部分打包成so文件,刚开始接触的时候真是痛苦呀,网上好多资料,都不是很详细,步骤也不清晰,
GitHub:https://github.com/Kaopiz/android-segmented-control 一 、添加依赖 implementation 'info.hoang8f:android-segmented:
这篇文章希望向你分享的是:如果你想开发一款App,而你请不起一个完整的开发团队,那么你如何借助外包公司来做好这件事;你如何去揽下立
Android开发(6):Service使用之bindService
一、Service简介 Service(服务)是一种可以在后台执行长时间运行操作而没有用户界面的应用组件,是四大组件之一。Service可以理解为