1

我想创建一个允许我设置手机壁纸的应用程序,但我遇到了问题:getApplicationContext().setWallpaper(bm)
它说不setWallpaper推荐使用,我应该使用另一种方法,但我不知道使用什么方法。
到目前为止,这是我的代码:

   package com.example.asus.incercare_proiect;

/**
 * Created by Asus on 29-Apr-15.
 */
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

 class bakapp extends Activity implements OnClickListener {
    ImageView display;
    int tophone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        // activitatea noastra sa acopere intreg ecranul
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.backapp);
        Button but = (Button) findViewById(R.id.setwallpaper);
        tophone = R.drawable.image4;
        but.setOnClickListener(this);
        display = (ImageView) findViewById(R.id.ivdisplay);
        ImageView image1 = (ImageView) findViewById(R.id.IVimage1);
        ImageView image2 = (ImageView) findViewById(R.id.IVimage2);
        ImageView image3 = (ImageView) findViewById(R.id.IVimage3);
        ImageView image4 = (ImageView) findViewById(R.id.image4);
        ImageView image5 = (ImageView) findViewById(R.id.IVimage5);

        image1.setOnClickListener(this);
        image2.setOnClickListener(this);
        image3.setOnClickListener(this);
        image4.setOnClickListener(this);
        image5.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Toast var;
        switch (v.getId()) {
            case R.id.IVimage1:
                display.setImageResource(R.drawable.image4);
                var = Toast.makeText(bakapp.this, "image changed",
                        Toast.LENGTH_SHORT);

                var.show();

                tophone = R.drawable.image4;
                break;
            case R.id.IVimage2:
                display.setImageResource(R.drawable.image5);
                var = Toast.makeText(bakapp.this, "image changed",
                        Toast.LENGTH_SHORT);

                var.show();

                tophone = R.drawable.image5;
                break;
            case R.id.IVimage3:
                display.setImageResource(R.drawable.image6);
                var = Toast.makeText(bakapp.this, "image changed",
                        Toast.LENGTH_SHORT);

                var.show();

                tophone = R.drawable.image6;
                break;
            case R.id.image4:
                display.setImageResource(R.drawable.image7);
                var = Toast.makeText(bakapp.this, "image changed",
                        Toast.LENGTH_SHORT);

                var.show();

                tophone = R.drawable.image7;
                break;
            case R.id.IVimage5:
                display.setImageResource(R.drawable.image8);
                var = Toast.makeText(bakapp.this, "image changed",
                        Toast.LENGTH_SHORT);

                var.show();

                tophone = R.drawable.image8;
                break;
            case R.id.setwallpaper:
                // trebuie folosit bitmap
                InputStream is = getResources().openRawResource(tophone);

                Bitmap bm = BitmapFactory.decodeStream(is);

                try {
                    getApplicationContext().setWallpaper(bm);

                // trebuie sa cerem permisiune pentru a seta Wallpaperul

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
           var = Toast.makeText(bakapp.this, "Wallpaper image changed",
                        Toast.LENGTH_SHORT);

                var.show();

                break;
        }
    }

}

谁能帮帮我?

4

2 回答 2

3

如果您查看官方文档,您会发现此方法已被弃用API 5,您应该改用WallpaperManager.setBitmap()

于 2015-04-29T14:34:17.787 回答
2

首先,您在 Menifest 文件中设置权限

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

然后使用 WallpaperManager 设置壁纸。

WallpaperManager myWallpaperManager 
        = WallpaperManager.getInstance(getApplicationContext());
        try {
            myWallpaperManager.setResource(R.drawable.five);
于 2015-04-29T14:35:59.077 回答