Manifest.xml中:
<service android:enabled="true" android:permission="android.permission.BIND_WALLPAPER" android:name="com.android.settings.common.display.BackgroundSettingService"> <intent-filter> <action android:name="android.service.wallpaper.WallpaperService"></action> </intent-filter> <meta-data android:name="android.service.wallpaper" android:resource="@xml/wallpaper"> </meta-data> </service>
选中设置中的条目:
Activity activity = getActivity(); PreferenceScreen screenBackgroundSetting = (PreferenceScreen) findPreference(KEY_BACKGROUND_SETTING); if(screenBackgroundSetting != null){ screenBackgroundSetting.setOnPreferenceClickListener(new OnPreferenceClickListener(){ @Override public boolean onPreferenceClick(Preference preference) { // TODO Auto-generated method stub startFragmentTransaction(new CommonBackgroundSettingFragment()); return true; } }); }
右边界面切换为如下Fragment:
package com.android.settings.common.display; import android.app.Fragment; import android.app.WallpaperInfo; import android.app.WallpaperManager; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.Toast; import com.android.settings.R; public class CommonBackgroundSettingFragment extends Fragment{ private Button btnBackground1; private static final String tagetPackageName = "com.android.wallpaper"; private static final int REQUEST_SET_LIVE_WALLPAPER = 101; private static final String TAG = "CommonBackgroundSettingFragment"; @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_background_setting, container, false); btnBackground1 = (Button) rootView.findViewById(R.id.btnBackground1); btnBackground1.requestFocus(); btnBackground1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setAction(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER); startActivityForResult(intent, REQUEST_SET_LIVE_WALLPAPER); Toast.makeText(CommonBackgroundSettingFragment.this.getActivity(), getString(R.string.start_background_setting), Toast.LENGTH_LONG).show(); } }); return rootView; } @Override public void onResume() { // TODO Auto-generated method stub super.onResume(); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_SET_LIVE_WALLPAPER) { if (isLiveWallpaperRunning(this.getActivity(), tagetPackageName)) { Toast.makeText(this.getActivity(), "LiveWallpaper is Running", Toast.LENGTH_LONG).show(); }else{ Toast.makeText(this.getActivity(), "LiveWallpaper is not Running", Toast.LENGTH_LONG).show(); } } super.onActivityResult(requestCode, resultCode, data); } /** * 判断一个动态壁纸是否已经在运行 * * @param context * :上下文 * @param tagetPackageName * :要判断的动态壁纸的包名 * @return */ public static boolean isLiveWallpaperRunning(Context context, String tagetPackageName) { WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);// 得到壁纸管理器 WallpaperInfo wallpaperInfo = wallpaperManager.getWallpaperInfo();// 如果系统使用的壁纸是动态壁纸话则返回该动态壁纸的信息,否则会返回null if (wallpaperInfo != null) { // 如果是动态壁纸,则得到该动态壁纸的包名,并与想知道的动态壁纸包名做比较 String currentLiveWallpaperPackageName = wallpaperInfo.getPackageName(); if (currentLiveWallpaperPackageName.equals(tagetPackageName)) { return true; } } return false; } }
设置动态壁纸的Service:
/** * LiveWallpaper Launcher And Preference Template * The MIT License * copyright (c) 2011 mdlab.jp * @author itoz */ package com.android.settings.common.display; import java.sql.Date; import java.text.SimpleDateFormat; import android.annotation.SuppressLint; import android.content.SharedPreferences; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Bundle; import android.os.Handler; import android.service.wallpaper.WallpaperService; import android.util.Log; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.widget.Toast; import com.android.settings.R; @SuppressLint("NewApi") public class BackgroundSettingService extends WallpaperService { private static final String TAG = "BackgroundSettingService"; private Handler handler = new Handler(); @Override public void onCreate() { Log.i(TAG, "service onCreate"); super.onCreate(); } @Override public void onDestroy() { Log.i(TAG, "service onDestory"); super.onDestroy(); } @Override public Engine onCreateEngine() { Log.i(TAG, "service onCreateEngine"); return new MyEngine(); } private class MyEngine extends Engine{ private float x = 200; private float y = 300; private Paint mPaint; private boolean mVisible = false; private Runnable drawThread = new Runnable() { public void run() { drawWallpaper(); } }; public MyEngine(){ Log.i(TAG, "MyEngine"); mPaint = new Paint(); mPaint.setColor(Color.RED); mPaint.setAntiAlias(true); mPaint.setStrokeWidth(4); mPaint.setStyle(Paint.Style.STROKE); mPaint.setTextSize(24); } @Override public Bundle onCommand(String action, int x, int y, int z, Bundle extras, boolean resultRequested) { // TODO Auto-generated method stub Log.i(TAG, "onCommand"); return super.onCommand(action, x, y, z, extras, resultRequested); } @Override public void onCreate(SurfaceHolder surfaceHolder) { Log.i(TAG, "onCreate"); super.onCreate(surfaceHolder); //作用是使壁纸能响应touch event,默认是false setTouchEventsEnabled(true); } @Override public void onDestroy() { Log.i(TAG, "onDestroy"); super.onDestroy(); handler.removeCallbacks(drawThread); } @Override public void onOffsetsChanged(float xOffset, float yOffset, float xOffsetStep, float yOffsetStep, int xPixelOffset, int yPixelOffset) { // TODO Auto-generated method stub Log.i(TAG, "onoffsetsChanged"); super.onOffsetsChanged(xOffset, yOffset, xOffsetStep, yOffsetStep, xPixelOffset, yPixelOffset); } @Override public void onVisibilityChanged(boolean visible) { Log.i(TAG, "onVisibilityChanged"); mVisible = visible; if (visible) { //开始 handler.postDelayed(drawThread, 50); } else { handler.removeCallbacks(drawThread); } } @Override public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) { Log.i(TAG, "onSurfaceChanged"); // TODO Auto-generated method stub super.onSurfaceChanged(holder, format, width, height); } @Override public void onSurfaceCreated(SurfaceHolder holder) { Log.i(TAG, "onSurfaceCreated"); super.onSurfaceCreated(holder); } @Override public void onSurfaceDestroyed(SurfaceHolder holder) { Log.i(TAG, "onSurfaceDestoryed"); // TODO Auto-generated method stub super.onSurfaceDestroyed(holder); handler.removeCallbacks(drawThread); } @Override public void onTouchEvent(MotionEvent event) { Log.i(TAG, "onTouchEvent"); // TODO Auto-generated method stub super.onTouchEvent(event); } private void drawWallpaper() { SurfaceHolder holder = getSurfaceHolder(); Canvas canvas = holder.lockCanvas(); drawTime(canvas); holder.unlockCanvasAndPost(canvas); //循环执行 handler.postDelayed(drawThread, 50); } private void drawTime(Canvas canvas) { Date date = new Date(System.currentTimeMillis()); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String dataStr = sdf.format(date); canvas.save(); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.captain_america); Bitmap mBitmapDisplayed = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), false); canvas.drawBitmap(mBitmapDisplayed, 0, 0, null); // canvas.drawColor(0xff00ff00); canvas.drawText(dataStr, x, y, mPaint); canvas.restore(); } } }
效果为:背景是“captain_america.png”,同时上层有时间的动态更新。
原文:http://www.cnblogs.com/leihupqrst/p/4396065.html