Android desktop wallpaper and shortcuts (on)

change wallpaper

Android uses wallpaperManager to change the wallpaper, and the interface method called is as follows:

Develop live wallpapers

The Android interface is dynamic and controlled by the program in real time. The development steps are as follows:

① Develop a subclass to inherit the WallpaperService base class

② After inheriting the base class, rewrite the onCreateEngine method, which returns the WallpaperService.Engine subclass object

③ Implement the subclass of WallpaperService.Engine, rewrite the onVisibilityChanged(), onOffsetsChanged() and other methods in it, and draw graphics dynamically through SurfaceHolder.

To run the live wallpaper, the following two configurations are required:

运行动态壁纸,需要下面两项配置:
         <service
            android:label="@string/app_name"
            android:name=".view.MyWallPaper"
            android:permission="android.permission.BIND_WALLPAPER"
            android:exported="true">
<!--            为动态壁纸配置intent-filter-->
            <intent-filter>
               <action android:name="android.service.wallpaper.WallpaperService"/>
            </intent-filter>
<!--            为动态壁纸配置meta-data-->
            <meta-data android:name="android.service.wallpaper"
                android:resource="@xml/livewallpaper"/>
        </service>

A shortcut

Such a shortcut will appear when you press and hold an application, which is very convenient when using a common function of the APP

The implementation is as follows:

static way

Add via AndroidManifest.xml

① Add in the main Activity:

<activity
            android:name=".view.MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
<!--            指定快捷方式的资源文件-->
            <meta-data android:name="android.app.shortcuts"
                android:resource="@xml/fk_shortcuts"/>
        </activity>

②Create a new shortcuts in xml and customize its content

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:enabled="true"
        android:icon="@drawable/ic_launcher_background"
        android:shortcutId="tsuky"
        android:shortcutDisabledMessage="@string/app_name"
        android:shortcutLongLabel="@string/app_name"
        android:shortcutShortLabel="@string/app_name">
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.example.yueeasy.view.MainActivity"
            android:targetPackage="com.example.yueeasy"/>
        <categories android:name="android:shortcuts,conversation"/>
    </shortcut>
</shortcuts>

The root element is <shortcuts/> and can contain multiple <shortcut/> sub-elements. The attributes of the <shortcut/> sub-elements are as follows:

enabled: Set whether the shortcut key item is available

icon: Set the icon of the shortcut key

shortcutDisabledMessage: Set the text displayed when the shortcut is disabled

shortcutLongLabel: Set the long title of the shortcut item

shortcutShortLabel: Set the short title of the shortcut item

Intent here indicates the operation we click on the shortcut, targetPackage is the package name of a target application, targetClass is the target class we want to jump to, android:action must be configured, otherwise it will crash

Categories, the official location of this thing only provides android.shortcut.conversation

dynamic way

Add shortcut:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
    try {
        if (true) {
            ShortcutManager mShortcutManager = getSystemService(ShortcutManager.class);
            List<ShortcutInfo> 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;
}

Processing of Intent jump page:

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;
    }
}

Desktop shortcut

Steps to add a program shortcut to the desktop:

① Use shortcutManager.isRequestPinShortcutSupported() to determine whether the current version supports Pinned shortcuts

②Create shortcutInfo, set its ID, icon, long title, short title, etc.

③Use the requestPinShortcut() method of shortcutManager to request to add a Pinned shortcut

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);
    }
}

Set the click event of a control, and then add the above code, after running, click the button to add shortcut will appear below

desktop controls

The desktop control is a small program that can be directly displayed on the desktop of the Android system, such as a clock calendar, etc.

Steps to Develop a Desktop Control

Desktop controls are controlled in the form of BroadcastReceiver, so each desktop control corresponds to a BroadcastReceiver. In order to better manage the development of controls, Android provides the AppWedgetProvider class, which is a subclass of BroadcastReceiver.

Therefore, to develop desktop controls, you only need to inherit AppWedgetProvider and rewrite different life cycle methods.

onUpdate() : The method responsible for updating the desktop controls

onDelete() : The callback method when the desktop control is deleted

onEnabled() : This method is called back when ACTION_APPWIDGET_ENABLED broadcast is received

onDisabled() : This method is called back when ACTION_APPWIDGET_DISABLED broadcast is received

  • Create a class that inherits AppWedgetProvider and override the onUpdate method
  • Create a RemoteViews object and load the specified layout file
  • If you need to change the content of the interface layout file loaded in the previous step, you can modify it through RemoteViews. RemoteViews mainly includes two components: ImageView and TextView.
  • Create a ComponentName object
  • Call AppWedgetManager to update desktop controls
  • public class Desktop extends AppWidgetProvider {
    
        @Override
        public 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);
        }
    }

    It also needs to be configured in the Manifest. Since its essence is broadcastReceiver, it needs to be registered:

    android:exported="true">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
    </intent-filter>
    <meta-data android:name="android.appwidget.provider"
        android:resource="@xml/appwidget"/>
</receiver>

As meta-data, you also need to add the appwidget xml file in the directory. From top to bottom are the initial display layout, minimum height, minimum width, and update frequency

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/activity_main"
    android:minHeight="70dp"
    android:minWidth="150dp"
    android:updatePeriodMillis="1000">
</appwidget-provider>

Guess you like

Origin blog.csdn.net/m0_56366502/article/details/129599024