Setting wallpaper in Android

前端 未结 3 1866
离开以前
离开以前 2021-01-01 03:25

I am developing a simple app that sets wallpapers based on user input. I am missing code for setting wallpapers. I have been looking for it in lots of websites in vain. Can

相关标签:
3条回答
  • 2021-01-01 03:31

    Works on Android 1.5 and above

    public void setWallpaper() {
      Context context = this.getBaseContext(); 
      Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), mImageIds[pos]);
    
      context.setWallpaper(mBitmap);
    }
    
    0 讨论(0)
  • 2021-01-01 03:47

    Here how we can set Wallpaper from our android application

    MainActivity.Java

    public class AlarmActivity extends Activity{
    
    
    
           @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
               setContentView(R.layout.activity_main);
    
    
    
                WallpaperManager myWallpaperManager
                        = WallpaperManager.getInstance(getApplicationContext());
    
    
                        try {
                            myWallpaperManager.setImageResource(R.raw.sample);
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
    
        }
    }
    

    Manifest.xml

    you should provide this permission

    <uses-permission android:name="android.permission.SET_WALLPAPER" />
    
    0 讨论(0)
  • 2021-01-01 03:53

    u can try

    InputStream inputStream = getResources().openRawResource(wallpaperResource);
    Bitmap setWallToDevice = BitmapFactory.decodeStream(inputStream);
    
    try {
    getApplicationContext().setWallpaper(setWallToDevice);
    } catch (IOException e) {
    // TODO Auto-generated catch block
        e.printStackTrace();
    }
    

    however this method is deprecated so u can use

    try {
    WallpaperManager.getInstance(getApplicationContext()).setResource(wallpaperResource);
    } catch (IOException e){
    e.printStackTrace();
    }
    
    0 讨论(0)
提交回复
热议问题