记录
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 activity
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
获取联系人数据后读取
@Override
protected 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){
}
}