android常用小工具聚餐(不定期part)
2016-10-03 22:19
239 查看
一:获取LayoutInflater对象三种方法:
1、LayoutInflater inflater=getLayoutInflater();
2、LayoutInflaterinflater(LayoutInflater)mContext.getSystemServic(LAYOUT_INFLATER_SERVICE);
3、LayoutInflater inflater=LayoutInflater.from(context);
二:为TextView添加斜线
中间加横线:
textView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG );
底部加横线:
textView.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG );
三:状态栏和标题栏
隐藏状态栏:
final Window window = getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
隐藏标题栏:
requestWindowFeature(Window.FEATURE_NO_TITLE);
四:清空手机上cookie
CookieSyncManager.createInstance(getApplicationContext());
CookieManager.getInstance().removeAllCookie();
五:自定义标题:
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); //声明使用自定义标题
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);//自定义布局赋值
六:剪切板:
//获取剪贴板管理服务
ClipboardManager cm=(ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
//将文本数据复制到剪贴板
cm.setText(message);
//读取剪贴板数据
cm.getText();
七:定时执行service
AlarmManager am =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, MyService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent,0);
long interval = DateUtils.MINUTE_IN_MILLIS * 30;
long firstWake = System.currentTimeMillis() + interval;
am.setRepeating(AlarmManager.RTC,firstWake, interval, pendingIntent);
八:设置透明度(这是窗体本身的透明度,非背景)
WindowManager.LayoutParamslp=getWindow().getAttributes();
lp.alpha=0.3f;
getWindow().setAttributes(lp);
//alpha在0.0f到1.0f之间。1.0完全不透明,0.0f完全透明
九:设置黑暗度
WindowManager.LayoutParamslp=getWindow().getAttributes();
lp.dimAmount=0.5f;
getWindow().setAttributes(lp);
get
//dimAmount在0.0f和1.0f之间,0.0f完全不暗,1.0f全暗
设置背景模糊
getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
//以上设置对dialog对话框同样有效
十、获取手机屏幕分辨率
一、在activity中
1、DisplayMetrics dm = getResources().getDisplayMetrics();
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels;
2、DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels;
二、在service中
DisplayMetrics dm = new DisplayMetrics();
dm = getResources().getDisplayMetrics();
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels;
三、在非activity中,传一个Context进来调用
public static int[] getScreen(Context c) {
int screenWidth=0; int
screenHeight;
DisplayMetrics dm = c.getResources().getDisplayMetrics();
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels;
return new int[]{screenWidth,screenHeight }
}
十一:安装卸载
安装apk文件
private void installAPK(File file) {
Intent intent =newIntent(Intent.ACTION_VIEW);
Uri data =Uri.fromFile(file);
String type="application/vnd.android.package-archive";
intent.setDataAndType(data,type);
startActivity(intent);
}
卸载apk文件
private void uninstallAPK(String packageName){
Intent intent =newIntent(Intent.ACTION_VIEW);
Uri data =Uri.parse("package:"+ packageName);
intent.setData(data);
startActivity(intent);
}
十二:编辑图片大小,保持图片不变形。
public static Bitmap resetImage(BitmapsourceBitmap,int
resetWidth,int resetHeight){
int width =sourceBitmap.getWidth();
int height =sourceBitmap.getHeight();
int tmpWidth;
int tmpHeight;
float scaleWidth =(float)resetWidth / (float)width;
float scaleHeight =(float)resetHeight / (float)height;
float maxTmpScale = scaleWidth>= scaleHeight ? scaleWidth : scaleHeight;
//保持不变形
tmpWidth = (int)(maxTmpScale*width);
tmpHeight = (int)(maxTmpScale*height);
Matrix m = newMatrix();
m.setScale(maxTmpScale,maxTmpScale,tmpWidth, tmpHeight);
sourceBitmap=Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(),sourceBitmap.getHeight(),m,false);
//切图
int x = (tmpWidth -resetWidth)/2;
int y =(tmpHeight -resetHeight)/2;
return Bitmap.createBitmap(sourceBitmap,x, y, resetWidth,resetHeight);
}
十三:从SIM卡中获取联系人
private Cursor getContacts() {
Uri uri =Uri.parse("content://sim/adn");
String[] projection = new String[] { "name", "phone" };
String selection = null;
String[] selectionArgs = null;
String sortOrder = null;
return managedQuery(uri, projection, selection, selectionArgs,sortOrder);
}
十四:检查网络是否连接
public boolean checkIntent(){
ConnectivityManagermannager=(ConnectivityManager)
this.getSystemService(CONNECTIVITY_SERVICE);
NetworkInfoinfo=mannager.getActiveNetworkInfo();
if(info==null ||!info.isConnected()){
return false;
}
if(info.isRoaming()){
return true;
}
return true;
}
需要权限:<uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
十五:开机自动启动
public class BootReceiverextends BroadcastReceiver{
private PendingIntent mAlarmSender;
@Override
publicvoid onReceive(Context context, Intent intent) {
// 在这里干你想干的事(启动一个Service,Activity等),本例是启动一个定时调度程序, 每30分钟启动一个Service去更新数据
mAlarmSender =PendingIntent.getService(context, 0,
newIntent(context,
RefreshDataService.class), 0);
long firstTime = SystemClock.elapsedRealtime();
AlarmManageram=(AlarmManager)context.getSystemService(Activity.ALARM_SERVICE);
am.cancel(mAlarmSender);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime,30*60*1000,
mAlarmSender);
}
}
清单文件配置:
<receiver android:name=".service.BootReceiver">
<intent-filter>
<!-- 系统启动完成后会调用-->
<action
android:name="android.intent.action.BOOT_COMPLETED">
</action>
</intent-filter>
</receiver>
十六:获取系统版本号:
PackageInfo info =this.getPackageManager().getPackageInfo(this.getPackageName(), 0);
intversionCode=nfo.versionCode;
string versionName=info.versionNam;
十七:应用程序动态全屏和退出全屏
让程序全屏的方法,大家都知道,那是静态的,程序运行之初就申明了。但是如果有这样的需求:要在程序运行的过程中,执行了某个操作而使之全屏,然后还需要退出全屏,怎么做?
如下:
WindowManager.LayoutParamsattrs = getWindow().getAttributes();
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setAttributes(attrs);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
修改window的LayoutParams参数,然后加上FLAG_LAYOUT_NO_LIMITS标志,就OK了。window会自动重新布局,呈现全屏的状态。
要退出全屏,只需要清除刚才加上的FLAG_FULLSCREEN参数,然后去掉FLAG_LAYOUT_NO_LIMITS标志。
如下
WindowManager.LayoutParamsattrs = getWindow().getAttributes();
attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setAttributes(attrs);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
十八:获取安装路径和已安装程序列表
android中获取当前程序路径
getApplicationContext().getFilesDir().getAbsolutePath()
(2)android取已安装的程序列表
List<PackageInfo>packageInfoList = getPackageManager().getInstalledPackages(0);
十九:不让文本框输入中文
android:digits="1234567890qwertyuiopasdfghjklzxcvbnm`-=[];,./~!@#$%^*()_+}{:?&<>"'"
二十:android中调用其它android应用
ComponentName comp = newComponentName("com.Test","com.login.Main");
intent = new Intent();
intent.setComponent(comp);
intent.setAction("android.intent.action.VIEW");
startActivity(intent);
二十一:获取设备型号、SDK版本及系统版本
String device_model= Build.MODEL; //设备型号
String version_sdk= Build.VERSION.SDK; //设备SDK版本
Stringversion_release = Build.VERSION.RELEASE; //设备的系统版本
二十二、获取手机屏幕分辨率
DisplayMetrics dm = new DisplayMereics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
float width = dm.widthPixels *dm.density;
float height = dm.heightPixels *dm.density
二十三:android阴影字体设置
<TextView
android:id="@+id/tvText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text1"
android:textSize="30sp"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:shadowColor="#ff0000ff"
android:shadowDx="5"
android:shadowDy="5"
android:shadowRadius="10"/>
android:shadowColor 阴影颜色
android:shadowDx 阴影的水平偏移量
android:shadowDy 阴影的垂直偏移量
android:shadowRadius 阴影的范围
为了统一风格和代码的复用,通常可以把这个样式抽取放入到style.xml文件中
<?xmlversion="1.0" encoding="utf-8"?>
<resources>
<stylename="textstyle">
<item name="android:shadowColor">#ff0000ff</item>
<itemname="android:shadowRadius">10</item>
<itemname="android:shadowDx">5</item>
<itemname="android:shadowDy">5</item>
</style>
</resources>
二十四:android实现手机震动功能
public class TipHelper {
public static void Vibrate(final Activityactivity, long milliseconds) {
Vibrator vib = (Vibrator)activity.getSystemService(Service.VIBRATOR_SERVICE);
vib.vibrate(milliseconds);
}
public static void Vibrate(final Activityactivity, long[] pattern,boolean isRepeat) {
Vibrator vib = (Vibrator)activity.getSystemService(Service.VIBRATOR_SERVICE);
vib.vibrate(pattern, isRepeat ? 1 :-1);
}
}
需要权限:<uses-permissionandroid:name="android.permission.VIBRATE" />
longmilliseconds:震动的时长,单位是毫秒
long[] pattern :自定义震动模式。数组中数字的含义依次是[静止时长,震动时长,静止时长,震动时长。。。]时长的单位是毫秒
boolean isRepeat :是否反复震动,如果是true,反复震动,如果是false,只震动一次
二十五:获取移动设备的IP地址:
public class Tools {
public static String getLocalIpAddress(){
try {
for(Enumeration<NetworkInterface> en =NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf =en.nextElement();
for(Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();enumIpAddr.hasMoreElements();) {
InetAddress inetAddress =enumIpAddr.nextElement();
if(!inetAddress.isLoopbackAddress()) {
returninetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e("出错啦", ex.toString());
}
return null;
}
}
然后
WifiManager wm =(WifiManager)getSystemService(WIFI_SERVICE);
WifiInfo wi =wm.getConnectionInfo();
System.out.println("IP地址是:"+Tools.getLocalIpAddress());
System.out.println("SSID:"+wi.getSSID());
最后记得加两个权限
<uses-permissionandroid:name="android.permission.INTERNET"/>
<uses-permissionandroid:name="android.permission.ACCESS_WIFI_STATE"/>
二十六:Android按下back键非退出隐藏到后台
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK){
Intent intent = newIntent(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
return true;
}
return super.onKeyDown(keyCode,event);
}
二十七:获取view在屏幕中的位置
int[] points = { 0,0 };
view.getLocationInWindow(points);
二十八:Android创建桌面快捷方式
/**
*为程序创建桌面快捷方式
*/
private void addShortcut(){
Intent shortcut = newIntent("com.android.launcher.action.INSTALL_SHORTCUT");
//快捷方式的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,getString(R.string.app_name));
shortcut.putExtra("duplicate",false); //不允许重复创建
//指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer
//注意: ComponentName的第二个参数必须加上点号(.),否则快捷方式无法启动相应程序
ComponentName comp = newComponentName(this.getPackageName(),"."+this.getLocalClassName());
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,new Intent(Intent.ACTION_MAIN).setComponent(comp));
//快捷方式的图标
ShortcutIconResource iconRes =Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
sendBroadcast(shortcut);
}
需要声明权限:<uses-permissionandroid:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
二十九:使用Shape代替图片效果
<?xmlversion="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<itemandroid:state_pressed="true" >
<shape>
<gradient
android:startColor="#ff8c00"
android:endColor="#FFFFFF"
android:angle="270"/>
<stroke
android:width="2dp"
android:color="#dcdcdc" />
<corners
android:radius="2dp"/>
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp"/>
</shape>
</item> <itemandroid:state_focused="true" >
<shape>
<gradient
android:startColor="#ffc2b7"
android:endColor="#ffc2b7"
android:angle="270"/>
<stroke
android:width="2dp"
android:color="#dcdcdc" />
<corners
android:radius="2dp"/>
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp"/>
</shape>
</item> <item>
<shape>
<gradient
android:startColor="#ff9d77"
android:endColor="#ff9d77"
android:angle="270"/>
<stroke
android:width="2dp"
android:color="#fad3cf" />
<corners
android:radius="2dp"/>
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp"/>
</shape>
</item>
</selector>
1、LayoutInflater inflater=getLayoutInflater();
2、LayoutInflaterinflater(LayoutInflater)mContext.getSystemServic(LAYOUT_INFLATER_SERVICE);
3、LayoutInflater inflater=LayoutInflater.from(context);
二:为TextView添加斜线
中间加横线:
textView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG );
底部加横线:
textView.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG );
三:状态栏和标题栏
隐藏状态栏:
final Window window = getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
隐藏标题栏:
requestWindowFeature(Window.FEATURE_NO_TITLE);
四:清空手机上cookie
CookieSyncManager.createInstance(getApplicationContext());
CookieManager.getInstance().removeAllCookie();
五:自定义标题:
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); //声明使用自定义标题
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);//自定义布局赋值
六:剪切板:
//获取剪贴板管理服务
ClipboardManager cm=(ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
//将文本数据复制到剪贴板
cm.setText(message);
//读取剪贴板数据
cm.getText();
七:定时执行service
AlarmManager am =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, MyService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent,0);
long interval = DateUtils.MINUTE_IN_MILLIS * 30;
long firstWake = System.currentTimeMillis() + interval;
am.setRepeating(AlarmManager.RTC,firstWake, interval, pendingIntent);
八:设置透明度(这是窗体本身的透明度,非背景)
WindowManager.LayoutParamslp=getWindow().getAttributes();
lp.alpha=0.3f;
getWindow().setAttributes(lp);
//alpha在0.0f到1.0f之间。1.0完全不透明,0.0f完全透明
九:设置黑暗度
WindowManager.LayoutParamslp=getWindow().getAttributes();
lp.dimAmount=0.5f;
getWindow().setAttributes(lp);
get
//dimAmount在0.0f和1.0f之间,0.0f完全不暗,1.0f全暗
设置背景模糊
getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
//以上设置对dialog对话框同样有效
十、获取手机屏幕分辨率
一、在activity中
1、DisplayMetrics dm = getResources().getDisplayMetrics();
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels;
2、DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels;
二、在service中
DisplayMetrics dm = new DisplayMetrics();
dm = getResources().getDisplayMetrics();
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels;
三、在非activity中,传一个Context进来调用
public static int[] getScreen(Context c) {
int screenWidth=0; int
screenHeight;
DisplayMetrics dm = c.getResources().getDisplayMetrics();
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels;
return new int[]{screenWidth,screenHeight }
}
十一:安装卸载
安装apk文件
private void installAPK(File file) {
Intent intent =newIntent(Intent.ACTION_VIEW);
Uri data =Uri.fromFile(file);
String type="application/vnd.android.package-archive";
intent.setDataAndType(data,type);
startActivity(intent);
}
卸载apk文件
private void uninstallAPK(String packageName){
Intent intent =newIntent(Intent.ACTION_VIEW);
Uri data =Uri.parse("package:"+ packageName);
intent.setData(data);
startActivity(intent);
}
十二:编辑图片大小,保持图片不变形。
public static Bitmap resetImage(BitmapsourceBitmap,int
resetWidth,int resetHeight){
int width =sourceBitmap.getWidth();
int height =sourceBitmap.getHeight();
int tmpWidth;
int tmpHeight;
float scaleWidth =(float)resetWidth / (float)width;
float scaleHeight =(float)resetHeight / (float)height;
float maxTmpScale = scaleWidth>= scaleHeight ? scaleWidth : scaleHeight;
//保持不变形
tmpWidth = (int)(maxTmpScale*width);
tmpHeight = (int)(maxTmpScale*height);
Matrix m = newMatrix();
m.setScale(maxTmpScale,maxTmpScale,tmpWidth, tmpHeight);
sourceBitmap=Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(),sourceBitmap.getHeight(),m,false);
//切图
int x = (tmpWidth -resetWidth)/2;
int y =(tmpHeight -resetHeight)/2;
return Bitmap.createBitmap(sourceBitmap,x, y, resetWidth,resetHeight);
}
十三:从SIM卡中获取联系人
private Cursor getContacts() {
Uri uri =Uri.parse("content://sim/adn");
String[] projection = new String[] { "name", "phone" };
String selection = null;
String[] selectionArgs = null;
String sortOrder = null;
return managedQuery(uri, projection, selection, selectionArgs,sortOrder);
}
十四:检查网络是否连接
public boolean checkIntent(){
ConnectivityManagermannager=(ConnectivityManager)
this.getSystemService(CONNECTIVITY_SERVICE);
NetworkInfoinfo=mannager.getActiveNetworkInfo();
if(info==null ||!info.isConnected()){
return false;
}
if(info.isRoaming()){
return true;
}
return true;
}
需要权限:<uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
十五:开机自动启动
public class BootReceiverextends BroadcastReceiver{
private PendingIntent mAlarmSender;
@Override
publicvoid onReceive(Context context, Intent intent) {
// 在这里干你想干的事(启动一个Service,Activity等),本例是启动一个定时调度程序, 每30分钟启动一个Service去更新数据
mAlarmSender =PendingIntent.getService(context, 0,
newIntent(context,
RefreshDataService.class), 0);
long firstTime = SystemClock.elapsedRealtime();
AlarmManageram=(AlarmManager)context.getSystemService(Activity.ALARM_SERVICE);
am.cancel(mAlarmSender);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime,30*60*1000,
mAlarmSender);
}
}
清单文件配置:
<receiver android:name=".service.BootReceiver">
<intent-filter>
<!-- 系统启动完成后会调用-->
<action
android:name="android.intent.action.BOOT_COMPLETED">
</action>
</intent-filter>
</receiver>
十六:获取系统版本号:
PackageInfo info =this.getPackageManager().getPackageInfo(this.getPackageName(), 0);
intversionCode=nfo.versionCode;
string versionName=info.versionNam;
十七:应用程序动态全屏和退出全屏
让程序全屏的方法,大家都知道,那是静态的,程序运行之初就申明了。但是如果有这样的需求:要在程序运行的过程中,执行了某个操作而使之全屏,然后还需要退出全屏,怎么做?
如下:
WindowManager.LayoutParamsattrs = getWindow().getAttributes();
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setAttributes(attrs);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
修改window的LayoutParams参数,然后加上FLAG_LAYOUT_NO_LIMITS标志,就OK了。window会自动重新布局,呈现全屏的状态。
要退出全屏,只需要清除刚才加上的FLAG_FULLSCREEN参数,然后去掉FLAG_LAYOUT_NO_LIMITS标志。
如下
WindowManager.LayoutParamsattrs = getWindow().getAttributes();
attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setAttributes(attrs);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
十八:获取安装路径和已安装程序列表
android中获取当前程序路径
getApplicationContext().getFilesDir().getAbsolutePath()
(2)android取已安装的程序列表
List<PackageInfo>packageInfoList = getPackageManager().getInstalledPackages(0);
十九:不让文本框输入中文
android:digits="1234567890qwertyuiopasdfghjklzxcvbnm`-=[];,./~!@#$%^*()_+}{:?&<>"'"
二十:android中调用其它android应用
ComponentName comp = newComponentName("com.Test","com.login.Main");
intent = new Intent();
intent.setComponent(comp);
intent.setAction("android.intent.action.VIEW");
startActivity(intent);
二十一:获取设备型号、SDK版本及系统版本
String device_model= Build.MODEL; //设备型号
String version_sdk= Build.VERSION.SDK; //设备SDK版本
Stringversion_release = Build.VERSION.RELEASE; //设备的系统版本
二十二、获取手机屏幕分辨率
DisplayMetrics dm = new DisplayMereics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
float width = dm.widthPixels *dm.density;
float height = dm.heightPixels *dm.density
二十三:android阴影字体设置
<TextView
android:id="@+id/tvText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text1"
android:textSize="30sp"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:shadowColor="#ff0000ff"
android:shadowDx="5"
android:shadowDy="5"
android:shadowRadius="10"/>
android:shadowColor 阴影颜色
android:shadowDx 阴影的水平偏移量
android:shadowDy 阴影的垂直偏移量
android:shadowRadius 阴影的范围
为了统一风格和代码的复用,通常可以把这个样式抽取放入到style.xml文件中
<?xmlversion="1.0" encoding="utf-8"?>
<resources>
<stylename="textstyle">
<item name="android:shadowColor">#ff0000ff</item>
<itemname="android:shadowRadius">10</item>
<itemname="android:shadowDx">5</item>
<itemname="android:shadowDy">5</item>
</style>
</resources>
二十四:android实现手机震动功能
public class TipHelper {
public static void Vibrate(final Activityactivity, long milliseconds) {
Vibrator vib = (Vibrator)activity.getSystemService(Service.VIBRATOR_SERVICE);
vib.vibrate(milliseconds);
}
public static void Vibrate(final Activityactivity, long[] pattern,boolean isRepeat) {
Vibrator vib = (Vibrator)activity.getSystemService(Service.VIBRATOR_SERVICE);
vib.vibrate(pattern, isRepeat ? 1 :-1);
}
}
需要权限:<uses-permissionandroid:name="android.permission.VIBRATE" />
longmilliseconds:震动的时长,单位是毫秒
long[] pattern :自定义震动模式。数组中数字的含义依次是[静止时长,震动时长,静止时长,震动时长。。。]时长的单位是毫秒
boolean isRepeat :是否反复震动,如果是true,反复震动,如果是false,只震动一次
二十五:获取移动设备的IP地址:
public class Tools {
public static String getLocalIpAddress(){
try {
for(Enumeration<NetworkInterface> en =NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf =en.nextElement();
for(Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();enumIpAddr.hasMoreElements();) {
InetAddress inetAddress =enumIpAddr.nextElement();
if(!inetAddress.isLoopbackAddress()) {
returninetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e("出错啦", ex.toString());
}
return null;
}
}
然后
WifiManager wm =(WifiManager)getSystemService(WIFI_SERVICE);
WifiInfo wi =wm.getConnectionInfo();
System.out.println("IP地址是:"+Tools.getLocalIpAddress());
System.out.println("SSID:"+wi.getSSID());
最后记得加两个权限
<uses-permissionandroid:name="android.permission.INTERNET"/>
<uses-permissionandroid:name="android.permission.ACCESS_WIFI_STATE"/>
二十六:Android按下back键非退出隐藏到后台
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK){
Intent intent = newIntent(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
return true;
}
return super.onKeyDown(keyCode,event);
}
二十七:获取view在屏幕中的位置
int[] points = { 0,0 };
view.getLocationInWindow(points);
二十八:Android创建桌面快捷方式
/**
*为程序创建桌面快捷方式
*/
private void addShortcut(){
Intent shortcut = newIntent("com.android.launcher.action.INSTALL_SHORTCUT");
//快捷方式的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,getString(R.string.app_name));
shortcut.putExtra("duplicate",false); //不允许重复创建
//指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer
//注意: ComponentName的第二个参数必须加上点号(.),否则快捷方式无法启动相应程序
ComponentName comp = newComponentName(this.getPackageName(),"."+this.getLocalClassName());
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,new Intent(Intent.ACTION_MAIN).setComponent(comp));
//快捷方式的图标
ShortcutIconResource iconRes =Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
sendBroadcast(shortcut);
}
需要声明权限:<uses-permissionandroid:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
二十九:使用Shape代替图片效果
<?xmlversion="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<itemandroid:state_pressed="true" >
<shape>
<gradient
android:startColor="#ff8c00"
android:endColor="#FFFFFF"
android:angle="270"/>
<stroke
android:width="2dp"
android:color="#dcdcdc" />
<corners
android:radius="2dp"/>
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp"/>
</shape>
</item> <itemandroid:state_focused="true" >
<shape>
<gradient
android:startColor="#ffc2b7"
android:endColor="#ffc2b7"
android:angle="270"/>
<stroke
android:width="2dp"
android:color="#dcdcdc" />
<corners
android:radius="2dp"/>
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp"/>
</shape>
</item> <item>
<shape>
<gradient
android:startColor="#ff9d77"
android:endColor="#ff9d77"
android:angle="270"/>
<stroke
android:width="2dp"
android:color="#fad3cf" />
<corners
android:radius="2dp"/>
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp"/>
</shape>
</item>
</selector>
相关文章推荐
- Android开发常用工具类(不定期进行更新)
- Android 常用工具命令
- Android 常用的方法小工具总结
- android,eclipse常用工具
- 常用的ubuntu第三方工具及android命令
- 常用开源工具介绍(不定期更新)
- Android常用工具
- Android SQLite3工具常用命令行总结
- android UI布局优化的常用工具
- Android常用技术、常用工具和开源项目
- Android常用开发工具的用法
- android 常用UI使用工具代码片段
- 常用的Android开发工具
- zdz工具箱v1.5 android版本发布了,集成各种个人生活中常用的工具,方便日常使用管理
- Android学习(2)-Android的常用工具
- Android调试.so库常用工具
- linux环境下android常用工具和技巧
- Android 常用代码工具
- Android开发常用工具收集
- 第3.1.2节 android常用的工具