Android的setWallpaper德precated [英] Android setWallpaper deprecated

查看:245
本文介绍了Android的setWallpaper德precated的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我是新来的Andr​​oid Studio和要问你们一个问题。
我想创建,让我把我的手机壁纸应用程序,但我有一个问题:getApplicationContext()setWallpaper(BM)。
它说,setWallpaper是德precated,我应该用另一种方法,但我没有IDEEA用了什么方法。
这是我的code迄今:

so i am new to Android Studio and wanted to ask you guys a question. I want to create an Application that allows me to set my phone's wallpaper, but i have a problem with : getApplicationContext().setWallpaper(bm); It says that setWallpaper is deprecated and i should use another method, but i have no ideea what method to use. This is my code so far:

   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;
        }
    }

}

谁能帮助我?

推荐答案

如果你看看在<一个href=\"http://developer.android.com/reference/android/content/Context.html#setWallpaper%28java.io.InputStream%29\"相对=nofollow>官方文档,你会发现,这个方法去$ P $因为pcated API 5 键,你应该使用<一个href=\"http://developer.android.com/reference/android/app/WallpaperManager.html#setBitmap%28android.graphics.Bitmap%29\"相对=nofollow> WallpaperManager.setBitmap()代替

If you take a look at the official docs, you will find, that this method is deprecated since API 5 and you should use WallpaperManager.setBitmap() instead.

这篇关于Android的setWallpaper德precated的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