二十一、获取手机屏幕分辨率
- DisplayMetrics dm = new DisplayMereics();
- getWindowManager().getDefaultDisplay().getMetrics(dm);
- float width = dm.widthPixels * dm.density;
- float height = dm.heightPixels * dm.density
在这里问什么要乘以 dm.density 了,是因为通过dm.widthPixels的到的结果始终是320,不是真实的屏幕分辨率,所以要乘以dm.density得到真实的分辨率。
二十二、在Activity里面播放背景音乐
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.mainlay);
- mediaPlayer = MediaPlayer.create(this, R.raw.mu);
- mediaPlayer.setLooping(true);
- mediaPlayer.start();
- }
二十三、让程序的界面不随机器的重力感应而翻转
第一种方法,在manifast文件里面
- <activity
- android:screenOrientation="portrait">
- </activity>
第二种,在代码里面
- setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
二十四、使activity全屏显示
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,
-
WindowManager.LayoutParams. FLAG_FULLSCREEN);
二十五、在RelativeLayout中使selector要注意点
关于selector的使用方法,可以参考http://blog.csdn.net/aomandeshangxiao/article/details/6759576这篇文章,今天,遇到在RelativeLayout中添加background为selector后没有反应的问题,寻摸了很长时间,一直没有找到原因,其实只要加上一句代码就完全可以解决:
- <span style="font-size:16px;">RelativeLayout 里面加上android:clickable="true"</span>
这样,RelativLayout就会出现在selector里面定义的效果。
二十六、显示或隐藏虚拟键盘
- 显示:
- InputMethodManager imm = (InputMethodManager)(getSystemService(Context.INPUT_METHOD_SERVICE));
- imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
- 隐藏:
- InputMethodManager imm = (InputMethodManager)(getSystemService(Context.INPUT_METHOD_SERVICE));
- imm.hideSoftInputFromWindow(m_edit.getWindowToken(), 0);
二十七、退出程序时清除通知中信息
- NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
- nm.cancelAll();
二十八、创建快捷方式
- Intent intent=new Intent();
- //设置快捷方式的图标
- intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.img));
- //设置快捷方法的名称
- intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "点击启动哥的程序"); //设置点击快键图标的响应操作
- intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(this,MainActivity.class));