android常用代码
1、图片旋转:
Bitmap bitmapOrg = BitmapFactory.decodeResource(this.getContext().getResources(), R.drawable.moon);
Matrix matrix = new Matrix();
matrix.postRotate(-90);//旋转的角度
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
bitmapOrg.getWidth(), bitmapOrg.getHeight(), matrix, true);
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
2、获取手机号码:
//创建电话管理
TelephonyManager tm = (TelephonyManager)
//与手机建立连接
activity.getSystemService(Context.TELEPHONY_SERVICE);
//获取手机号码
String phoneId = tm.getLine1Number();
//记得在manifest file中添加
<uses-permission
Android:name="android.permission.READ_PHONE_STATE" />
//程序在模拟器上无法实现,必须连接手机
3.格式化string.xml 中的字符串:
// in strings.xml..
<string name="my_text">Thanks for visiting %s. You age is %d!</string>
// and in the java code:
String.format(getString(R.string.my_text), "oschina", 33);
4、Android设置全屏的方法:
A.在java代码中设置
/** 全屏设置,隐藏窗口所有装饰 */
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
B、在AndroidManifest.xml中配置
<activity android:name=".Login.NetEdit" android:label="@string/label_net_Edit"
android:screenOrientation="portrait" android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.Net_Edit" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
5、设置Activity为Dialog的形式:
在AndroidManifest.xml中配置Activity节点是配置theme如下:
Android:theme="@android:style/Theme.Dialog"
6、检查当前网络是否连上:
ConnectivityManager con=(ConnectivityManager)getSystemService(Activity.CONNECTIVITY_SERVICE);
boolean wifi=con.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
boolean internet=con.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
在AndroidManifest.xml 增加权限:
<uses-permission Android:name="android.permission.ACCESS_NETWORK_STATE" />
7、检测某个Intent是否有效:
public static boolean isIntentAvailable(Context context, String action) {
final PackageManager packageManager = context.getPackageManager();
相关新闻>>
- 发表评论
-
- 最新评论 更多>>