Android使用wallpaperManager来改变壁纸,所调用的接口方法如下:
Android界面是动态的,由程序实时控制的,开发步骤如下:
①开发一个子类继承WallpaperService基类
②继承基类后重写onCreateEngine方法,该方法返回WallpaperService.Engine子类对象
③实现WallpaperService.Engine的子类,重写其中的onVisibilityChanged()、onOffsetsChanged()等方法,通过SurfaceHolder进行动态的绘制图形。
运行动态壁纸,需要下面两项配置:
运行动态壁纸,需要下面两项配置:
在长按某个应用的时候就会出现这样的快捷方式,在使用APP某个常用功能时很方便
实现方式如下:
通过AndroidManifest.xml方式进行添加
①在主Activity中添加:
②在xml中新建一个shortcuts,自定义其中内容
根元素是
enabled:设置该快捷键项是否可用
icon:设置该快捷键的图标
shortcutDisabledMessage:设置禁用该快捷项时所显示的文本
shortcutLongLabel:设置该快捷项的长标题
shortcutShortLabel:设置该快捷项的短标题
intent 这里表示我们点击shortcut进行的操作,,targetPackage是指定一个目标应用的包名,targetClass是我们要跳转的目标类, android:action一定要配置, 否则会崩溃
Categories,这个东西目前位置官方只给提供了android.shortcut.conversation
添加快捷方式:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {try {if (true) {ShortcutManager mShortcutManager = getSystemService(ShortcutManager.class);List infoList = new ArrayList<>();infoList.add(createShortcutInfo("id1", 0, icon1, "name1"));infoList.add(createShortcutInfo("id2", 1, icon2, "name2"));if (mShortcutManager != null)mShortcutManager.setDynamicShortcuts(infoList);}} catch (Exception e) {e.printStackTrace();}
}
private ShortcutInfo createShortcutInfo(String id, int rank, int icon, String label) {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {return new ShortcutInfo.Builder(this, id).setShortLabel(label)// 快捷方式桌面名称.setLongLabel(label)// APP长按显示的标题.setRank(rank)// 显示顺序.setIcon(Icon.createWithResource(this, icon))// 快捷方式的图标.setIntent(new Intent(Intent.ACTION_MAIN, null, this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)//根据需求来设置Flag.putExtra("key", id))// 传递跳转参数.build();}return null;
}
Intent跳转页面的处理:
Intent intent = getIntent();
String data = intent.getStringExtra("key");
if(data != null){switch (data){case "id1":Intent intent1 = new Intent(this, test1Activity.class);startActivity(intent1);break;case "id2":Intent intent1 = new Intent(this, test2Activity.class);startActivity(intent2);break;}
}
把一个程序的快捷方式添加到桌面的步骤:
①使用shortcutManager.isRequestPinShortcutSupported()判断当前版本是否支持Pinned快捷方式
②创建shortcutInfo,设置它的ID、图标、长标题、短标题等。
③使用shortcutManager的 requestPinShortcut()方法请求添加Pinned快捷方式
ShortcutManager shortcutManager=getSystemService(ShortcutManager.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {if (shortcutManager.isRequestPinShortcutSupported()){Intent intent=new Intent(MainActivity.this,TsukyActivity.class);intent.setAction("android.intent,action,VIEW");//如果ID是my_shortcut的快捷方式已经存在,就用已有的信息 不存在则设置ShortcutInfo shortcutInfo=new ShortcutInfo.Builder(MainActivity.this,"my_shortcut").setShortLabel("快捷方式").setIcon(Icon.createWithResource(this,R.drawable.heart)).setIntent(intent).build();//请求添加Pinned快捷方式shortcutManager.requestPinShortcut(shortcutInfo,null);}
}
设置了一个控件的点击事件,然后加入上述代码,运行后点击就会出现下面的添加快捷方式的按钮
桌面控件就是能直接显示在Android系统桌面上的小程序,时钟日历等
桌面控件是通过BroadcastReceiver的形式来进行控制的,因此每个桌面控件都对应一个BroadcastReceiver,为了更好的管理控件的开发,Android提供了AppWedgetProvider类,他是BroadcastReceiver的子类。
因此开发桌面控件,只要继承AppWedgetProvider,重写不同的生命周期方法即可。
onUpdate():负责更新桌面控件的方法
onDelete():桌面控件被删除时的回调方法
onEnabled():收到ACTION_APPWIDGET_ENABLED broadcast时回调该方法
onDisabled():收到ACTION_APPWIDGET_DISABLED broadcast时回调该方法
public class Desktop extends AppWidgetProvider {@Overridepublic void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {super.onUpdate(context, appWidgetManager, appWidgetIds);//加载页面布局文件 创建RemoteView文件RemoteViews remoteViews=new RemoteViews(context.getPackageName(), R.drawable.heart);//为ImageView设置图片remoteViews.setImageViewResource(R.id.text,R.drawable.heart);//将AppWidgetProvider的子类伪装成ComponentName对象ComponentName componentName=new ComponentName(context,Desktop.class);//调用appWidgetManager将remoteViews添加到ComponName中appWidgetManager.updateAppWidget(componentName,remoteViews);}
}
还需要在Manifest中进行配置,由于他的本质是broadcastReceiver,所以要注册:
android:exported="true">
作为meta-data,还需要在目录中添加appwidget的xml文件。从上到下分别是初始显示布局、最小高度、最小宽度、更新频率