gpt4 book ai didi

java - 重启设备后如何使用bootreciever设置壁纸? (安卓, eclipse )

转载 作者:行者123 更新时间:2023-11-30 02:41:11 28 4
gpt4 key购买 nike

我正在制作一个壁纸应用程序,但我遇到了一个问题,即重启手机后壁纸会放大,所以我尝试使用 bootreciever,它做了我想要它做的事情,但它只适用于应用程序中的第一张图片和每当我将任何其他图像设置为墙纸并重新启动设备时,图像都会更改为第一张图像。

那么有谁知道如何解决这个问题,在此先感谢,

这是我的 MainActivity java:

公共(public)类 MainActivity 扩展 Activity 实现 OnClickListener {

static int tophone;
ImageView display;
public static Integer[] tophone2 = {
R.drawable.iv1,R.drawable.iv2,R.drawable.iv3,R.drawable.iv4 };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);

tophone = R.drawable.iv1;
display = (ImageView) findViewById(R.id.IView);
ImageView image1 = (ImageView) findViewById(R.id.iv1);
ImageView image2 = (ImageView) findViewById(R.id.iv2);
ImageView image3 = (ImageView) findViewById(R.id.iv3);
ImageView image4 = (ImageView) findViewById(R.id.iv4);
Button setWp = (Button) findViewById(R.id.setWp);

Picasso.with(MainActivity.this).load(R.drawable.iv1_s).into(image1);
Picasso.with(MainActivity.this).load(R.drawable.iv2_s).into(image2);
Picasso.with(MainActivity.this).load(R.drawable.iv3_s).into(image3);
Picasso.with(MainActivity.this).load(R.drawable.iv4_s).into(image4);


image1.setOnClickListener(this);
image2.setOnClickListener(this);
image3.setOnClickListener(this);
image4.setOnClickListener(this);
setWp.setOnClickListener(this);
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.iv1:
display.setImageResource(R.drawable.iv1);
tophone = tophone2[0];
break;
case R.id.iv2:
display.setImageResource(R.drawable.iv2);
tophone = tophone2[1];
break;
case R.id.iv3:
display.setImageResource(R.drawable.iv3);
tophone = tophone2[2];
break;
case R.id.iv4:
display.setImageResource(R.drawable.iv4);
tophone = tophone2[3];
break;
case R.id.setWp:
Toast WpSet = Toast.makeText(MainActivity.this,"Wallpaper Set", Toast.LENGTH_SHORT);

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(), tophone);
Bitmap bitmap = Bitmap.createScaledBitmap(tempbitMap,width,height, true);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(MainActivity.this);
wallpaperManager.setWallpaperOffsetSteps(1, 1);
wallpaperManager.suggestDesiredDimensions(width, height);
try {
wallpaperManager.setBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
WpSet.show();
break;
}



}

}

这是我的 BootReceiver java:

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.tophone2[position]);
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());
}
}
}

新的引导接收器:

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

@Override public void onReceive(Context context,Intent intent){
Intent in = new Intent(context, MainActivity.class);
in.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(in);
try{
SharedPreferences sharedPreferences = context.getSharedPreferences("wallpaperapp",MainActivity.tophone);
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.someDefault);
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());
}
}
}

最佳答案

第一个变化

SharedPreferences       sharedPreferences = getSharedPreferences("wallpaperapp",0);

SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);

每当您更改壁纸时,您应该将新更改的壁纸的资源 ID 保存到您的共享首选项文件中,在您的 onClick 方法中:

sharedPreference.edit().putInt(KEY, v.getId()).commit();

注意* putInt 方法中的 KEY 变量是一个您必须定义的常量,它是您要更改的首选项的名称,您可以给它赋值“wallpaperapp”

现在您可以在手机完成启动时简单地检索 ID。使用相同的 KEY

int retrievedImageID = sharedPreference.getInt(KEY, someDefaultValue);

然后简单的设置图片

display.setImageResource(retrievedImageID);

这里是android官方关于sharedPreferences的文档 http://developer.android.com/training/basics/data-storage/shared-preferences.html

您的 MainActivity 看起来像这样:

public class MainActivity extends Activity implements OnClickListener {
// add these variables
public static final String KEY = "wallpaperapp"
private SharedPreferences sharedPreference;
private int retrievedImageID;

protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
//add these lines
sharedPreference = getPreferences(MODE_PRIVATE);
changeWallpaper();
}

public void onClick(View v) {
switch(){}
// add this line below the switch statement
sharedPreference.edit().putInt(KEY, v.getId()).commit();
}
//add this method
public void changeWallpaper(){
int someDefaultValue = R.id.iv1;
retrievedImageID = sharedPreference.getInt(KEY, someDefaultValue);
display.setImageResource(retrievedImageID);
}

还有你的引导接收器:

public void onReceive(Context context,Intent intent){
// comment out the existing code in here
// and add the following lines
Intent in = new Intent(context, MainActivity.class);
// these flags must be set for the receiver to start the activity
in.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(in);
}

关于java - 重启设备后如何使用bootreciever设置壁纸? (安卓, eclipse ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25735088/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com