導航:首頁 > 操作系統 > android常用代碼

android常用代碼

發布時間:2022-11-20 14:53:46

A. android app開發中常用到哪些開源框架

在前面的課程中,隨著對Android體系的了解,已經可以進行正常的Android應用開發了。在Android開發中,同其他工程開發一樣,也經常使用一些提高效率的框架,本文我們做一個對比。這些框架,既包括:網路請求框架、也包括圖片載入庫框架、還包括資料庫操作等一些框架,總之,了解和熟悉這些框架,會對自己的開發效率有很大的提升和幫助。

網路請求框架

1、okHttp

在前文的學習中,我們已經了解過okHttp,是一個常用的網路載入庫。

2、Retrofit

介紹

Retrofit是一個很不錯的網路請求庫,該庫是square開源的另外一個庫,之前的okhttp也是該公司開源的。

Retrofit是基於OkHttp封裝的RESTful網路請求框架,使用註解的方式配置請求。優點是速度快,使用註解,callback函數返回結果自動包裝成java對象。官方自己的介紹說:

A type-safe REST client for Android and Java

該網路框架在github上的地址如下:https://square.github.io/retrofit/

要求

Retrofit支持的http方式方式包括 GET/POST/PUT/DELETE/HEAD/PATCH,Retrofit要求Java的版本是1.8+,Android應用的API版本應該在21+。

依賴

使用Retrofit庫,和其他庫一樣,首先需要設置依賴,依然是在build.gradle文件中設置依賴:

//添加retrofit庫依賴

implementation 『com.squareup.retrofit2:retrofit:2.1.0』

//添加gson轉換器

implementation 『com.squareup.retrofit2:converter-gson:2.1.0』

使用

通過一個例子,我們可以來演示該框架的使用步驟:

