主要代码如下:
in Launcher.java: private static final int MENU_GROUP_WALLPAPER = 1; private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1; private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1; private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1; private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1; private static final int MENU_GROUP_BACKGROUND= 2; //这里引入背景设置按钮组 private static final int MENU_BACKGROUND_SETTINGS = MENU_SYSTEM_SETTINGS + 1; ... private static final int REQUEST_PICK_BACKGROUND = 11; //为将来的startActivityForResult设定标签 ... @Override protected void onActivityResult(final int requestCode, int resultCode, final Intent data) { //重写onActivityResult方法,在其中修改背景图片 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) { final PendingAddArguments args = new PendingAddArguments(); args.requestCode = requestCode; args.intent = data; args.container = mPendingAddInfo.container; args.screen = mPendingAddInfo.screen; args.cellX = mPendingAddInfo.cellX; args.cellY = mPendingAddInfo.cellY; if (isWorkspaceLocked()) { sPendingAddList.add(args); } else { delayExitSpringLoadedMode = completeAdd(args); } }else if((requestCode) == REQUEST_PICK_BACKGROUND){ Bundle b = data.getExtras(); int num = b.getInt("wallpaper"); //获得特定的返回标签值 if(1 == num){ AppsCustomizeTabHost ACT = (AppsCustomizeTabHost)findViewById(R.id.apps_customize_pane); //获得背景 LinearLayout ll = (LinearLayout)ACT.findViewById(R.id.apps_customize_content); //的布局文件 ll.setBackgroundResource(R.drawable.wallpaper_bubblegum); //设定背景为wallpaper_bubblegum } } ... @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case MENU_WALLPAPER_SETTINGS: startWallpaper(); return true; case MENU_BACKGROUND_SETTINGS: // 监听按钮事件 startBackground(); return true; } return super.onOptionsItemSelected(item); } ... private void startBackground(){ //发送Intent,转入settings界面 final Intent pickBackground = new Intent(Launcher.this, BackgroundSettings.class); startActivityForResult(pickBackground,REQUEST_PICK_BACKGROUND); //使用startActivityForResult用来获得选择背景图片的信息 }
private Intent intent; private Button button; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.background_settings); button = (Button)findViewById(R.id.background_to_wallpaper); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub intent = new Intent(BackgroundSettings.this, Launcher.class); intent.putExtra("wallpaper", 1); //添加一对返回用的键值 setResult(RESULT_OK,intent); //设定返回值 finish(); //finish之后intent就会被发出,然后Launcher总的onActivityResult就会被调用 } }); } @Override public void onBackPressed() { //如果这里不重写onBackPressed方法,就会在按动返回按钮时使整个Launcher应用崩溃。。。 // TODO Auto-generated method stub intent = new Intent(BackgroundSettings.this, Launcher.class); intent.putExtra("wallpaper", 0); setResult(RESULT_OK,intent); finish(); }
由于我是在两个手机上弄的对比,一个是中文,一个是纯英文的。。。
现在只是加了一张图片用来实现功能,以后有时间了会将系统的wallpaperchooser界面导入到backgroundsetting中,这样就会美观一些。
接下来就是学习拖放功能的实现以及service监听的具体应用,然后完善这个小程序了。