一、圖片旋轉
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); java
二、獲取手機號碼
//建立電話管理 android
TelephonyManager tm = (TelephonyManager) 瀏覽器
//與手機創建鏈接
activity.getSystemService(Context.TELEPHONY_SERVICE); 網絡
//獲取手機號碼 app
String phoneId = tm.getLine1Number(); oop
//記得在manifest file中添加
<uses-permission
android:name="android.permission.READ_PHONE_STATE" /> post
//程序在模擬器上沒法實現,必須鏈接手機 this
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); .net
四、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> code
五、設置Activity爲Dialog的形式
在AndroidManifest.xml中配置Activity節點是配置theme以下:
android:theme="@android :style/Theme.Dialog"
六、檢查當前網絡是否連上
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" />
七、檢測某個Intent是否有效
public static boolean isIntentAvailable(Context context, String action) {
final PackageManager packageManager = context.getPackageManager();
final Intent intent = new Intent(action);
List<ResolveInfo> list =
packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
八、android 撥打電話
try {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:+110"));
startActivity(intent);
} catch (Exception e) {
Log.e("SampleApp", "Failed to invoke call", e);
}
九、android中發送Email
Intent i = new Intent(Intent.ACTION_SEND);
//i.setType("text/plain"); //模擬器請使用這行
i.setType("message/rfc822") ; // 真機上使用這行
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"test@gmail.com","test@163.com});
i.putExtra(Intent.EXTRA_SUBJECT,"subject goes here");
i.putExtra(Intent.EXTRA_TEXT,"body goes here");
startActivity(Intent.createChooser(i, "Select email application."));
十、android中打開瀏覽器
Intent viewIntent = new
Intent("android.intent.action.VIEW",Uri.parse("http://vaiyanzi.cnblogs.com"));
startActivity(viewIntent);
十一、android 獲取設備惟一標識碼
String android_id = Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID);
十二、android中獲取IP地址
public 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()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e(LOG_TAG, ex.toString());
}
return null;
}
1三、android獲取存儲卡路徑以及使用狀況
/** 獲取存儲卡路徑 */
File sdcardDir=Environment.getExternalStorageDirectory();
/** StatFs 看文件系統空間使用狀況 */
StatFs statFs=new StatFs(sdcardDir.getPath());
/** Block 的 size*/
Long blockSize=statFs.getBlockSize();
/** 總 Block 數量 */
Long totalBlocks=statFs.getBlockCount();
/** 已使用的 Block 數量 */
Long availableBlocks=statFs.getAvailableBlocks();
14 android中添加新的聯繫人
private Uri insertContact(Context context, String name, String phone) {
ContentValues values = new ContentValues();
values.put(People.NAME, name);
Uri uri = getContentResolver().insert(People.CONTENT_URI, values);
Uri numberUri = Uri.withAppendedPath(uri, People.Phones.CONTENT_DIRECTORY);
values.clear();
values.put(Contacts.Phones.TYPE, People.Phones.TYPE_MOBILE);
values.put(People.NUMBER, phone);
getContentResolver().insert(numberUri, values);
return uri;
}
1五、查看電池使用狀況 Intent intentBatteryUsage = new Intent(Intent.ACTION_POWER_USAGE_SUMMARY); startActivity(intentBatteryUsage);