1、定義請求介面,即程序中都需要什麼請求操作
public interface HttpServices {

/**

B. android中一些代碼的名稱比如上,左,右...

top left right...你要這些嗎?

C. android 代碼

public class ApkClientActivity extends Activity {
static final String TAG = "ApkClientActivity";
Context mContext;
DownloadManager manager ;
DownloadCompleteReceiver receiver;
Button downBtn ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContext = this;
//獲取下載服務
manager =(DownloadManager)getSystemService(DOWNLOAD_SERVICE);
receiver = new DownloadCompleteReceiver();
downBtn = (Button)findViewById(R.id.downBtn);
downBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//創建下載請求
DownloadManager.Request down=new DownloadManager.Request (Uri.parse("http://192.168.0.66:8080/qqinput.apk"));
//設置允許使用的網路類型,這里是移動網路和wifi都可以
down.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE|DownloadManager.Request.NETWORK_WIFI);
//禁止發出通知,既後台下載
down.setShowRunningNotification(false);
//不顯示下載界面
down.setVisibleInDownloadsUi(false);
//設置下載後文件存放的位置
down.(mContext, null, "qqinput.apk");
//將下載請求放入隊列
manager.enqueue(down);
}
});
}
//接受下載完成後的intent
class DownloadCompleteReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)){
long downId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
Log.v(TAG," download complete! id : "+downId);
Toast.makeText(context, intent.getAction()+"id : "+downId, Toast.LENGTH_SHORT).show();
}
}
}

@Override
protected void onResume() {
registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
super.onResume();
}

@Override
protected void onDestroy() {
if(receiver != null)unregisterReceiver(receiver);
super.onDestroy();
}
}

其中在設置 down.setShowRunningNotification(false);時,需要添加相應的許可權:
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
否則,會報錯!

AndroidManifest.xml文件內容如下:

Xml代碼
<strong><uses-sdk android:minSdkVersion="9" /></strong>

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".ApkClientActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />

D. 怎麼用android編寫代碼

一、用RelativeLayout進行純代碼布局的理論基礎
1、RelativeLayout,顧名思義,就是以「相對」位置/對齊 為基礎的布局方式。
2、android.widget.RelativeLayout 有個繼承自android.view.ViewGroup.LayoutParams 的內嵌類 LayoutParams,使用這個類的實例
調用RelativeLayout.addView 就可以實現「相對布局」。 android.widget.RelativeLayout.LayoutParams 有一個構造函數:
RelativeLayout.LayoutParams(int w, int h),參數指定了子 View 的寬度和高度,這一點和其父類是一樣的。而實現相對布局的關
鍵在它的 兩個 addRule 方法上。anchor 參數指定可以是View 的 id(「相對於誰」)、RelativeLayout.TRUE(啟用某種對齊方式) 或者
是-1(應用於某些不需要 anchor 的 verb);AddRule 方法的 verb 參數指定相對的「動作」(以下常量均定義於
android.widget.RelativeLayout中,為了簡便不給出其全名):
3、ALIGN_BOTTOM、ALIGN_LEFT、 ALIGN_RIGHT、 ALIGN_TOP: 本 View 的 底邊/左邊/右邊/頂邊 和 anchor 指定的 View 的
底邊/左邊/右邊/頂邊 對齊。
ALIGN_WITH_PARENT_BOTTOM 、ALIGN_WITH_PARENT_LEFT 、 ALIGN_WITH_PARENT_RIGHT 、
ALIGN_WITH_PARENT_TOP : 和上面一組常量類似,只不過不需要再指定 anchor, 其 anchor 自動為 Parent View。
CENTER_HORIZONTAL、CENTER_IN_PARENT 、CENTER_VERTICAL : 如果 anchor 為 TRUE,在 Parent 中 水平居中/水平
和垂直均居中/垂直居中。
POSITION_ABOVE 、POSITION_BELOW 、 POSITION_TO_LEFT 、POSITION_TO_RIGHT : 本 View 位於 anchor 指定的 View
的上邊/下邊/左邊/右邊。
二、案例
1、布局文件如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF" >
<com..mapapi.map.MapView
android:id="@+id/_map_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" >
</com..mapapi.map.MapView>
<RelativeLayout
android:id="@+id/anquan_map_l1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp" >
<ImageButton
android:id="@+id/but_of_lukuang"
android:layout_width="38.0dip"
android:layout_height="38.0dip"
android:background="@drawable/main_map_button_bg"
android:src="@drawable/maptraffic_icon_off" />
<ImageButton
android:id="@+id/btn_of_bobao"
android:layout_width="38.0dip"
android:layout_height="38.0dip"
android:layout_below="@id/but_of_lukuang"
android:layout_marginTop="5dp"
android:visibility="gone"
android:background="@drawable/main_map_button_bg"
android:src="@drawable/netfriend_bobao_n" />
<ImageButton
android:id="@+id/btn_of_layer"
android:layout_width="38.0dip"
android:layout_height="38.0dip"
android:layout_below="@+id/btn_of_bobao"
android:layout_marginTop="5dp"
android:background="@drawable/main_map_button_bg"
android:src="@drawable/main_map_icon_layer" />
</RelativeLayout>
</RelativeLayout>
2、代碼如下
//得到
mapButtonRL = (RelativeLayout) findViewById(R.id.anquan_map_l1);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.BELOW, R.id.btn_of_layer);
showModeButton = new Button(this);
showModeButton.setText("全部顯示");
showModeButton.setId(SHOW_MODE);
showModeButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
mapButtonRL.addView(showModeButton, lp1);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.BELOW, SHOW_MODE);
positionButton = new Button(this);
positionButton.setText("位置");
positionButton.setId(POSITION);
positionButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
mapButtonRL.addView(positionButton, lp2);

E. android代碼求解釋

這個其實完全可以歸結為java代碼,Map<String, String> params 這個是申明一個Map對象,裡面存放鍵值對,key-value 分別是String類型 ,後面的三句put代碼,分別往裡面存放了三個鍵值對而已

F. Android 中代碼定義顏色的幾種方式

Android開發中顏色的自定義方法

1、使用Color類的常量,如:
int color = Color.BLUE; // 創建一個藍色 是使用Android提供的顏色 int color = Color.RED; int color = Color.WHITE; 2、通過ARGB構建,如:
int color = Color.argb ( 127, 255, 0, 255 ); // 半透明的紫色
其中第一個參數表示透明,0表示完全透明,255(ff)表示完全不透明;後三位分別代表RGB的值了。 3、使用XML資源文件來定義顏色
該方法擴展性好,便於修改和共享,如在values目錄下創建一個color.xml: <?xml version=」 1.0」 encoding=」utf -8」> <resources>
<color name=」mycolor」> #7fff00ff</color> </resources>
定義了一個名為mycolor的顏色,在別的地方就可以通過引用mycolor來獲取該顏色值,如textView定義中:
android:textColor= "@drawable/mycolor"
Java代碼中可以使用ResourceManager類中的getColor來獲取該顏色: int color = getResources().getColor(R.color.mycolor);
這與第二種方法得到的值是一樣的,getResources()方法返回當前活動Activity的ResourceManager類實例。
說明:XML定義方法接受6位和8位兩種表示法,而且開頭必須是#,8位定義時前兩位表示透明。 4、直接定義色值,如: int color = 0xff00ff00;
這種方法必須使用0x開頭,而不是用我們常用的#。與方法3不一樣,值也必須用8位表示 ,不接受6位的顏色表示。分組一下0x|ff|ff00ff,0x是代表顏色整數的標記,ff是表示透明度,ff00ff表示RGB顏色值。
=======================
補充一點Android布局中背景圖片的設置(編輯LinearLayout):
* 可以使用純色:android:background="@drawable/mycolor" (XML資源文件中定義的顏色)
* 也可使用圖片:android:background="@drawable/bg" (需要將一個名為bg.jpg或png的圖片拷貝到res/drawable-hdpi目錄下)。

G. 這段android代碼什麼意思

這個代碼是為了給Android里的一個ListView填充具體的Item行用的。
首先你要搞清楚,getView()是誰會調用, 這個方法是UI系統會調用。
它會詢問你列表的每一行怎麼樣布局或者說渲染,於是你在getView里畫出你的視圖,然後系統UI幫你填到列表裡面去。
convertView = mInflater.inflate(R.layout.itemrow, null);
這行代碼可以清楚地看到,你己經為列表的行定義了一個布局在你的資源文件夾里,名字叫itemrow.xml. 你打開看看就明白什麼意思了,
裡面至少定義了一個文字行取名叫「 ITEMNAME」
裡面至少還定義了一個多選框按鈕名叫「 CHECKED」
所以你下面的代碼:holder.cb.setOnClickListener(new View.OnClickListener() {
就是為這個多選框設置了點擊事件,一旦用戶點擊了多選框,則記錄下來以備後用。最後你可以知道哪些行被用戶選中了。

切記切記!getView是UI系統為主體,是它來get你繪制的某一行View。
所以你的任務就是給他一行View,至於是哪一行int position參數 己經告訴你了。
根據position參數你可以在你的數組或者map中得到相應的對象。根據相應的對象你又可以得到相應的文字信息去繪制你的那一行VIEW

H. Android的一段常用動畫效果代碼(如何讓點擊的圖片控制項加速飛入到指定位置)

首相要new一個這個圖片image對象
然後用TranslateAnimation animation = new TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta)函數,將現在的的坐標和要移到的坐標寫進去,然後用animation.setFillAfter(true);讓圖片停留在那。最後image.startAnimation(animation )就可以了,我是看見QQ 2011以前用過這個移動的動畫做了一下,就是這么實現的

I. Android中如何使用代碼打開各種類型的文件

在安卓中打開音樂、視頻、圖片、文檔等文件是需要有讀取SD卡許可權的,如果是6.0以下的系統,則直接在清單文件中聲明SD卡讀取許可權即可;如果是6.0或以上,則需要動態申請許可權。

在7.0以下中打開文件時,通過intent調用系統安裝得人軟體打開文件就好了,但是在android7.0及以上的機子上這么做會報android.os.FileUriExposedException錯誤,

1)讀取SD卡

2)動態申請許可權

//設備API大於6.0時,主動申請許可權(讀取文件的許可權)

public static  void requestPermission(Activity context) {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

        if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)

                != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,

                    Manifest.permission.READ_EXTERNAL_STORAGE}, 0);

        }

    }

}

3)讀取文件

intent = OpenFileUtil.openFile(filePath+"/"+FileName+"."+end);

使用OpenFileUtil這個。鏈接: https://www.jianshu.com/p/1414101858c1

為了兼容Android7.0,獲取文件Uri需要使用到FileProvider。

1)首先是AndroidManifest文件裡面注冊FileProvider

    android:name="android.support.v4.content.FileProvider"

    android:authorities="${applicationId}.provider"

    android:exported="false"

    android:grantUriPermissions="true">

        android:name="android.support.FILE_PROVIDER_PATHS"

        android:resource="@xml/provider_paths" /> //需要自己編寫xml文件

2)provider_paths.xml文件的編寫

    // .表示根目錄

3)打開文檔方式為

intent = new Intent(Intent.ACTION_VIEW);

intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

File txtFile = new File(filePath+"/"+FileName+"."+end);

Uri contentUri = FileProvider.getUriForFile(MyApplication.getContext(), BuildConfig.APPLICATION_ID+".provider", txtFile);

intent.setDataAndType(contentUri, "application/vnd.android.package-archive");

grantUriPermission(context, contentUri, intent);

startActivity(intent);

4)grantUriPermission方法添加許可權

private static void grantUriPermission (Context context, Uri fileUri, Intent intent) {

    List resInfoList = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);

    for (ResolveInfo resolveInfo : resInfoList) {

        String packageName = resolveInfo.activityInfo.packageName;

        context.grantUriPermission(packageName, fileUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);

    }

}

綜合兩種情況:

//判斷是否是AndroidN以及更高的版本,Build.VERSION_CODES.N是Android 7.0

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

    intent = new Intent(Intent.ACTION_VIEW);

    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    File txtFile = new File(filePath+"/"+FileName+"."+end);

    Uri contentUri = FileProvider.getUriForFile(MyApplication.getContext(), BuildConfig.APPLICATION_ID+".provider", txtFile);

    Log.i("文件地址:",contentUri.toString());

    intent.setDataAndType(contentUri, "application/vnd.android.package-archive");

    grantUriPermission(MyApplication.getContext(), contentUri, intent);

} else {

    //7.0以下的可以打開文件了

    intent = OpenFileUtil.openFile(filePath+"/"+FileName+"."+end);

}

MyApplication.getContext().startActivity(intent);

J. 關於android一段代碼

layout_x:指定這個控制項的左上角點的x軸坐標
layout_y:指定這個控制項的左上角點的y軸坐標
這2個只有用在AbsoluteLayout(絕對布局)中才會看到效果
一般系統生成的布局文件默認用LinearLayout(線性布局)

閱讀全文

與android常用代碼相關的資料

熱點內容
dvd光碟存儲漢子演算法 瀏覽:757
蘋果郵件無法連接伺服器地址 瀏覽:962
phpffmpeg轉碼 瀏覽:671
長沙好玩的解壓項目 瀏覽:144
專屬學情分析報告是什麼app 瀏覽:564
php工程部署 瀏覽:833
android全屏透明 瀏覽:737
阿里雲伺服器已開通怎麼辦 瀏覽:803
光遇為什麼登錄時伺服器已滿 瀏覽:302
PDF分析 瀏覽:484
h3c光纖全工半全工設置命令 瀏覽:143
公司法pdf下載 瀏覽:381
linuxmarkdown 瀏覽:350
華為手機怎麼多選文件夾 瀏覽:683
如何取消命令方塊指令 瀏覽:349
風翼app為什麼進不去了 瀏覽:778
im4java壓縮圖片 瀏覽:362
數據查詢網站源碼 瀏覽:150
伊克塞爾文檔怎麼進行加密 瀏覽:892
app轉賬是什麼 瀏覽:163