简体   繁体   English

关闭后,动态壁纸仍在运行

[英]Live wallpaper is still running after i close it

I'm studing live wallpaper on Android. 我在Android上学习动态壁纸。 I notice that my wallpaper is still running despite i select another wallpaper. 我注意到我的壁纸仍在运行,尽管我选择了另一个壁纸。 To reproduce this behavior, i will use a simple live wallpaper. 要重现此行为,我将使用简单的动态壁纸。 The manifest is: 清单是:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.wallpaper.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="23" />

    <uses-feature android:name="android.software.live_wallpaper" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <service
            android:name=".DemoWallpaperService"
            android:label="Demo Live Wallpaper"
            android:permission="android.permission.BIND_WALLPAPER" >
            <intent-filter>
                <action android:name="android.service.wallpaper.WallpaperService" />
            </intent-filter>

            <meta-data
                android:name="android.service.wallpaper"
                android:resource="@xml/wallpaper" />
        </service>
    </application>

</manifest>

The service code is: 服务代码是:

package org.wallpaper.test;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.util.Log;
import android.view.SurfaceHolder;

public class DemoWallpaperService extends WallpaperService {
    @Override
    public Engine onCreateEngine() {
        Log.i("DemoWallpaperService","DemoWallpaperService > onCreateEngine");      
        return new DemoWallpaperEngine();
    }

    @Override
    public void onCreate() {
        Log.i("DemoWallpaperService","DemoWallpaperService > onCreate");    
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        Log.i("DemoWallpaperService","DemoWallpaperService > onDestroy");   
        super.onDestroy();
    }

    private class DemoWallpaperEngine extends Engine {
        private boolean mVisible = false;
        private final Handler mHandler = new Handler();
        private final Runnable mUpdateDisplay = new Runnable() {
            @Override
            public void run() {
                draw();
            }
        };

        private void draw() {
            SurfaceHolder holder = getSurfaceHolder();
            Canvas c = null;
            try {
                c = holder.lockCanvas();
                if (c != null) {
                    Paint p = new Paint();
                    p.setTextSize(20);
                    p.setAntiAlias(true);
                    String text = "system time: " + Long.toString(System.currentTimeMillis());
                    float w = p.measureText(text, 0, text.length());
                    int offset = (int) w / 2;
                    int x = c.getWidth() / 2 - offset;
                    int y = c.getHeight() / 2;
                    p.setColor(Color.BLACK);
                    c.drawRect(0, 0, c.getWidth(), c.getHeight(), p);
                    p.setColor(Color.WHITE);
                    c.drawText(text, x, y, p);
                }
            } finally {
                if (c != null)
                    holder.unlockCanvasAndPost(c);
            }
            mHandler.removeCallbacks(mUpdateDisplay);
            if (mVisible) {
                mHandler.postDelayed(mUpdateDisplay, 100);
            }
        }

        @Override
        public void onVisibilityChanged(boolean visible) {
            mVisible = visible;
            if (visible) {
                draw();
            } else {
                mHandler.removeCallbacks(mUpdateDisplay);
            }
        }

        @Override
        public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            draw();
        }

        @Override
        public void onSurfaceDestroyed(SurfaceHolder holder) {
            super.onSurfaceDestroyed(holder);
            mVisible = false;
            mHandler.removeCallbacks(mUpdateDisplay);
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            mVisible = false;
            mHandler.removeCallbacks(mUpdateDisplay);
        }
    }
}

After compile, deploy on device and select it. 编译完成后,在设备上部署并选择它。

在此输入图像描述

Now i select another wallpaper, so i expect the wallpaper process die. 现在我选择另一个壁纸,所以我期待壁纸工艺死亡。 If i go to DDMS i see that the process is still running: 如果我去DDMS,我发现该过程仍在运行:

在此输入图像描述

And there many thread still alive: 还有许多线程还活着:

在此输入图像描述

This is correct, or the wallpaper does not close properly? 这是正确的,还是壁纸没有正确关闭?

Thanks 谢谢

After investigation i found that, despite the invocation onDestroy method of the service, the wallpaper is still alive. 调查后我发现,尽管调用onDestroy方法的服务,壁纸仍然存在。 The process is put in the cached processes. 该过程放在缓存的进程中。 It is done to resume it faster, if your reselect it. 如果重新选择它,可以更快地恢复它。

The process will be kill when memory is claimed. 在声明内存时,该过程将被终止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM