Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android動態壁紙調用的簡單實例

android動態壁紙調用的簡單實例

編輯:關於Android編程

調用後動態壁紙其實是顯示在Activity的後面,而Activity則是透明顯示,這樣就可以看到下面的動態壁紙,如果Activity不是透明的則什麼也看不到。

代碼中有用到兩個接口

IWallpaperService mService;

IWallpaperEngine mEngine;

我們可以看到該目錄下面有三個aidl接口,分別是

復制代碼 代碼如下:
interface IWallpaperConnection {

    void attachEngine(IWallpaperEngine engine);

    ParcelFileDescriptor setWallpaper(String name);

}

oneway interface IWallpaperService {

    void attach(IWallpaperConnection connection,

            IBinder windowToken, int windowType, boolean isPreview,

            int reqWidth, int reqHeight);

}


oneway interface IWallpaperEngine {

    void setDesiredSize(int width, int height);

    void setVisibility(boolean visible);

    void dispatchPointer(in MotionEvent event);

    void dispatchWallpaperCommand(String action, int x, int y,            int z, in Bundle extras);

    void destroy();

}

定義壁紙管理和壁紙信息變量
復制代碼 代碼如下:
private WallpaperManager mWallpaperManager = null;

private WallpaperInfo mWallpaperInfo = null;

private WallpaperConnection mWallpaperConnection = null;

private Intent mWallpaperIntent;

初始化這些變量
復制代碼 代碼如下:
mWallpaperManager = WallpaperManager.getInstance(this);

mWallpaperInfo = mWallpaperManager.getWallpaperInfo();//如果返回null則說明當前不是動態壁紙

mWallpaperIntent = new Intent(WallpaperService.SERVICE_INTERFACE);

mWallpaperIntent.setClassName(mWallpaperInfo.getPackageName(), mWallpaperInfo.getServiceName());

綁定動態壁紙服務
復制代碼 代碼如下:
bindService(mIntent, this, Context.BIND_AUTO_CREATE);

IWallpaperService mService;//這裡有一個adil接口

在連接監聽中試著attach

復制代碼 代碼如下:
public void onServiceConnected(ComponentName name, IBinder service) {

                mService = IWallpaperService.Stub.asInterface(service);

                try {

                    mService.attach(this, view.getWindowToken(),

                    //        WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY,
                            WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA,

                                  true, root.getWidth(), root.getHeight());

                } catch (RemoteException e) {

                    Log.w("", "Failed attaching wallpaper; clearing", e);
                }
        }

在bindService的時候發現總是失敗,後來發現是權限問題,只有擁有系統權限的apk才可以使用WallpaperService.SERVICE_INTERFACE服務,所以問題就變成了怎麼獲取系統權限。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved