日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android常用代码合集,Android开发常用经典代码段集锦

發布時間:2023/12/20 Android 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android常用代码合集,Android开发常用经典代码段集锦 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文實例總結了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中添加

android:name="android.permission.READ_PHONE_STATE" />

//程序在模擬器上無法實現,必須連接手機

3.格式化string.xml 中的字符串

// in strings.xml..

Thanks for visiting %s. You age is %d!

// 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中配置

android:screenOrientation="portrait" android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">

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 增加權限:

7、檢測某個Intent是否有效

public static boolean isIntentAvailable(Context context, String action) {

final PackageManager packageManager = context.getPackageManager();

final Intent intent = new Intent(action);

List list =

packageManager.queryIntentActivities(intent,

PackageManager.MATCH_DEFAULT_ONLY);

return list.size() > 0;

}

8、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);

}

9、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."));

10、android中打開瀏覽器

Intent viewIntent = new

Intent("android.intent.action.VIEW",Uri.parse("http://vaiyanzi.cnblogs.com"));

startActivity(viewIntent);

11、android 獲取設備唯一標識碼

String android_id = Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID);

12、android中獲取IP地址

public String getLocalIpAddress() {

try {

for (Enumeration en = NetworkInterface.getNetworkInterfaces();

en.hasMoreElements();) {

NetworkInterface intf = en.nextElement();

for (Enumeration 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;

}

13、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;

}

15、查看電池使用情況

Intent intentBatteryUsage = new Intent(Intent.ACTION_POWER_USAGE_SUMMARY);

startActivity(intentBatteryUsage);

16、獲取進程號

ActivityManager mActivityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);

List mRunningProcess = mActivityManager.getRunningAppProcesses();

int i = 1;

for (ActivityManager.RunningAppProcessInfo amProcess : mRunningProcess) {

Log.e("homer Application", (i++) + " PID = " + amProcess.pid + "; processName = " + amProcess.processName);

}

希望本文所述對大家Android程序設計有所幫助。

總結

以上是生活随笔為你收集整理的android常用代码合集,Android开发常用经典代码段集锦的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。