Android官方Guide回顾补漏
来源:互联网 发布:乔丹和詹姆斯数据对比 编辑:程序博客网 时间:2024/05/21 09:08
记录
1.设置启动Activity
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="ANDROID.INTENT.CATEGORY.LAUNCHER" /> </intent-filter>
2.针对不同版本采用不同处理办法
private void setUpActionBar() { // Make sure we're running on Honeycomb or higher to use ActionBar APIs if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { ActionBar actionBar = getActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); //左上角图标是否显示 }}
3.默认安装位置
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example" android:installLocation="auto">
4.文件相关知识
getFilesDir() //返回应用内部目录的File
getCacheDir()//返回应用临时缓存文件的内部目录的File
File file = File.createTempFile(filename.null,getCacheDir());//创建临时文件 getExternalStoragePublicDirectory(Environment.DIRECTORY_XXXX)//得到外部存储的公共文件夹
getExternalFilesDir() //得到外部存储的私有文件夹·
getFreeSpace() //获取可用空间
myFile.delete()//删除文件
myContext.deleteFile(filename)//删除内部文件
检测外部存储是否可写
public boolean isExternalStorageWritable() { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { return true; } return false;}
5.Intent
使用Uri来携带数据, 通过Intent的第一个参数Action确定需要调用的Activity。
Uri webpage = Uri.parse("http://www.android.com");Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
检测是否存在可响应的Activity
PackageManager packageManager = getPackageManager();List activities = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);boolean isIntentSafe = activities.size() > 0;
Intent chooser = Intent.createChooser(intent, title);// Verify the intent will resolve to at least one activityif (intent.resolveActivity(getPackageManager()) != null) { startActivity(chooser);}
获取联系人数据后读取
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) { // Check which request it is that we're responding to if (requestCode == PICK_CONTACT_REQUEST) { // Make sure the request was successful if (resultCode == RESULT_OK) { // Get the URI that points to the selected contact Uri contactUri = data.getData(); // We only need the NUMBER column, because there will be only one row in the result String[] projection = {Phone.NUMBER}; // Perform the query on the contact to get the NUMBER column // We don't need a selection or sort order (there's only one result for the given URI) // CAUTION: The query() method should be called from a separate thread to avoid blocking // your app's UI thread. (For simplicity of the sample, this code doesn't do that.) // Consider using CursorLoader to perform the query. Cursor cursor = getContentResolver() .query(contactUri, projection, null, null, null); cursor.moveToFirst(); // Retrieve the phone number from the NUMBER column int column = cursor.getColumnIndex(Phone.NUMBER); String number = cursor.getString(column); // Do something with the phone number... } }}
6.系统权限
从6.0(api23)开始,安卓程序在运行时请求权限,而不是再程序安装时请求权限。
检查权限的方式
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_CALENDAR); //有权限时返回 PackageManager.PERMISSION_GRANTED 无权限时返回PERMISSION_DENIED
ActivityCompat.requestPermissions(thisActivity,//无权限时请求权限 new String[]{Manifest.permission.READ_CONTACTS}, MY_PERMISSIONS_REQUEST_READ_CONTACTS);
处理权限请求响应
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case MY_PERMISSIONS_REQUEST_READ_CONTACTS: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission was granted, yay! Do the // contacts-related task you need to do. } else { // permission denied, boo! Disable the // functionality that depends on this permission. } return; } // other 'case' lines to check for other // permissions this app might request }}
7.应用间数据分享
首先定义Intent 设置Intent需要传递的数据
Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_STREAM,imageUri); sendIntent.setType("image/jpeg"); startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
在需要获取信息的Activity的配置文件里设置过滤器
<intent-filter> <action android:name="ANDROID.INTENT.ACTION.SEND" /> <category android:name="ANDROID.INTENT.CATEGORY.DEFAULT" /> <data android:mimeType="image/*" /> </intent-filter>
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_intent); Intent intent = getIntent(); String action = intent.getAction(); String type = intent.getType(); if(Intent.ACTION_SEND.equals(action) && type != null){ if(type.startsWith("image/")){ handleSendImage(intent); } } } private void handleSendImage(Intent intent) { Uri imageUri = intent.getParcelableExtra(Intent.EXTRA_STREAM); if(imageUri != null){ } }
0 0
- Android官方Guide回顾补漏
- Android官方文档翻译-guide-储存选择
- Android知识补漏
- android学习前后历程回顾,以及对资源分享,查缺补漏,温故知新———我是新手我标记
- Android官方API Guide学习之一 Android介绍-基础知识
- 【补漏】
- Android官方API Guide学习之二 设备兼容性
- Android官方API Guide学习之三 系统权限
- Redis debugging guide---官方
- ActionBar官方Guide
- Android查缺补漏之Toolbar
- Android查缺补漏之ListView
- Android查缺补漏之SpannableString
- android guide
- Android Guide
- Android官方API Guide学习之四 Intents和Intent Filters
- iOS App Programming Guide 官方文档
- 苹果官方《Sprite Kit Programming Guide》翻译
- Java 异常的捕获与处理详解(一)
- 【机房重构】 抽象工厂+反射+配置文件(下)
- poj 1011 sticks (dfs + 剪枝)
- codeforces 785D Anton and School
- linux 内核模块学习
- Android官方Guide回顾补漏
- 判断按钮的弹起和点击事件 mfc
- pip 指定源
- 做游戏,学编程(C语言) 飞机游戏 2 (思考改进)
- java线程浅析[结合源码讲述Thread的生命周期]
- linux下repo文件的配置
- 将matlab中数据保存为txt或dat格式
- git 基本操作记录
- 1. 需求和动态伸缩的网络架构