我有一个壁纸应用程序,其中包含一个包含图像的列表视图,然后您选择一个图像然后将其设置为壁纸但我不知道如何判断用户选择了哪个项目然后我有一个广播接收器,所以壁纸在设备通电后不会发生变化,但我也不知道如何告诉它选择了哪个项目。
这是我的MainActivty:
public class Set1_Activity extends Activity {
ImageView display;
ListView list;
Integer[] images = {
R.drawable.iv1_s, R.drawable.iv2_s, R.drawable.iv3_s, R.drawable.iv4_s,
.......
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main2);
display = (ImageView) findViewById(R.id.IView);
list = (ListView) findViewById(R.id.setOneListView);
HeartlessAdapter adapter = new HeartlessAdapter(this, images);
list.setAdapter(adapter);
Button setWp = (Button) findViewById(R.id.setWp);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
switch(position){
case 0:
display.setImageResource(R.drawable.iv1);
break;
case 1:
display.setImageResource(R.drawable.iv2);
break;
case 2:
display.setImageResource(R.drawable.iv3);
break;
........
setWp.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast WpSet = Toast.makeText(Set1_Activity.this,"Wallpaper Set", Toast.LENGTH_SHORT);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
Bitmap tempbitMap = BitmapFactory.decodeResource(getResources(), //what should I write here );
Bitmap bitmap = Bitmap.createScaledBitmap(tempbitMap,width,height, true);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(Set1_Activity.this);
wallpaperManager.setWallpaperOffsetSteps(1, 1);
wallpaperManager.suggestDesiredDimensions(width, height);
try {
wallpaperManager.setBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
WpSet.show();
}
});
这是我的广播接收者:
public class BootReceiver extends BroadcastReceiver {
private static final String TAG="BootReceiver";
@Override public void onReceive(Context context,Intent intent){
try{
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(), //what should i type here);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
wallpaperManager.setWallpaperOffsetSteps(1, 1);
wallpaperManager.suggestDesiredDimensions(width, height);
try {
wallpaperManager.setBitmap(tempbitMap);
} catch (IOException e) {
e.printStackTrace();
}
}catch(Exception e){
Log.e(TAG,e.toString());
}
}
}
答案 0 :(得分:0)
为什么不使用preferences
,请在MainActivity
类中使用此方法来保存位置:
创建一个类来保存/检索首选项值:
import android.content.Context;
public final class Prefs {
public static void setWPSelected(Context context, int index_wallpaper){
context.getSharedPreferences("MY_PREFS", 0).edit().putInt("wallpaper_selected", index_wallpaper).commit();
}
public static int getWPSelected(Context context){
return context.getSharedPreferences("MY_PREFS", 0).getInt("wallpaper_selected", 0);
}
}
保存位置:
...
...
...
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Prefs.setWPSelected(getApplicationContext(), position); //* Saving position
switch(position){
case 0:
...
...
...
检索职位:
...
...
...
@Override public void onReceive(Context context,Intent intent){
try{
DisplayMetrics metrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(metrics);
int position = Prefs.getWPSelected(context); //*retrieving position.
...
...
...