Java 重启设备后如何使用BootReceiver设置壁纸?(android、eclipse)
我正在制作一个壁纸应用程序,但我遇到了一个问题,即重新启动手机后壁纸会放大,因此我尝试使用BootReceiver,它会执行我希望它执行的操作,但它仅适用于应用程序中的第一个图像,并且每当我将任何其他图像设置为壁纸并重新启动设备时,图像会更改为第一个图像 那么有人知道如何解决这个问题吗,提前谢谢 以下是我的主要活动: 公共类MainActivity扩展活动实现OnClickListener{Java 重启设备后如何使用BootReceiver设置壁纸?(android、eclipse),java,android,eclipse,Java,Android,Eclipse,我正在制作一个壁纸应用程序,但我遇到了一个问题,即重新启动手机后壁纸会放大,因此我尝试使用BootReceiver,它会执行我希望它执行的操作,但它仅适用于应用程序中的第一个图像,并且每当我将任何其他图像设置为壁纸并重新启动设备时,图像会更改为第一个图像 那么有人知道如何解决这个问题吗,提前谢谢 以下是我的主要活动: 公共类MainActivity扩展活动实现OnClickListener{ static int tophone; ImageView display; public static
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());
}
}
}
新BootReceiver:
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);
到
无论何时更改墙纸,都应使用onClick方法将新更改墙纸的资源id保存到共享首选项文件中:
sharedPreference.edit().putInt(KEY, v.getId()).commit();
注意*putInt方法中的关键变量是一个常数,您必须定义它,它是您要更改的首选项的名称,您可以给它一个值“WallperApp”
现在,您只需在手机完成引导后检索id即可。使用相同的密钥
int retrievedImageID = sharedPreference.getInt(KEY, someDefaultValue);
然后简单地设置图像
display.setImageResource(retrievedImageID);
以下是关于SharedReferences的android官方文档
您的主要活动将类似于:
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文件中提供的代码?我的MainActivity或BootReceiver?您应该将资源id保存在OnClick方法中的MainActivity中。其他两行代码您可以将其放在自己的方法中,然后在BootReceiver中,您可以n调用此方法我在MODE_PRIVATE中遇到一个错误,无法将其解析为变量,并且您不能使用我的代码为我提供一个示例,因为我很难理解您的示例:),抱歉,所有的麻烦,我仍然是初学者您必须导入上下文包:import android.content.Contextthe context pa包装是进口的
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);
}