【问题标题】:I want to change the current wallpaper to another one programatically in android.Is it possible? [duplicate]我想在android中以编程方式将当前墙纸更改为另一个墙纸。这可能吗? [复制]
【发布时间】:2012-03-27 12:39:32
【问题描述】:

可能重复:
Android - how to set the wallpaper image

我想在android中以编程方式将当前墙纸更改为另一个墙纸。这可能吗?我正在做一个根据用户设置的条件自动更改墙纸的项目

【问题讨论】:

标签: android


【解决方案1】:

首先在AndroidManifest.xml文件中添加此权限

<uses-permission android:name="android.permission.SET_WALLPAPER">

ma​​in.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    android:orientation="vertical" >

    <Button
        android:layout_margin="20dp"
        android:id="@+id/set"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Set WallPaper" />
<ImageView
        android:layout_margin="20dp"
        android:id="@+id/preview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
         />

</LinearLayout>

那么下面是你要实现的java代码:

     public class SetWallpaper extends Activity {
            Bitmap bitmap;
            int lastImageRef;

            /** Called when the activity is first created. */
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);

                Button buttonSetWallpaper = (Button)findViewById(R.id.set);
                ImageView imagePreview = (ImageView)findViewById(R.id.ic_launcher);
                imagePreview.setImageResource(R.drawable.five);

                buttonSetWallpaper.setOnClickListener(new Button.OnClickListener(){
                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        WallpaperManager myWallpaperManager
                        = WallpaperManager.getInstance(getApplicationContext());
                        try {
                                myWallpaperManager.setResource(R.drawable.ic_launcher);
                        } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                    }});
             }
        }

【讨论】:

    【解决方案2】:

    是的你indeed can

    public void setStream (InputStream data)
    

    来自developers site

    将当前系统壁纸更改为特定字节流。这 将 InputStream 复制到持久存储中,现在将 用作壁纸。目前它必须是 JPEG 或 PNG 图片。成功后,意图 ACTION_WALLPAPER_CHANGED 被广播。 参数

    data 包含要安装为墙纸的原始数据的流。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多