Simple Example of android live wallpaper calling

Source: Internet
Author: User

After the call, the dynamic wallpaper is actually displayed behind the Activity, while the Activity is transparent, so that you can see the following dynamic wallpaper. if the Activity is not transparent, nothing can be seen.

Two interfaces are used in the code.

IWallpaperService mService;

IWallpaperEngine mEngine;

We can see that the directory contains three aidl interfaces, which are

Copy codeThe Code is as follows: 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 ();

}

Define wallpaper management and wallpaper information Variables

Copy codeThe Code is as follows: private WallpaperManager mWallpaperManager = null;

Private WallpaperInfo mWallpaperInfo = null;

Private WallpaperConnection mWallpaperConnection = null;

Private Intent mWallpaperIntent;

Initialize these variables

Copy codeThe Code is as follows: mWallpaperManager = WallpaperManager. getInstance (this );

MWallpaperInfo = mWallpaperManager. getWallpaperInfo (); // If null is returned, the current wallpaper is not a dynamic one.

MWallpaperIntent = new Intent (WallpaperService. SERVICE_INTERFACE );

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

Bind dynamic wallpaper Service

Copy codeThe Code is as follows: bindService (mIntent, this, Context. BIND_AUTO_CREATE );

IWallpaperService mService; // here is an adil Interface

Test attach in connection listening

Copy codeThe Code is as follows: 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 );
}
}

The bindService always fails. Later, the permission problem was discovered. Only the apk with system permissions can use the WallpaperService. SERVICE_INTERFACE service. Therefore, the problem becomes how to obtain system permissions.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.