重新启动智能手机后,放大我创建的壁纸

时间:2014-06-02 15:34:52

标签: android android-intent

我遇到了壁纸问题。当我创建并重置我的手机时,壁纸就会出现。如何更改BootReceiver类以正确加载wallpaer和screen的位置?

MainActivity:

public class MainActivity extends ActionBarActivity implements View.OnClickListener {

    public static int toPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView image1 = (ImageView) findViewById(R.id.IVimage1);
        ImageView image2 = (ImageView) findViewById(R.id.IVimage2);
        ImageView image3 = (ImageView) findViewById(R.id.IVimage3);

        Context context;
        image1.setOnClickListener(this);
        image2.setOnClickListener(this);
        image3.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void onClick(View view) {
        Intent intent = new Intent(this, Podglad.class);
        switch (view.getId()) {
            case R.id.IVimage1:
                toPhone = R.drawable.kwiaty;
                startActivity(intent);
                break;
            case R.id.IVimage2:
                toPhone = R.drawable.listek;
                startActivity(intent);
                break;
            case R.id.IVimage3:
                toPhone = R.drawable.kwiaty;
                startActivity(intent);
                break;
        }
    }
}

Podglad:

public class Podglad extends ActionBarActivity {
    final Context context = getApplicationContext();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_podglad);

        final Intent intent = new Intent();
        View myView =  this.findViewById(R.id.a);
        myView.setBackgroundResource(MainActivity.toPhone);
        Button b=(Button)findViewById(R.id.button);
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                SharedPreferences sharedPreferences = getSharedPreferences("wallpaperapp", 0);
                sharedPreferences.edit().putInt("position", 0).commit();
                DisplayMetrics metrics = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(metrics);
                int height = metrics.heightPixels;
                int width = metrics.widthPixels;
                Bitmap tempbitMap = BitmapFactory.decodeResource(getResources(), MainActivity.toPhone);
                Bitmap bitmap = Bitmap.createScaledBitmap(tempbitMap, width, height, true);
                WallpaperManager wallpaperManager = WallpaperManager.getInstance(Podglad.this);
                wallpaperManager.setWallpaperOffsetSteps(1, 1);
                wallpaperManager.suggestDesiredDimensions(width, height);
                try {
                    wallpaperManager.setBitmap(bitmap);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.podglad, menu);
        return true;
    }
}

BootReceiver:

public class BootReceiver extends BroadcastReceiver {
    private static final String TAG="BootReceiver";

    @Override public void onReceive(Context context,Intent intent){
        try{
            SharedPreferences sharedPreferences = context.getSharedPreferences("wallpaperapp",0);
            int position= sharedPreferences.getInt("position", 0);
            DisplayMetrics metrics = new DisplayMetrics();
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            windowManager.getDefaultDisplay().getMetrics(metrics);
            int height = metrics.heightPixels;
            int width = metrics.widthPixels;
            Bitmap tempbitMap = BitmapFactory.decodeResource(context.getResources(),MainActivity.toPhone);
            Bitmap bitmap = Bitmap.createScaledBitmap(tempbitMap,width,height, true);
            WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
            wallpaperManager.setWallpaperOffsetSteps(1, 1);
            wallpaperManager.suggestDesiredDimensions(width, height);
            try {
                wallpaperManager.setBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch(Exception e) {
            Log.e(TAG,e.toString());
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以为" android.intent.action.BOOT_COMPLETED"注册BroadcastReceiver并重做你所做的一切,让它在第一时间起作用。

或者您可以调整图像大小并裁剪图像以适合所需尺寸,以便在设备重新启动时加载已经完美尺寸的图像。

可能还有其他解决方案,但首先想到的是这两种解决方案。


编辑,以下是我在使用一种方法的评论中的含义示例:

public class WallpaperUtil {
    public static setWallpaper(Context context) {
        try {
            SharedPreferences sharedPreferences = context
                                .getSharedPreferences("wallpaperapp",0);
            ...
        WindowManager windowManager = (WindowManager) context
                                .getSystemService(Context.WINDOW_SERVICE);
            ...
        } catch(Exception e) {
            Log.e(TAG,e.toString());
        }
    }
}

public class BootReceiver extends BroadcastReceiver {

    @Override public void onReceive(Context context,Intent intent) {
        WallpaperUtil.setWallpaper(getApplicationContext);
    }
}

public class SetWallpaperActivity extends Activity
                                  implements View.OnClickListener {
    ...
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.buttonSetWallpaper:
                // save choice to SharedPreferences then
                WallpaperUtil.setWallpaper(this);
                break;
            default:
                break;
        }
    }
}