导航:首页 > 操作系统 > 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
苹果邮件无法连接服务器地址 浏览:963
phpffmpeg转码 浏览:671
长沙好玩的解压项目 浏览:145
专属学情分析报告是什么app 浏览:564
php工程部署 浏览:833
android全屏透明 浏览:737
阿里云服务器已开通怎么办 浏览:803
光遇为什么登录时服务器已满 浏览:302
PDF分析 浏览:485
h3c光纤全工半全工设置命令 浏览:143
公司法pdf下载 浏览:382
linuxmarkdown 浏览:350
华为手机怎么多选文件夹 浏览:683
如何取消命令方块指令 浏览:349
风翼app为什么进不去了 浏览:778
im4java压缩图片 浏览:362
数据查询网站源码 浏览:150
伊克塞尔文档怎么进行加密 浏览:892
app转账是什么 浏览:163