Android 实时文件夹
实时文件夹是一种用来显示由某个ContentProvider提供的数据信息的桌面组件。要创建一个实时文件夹,必须要有两个方面的支持。
1,要定义一个用来创建实时文件夹的Activity。
2,所指定数据信息URI的ContentProvider必须支持实时文件夹时文件夹查询
一、定义创建实时文件夹的Activity
想在桌面长按后选择实时文件夹就会弹出一个可用实时文件夹的列表对话框,必须在应用程序内的Activity中添加一个Action为android.intent.action.CREATE_LIVE_FOLDER的IntentFilter。而在这个创建实时文件夹的Activity中,我们要把实时文件夹的信息以附加信息的形式存储在一个Intent对象当中。
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.studio.android.ch10.ex2"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon"
- android:label="@string/app_name">
- <activity android:name=".MyAllContacts"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name=
- "android.intent.action.CREATE_LIVE_FOLDER" />
- <category android:name=
- "android.intent.category.DEFAULT" />
- </intent-filter>
- </activity>
- </application>
- <uses-sdk android:minSdkVersion="3" />
- </manifest>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.studio.android.ch10.ex2"android:versionCode="1"android:versionName="1.0"><application android:icon="@drawable/icon" android:label="@string/app_name"><activity android:name=".MyAllContacts"android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.CREATE_LIVE_FOLDER" /><category android:name="android.intent.category.DEFAULT" /></intent-filter></activity></application><uses-sdk android:minSdkVersion="3" />
</manifest>
由于Content的ContentProvider已经实现了对实时文件夹的相关支持
- import android.app.Activity;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.provider.Contacts;
- import android.provider.LiveFolders;
- public class MyAllContacts extends Activity {
- public static final Uri LIVE_FOLDER_URI =
- Uri.parse("content://contacts/live_folders/people");
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- if (getIntent().getAction()
- .equals(LiveFolders.ACTION_CREATE_LIVE_FOLDER)) {
- Intent intent = new Intent();
- intent.setData(LIVE_FOLDER_URI);//在文件夹,对于要查询的URI则是以Data的形式存储在Intent对象中。Contacts的ContentProvider已经实现了对实时文件夹的相关支持。
- intent.putExtra(
- LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT,
- new Intent(Intent.ACTION_VIEW,
- Contacts.People.CONTENT_URI));
- intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME,
- "MyAllContacts");
- intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON,
- Intent.ShortcutIconResource.fromContext(this,
- R.drawable.icon));
- intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
- LiveFolders.DISPLAY_MODE_LIST);//还可以设置LiveFolders.DISPLAY_MODE_LIST
- setResult(RESULT_OK, intent);
- } else {
- setResult(RESULT_CANCELED);
- }
- finish();
- }
- }
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts;
import android.provider.LiveFolders;public class MyAllContacts extends Activity {public static final Uri LIVE_FOLDER_URI =Uri.parse("content://contacts/live_folders/people");@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);if (getIntent().getAction().equals(LiveFolders.ACTION_CREATE_LIVE_FOLDER)) {Intent intent = new Intent();intent.setData(LIVE_FOLDER_URI);//在文件夹,对于要查询的URI则是以Data的形式存储在Intent对象中。Contacts的ContentProvider已经实现了对实时文件夹的相关支持。intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT,new Intent(Intent.ACTION_VIEW,Contacts.People.CONTENT_URI));intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME,"MyAllContacts");intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON,Intent.ShortcutIconResource.fromContext(this,R.drawable.icon));intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE, LiveFolders.DISPLAY_MODE_LIST);//还可以设置LiveFolders.DISPLAY_MODE_LISTsetResult(RESULT_OK, intent);} else {setResult(RESULT_CANCELED);}finish();}
}
二、定义支持实时文件夹的ContentProvider
要使一个ContentProvider支持实时文件夹的查询,主要要实现下面2个:
1,为实时文件夹查询定义一个专门的URI
2,在query查询方法中针对实时文件夹的路径进行相应的查询然后返回含有特定列名的Cursor
在CountryCode.java中
//为URI匹配器增加实时文件夹URI的匹配号码
public static final int LIVE_FOLDER = 3;
---
---
---
//定义实时文件夹的URI
public static final Uri LIVE_FOLDER_URI =
Uri.parse("content://" + AUTHORITY + "/livefolder");
在MyProvider.java中
static {
sMatcher = new UriMatcher(UriMatcher.NO_MATCH);
----
---
sMatcher.addURI(CountryCode.AUTHORITY,
"livefolder/", CountryCode.LIVE_FOLDER);
}
---
---
@Override
public Cursor query(Uri uri, String[] projection,
String selection, String[] args,String order) {
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor c;
switch (sMatcher.match(uri)) {
----
case CountryCode.LIVE_FOLDER:
String[] myProjection = {
//注意更改别名
CountryCode.ID + " AS " + LiveFolders._ID,
CountryCode.COUNTRY + " AS " + LiveFolders.NAME,
CountryCode.CODE + " AS " + LiveFolders.DESCRIPTION
};
c = db.query(CountryCode.TB_NAME, myProjection, selection,
args,null,null,order);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
CreateLiveFolder.java中
import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.provider.LiveFolders;
- public class CreateLiveFolder extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- if (getIntent().getAction()
- .equals(LiveFolders.ACTION_CREATE_LIVE_FOLDER)) {
- Intent intent = new Intent();
- intent.setData(CountryCode.LIVE_FOLDER_URI);
- intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME,
- "CountryCode");
- intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON,
- Intent.ShortcutIconResource.fromContext(this,
- R.drawable.icon));
- intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
- LiveFolders.DISPLAY_MODE_LIST);
- setResult(RESULT_OK, intent);
- } else {
- setResult(RESULT_CANCELED);
- }
- finish();
- }
- }
import android.content.Intent;
import android.os.Bundle;
import android.provider.LiveFolders;public class CreateLiveFolder extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);if (getIntent().getAction().equals(LiveFolders.ACTION_CREATE_LIVE_FOLDER)) {Intent intent = new Intent();intent.setData(CountryCode.LIVE_FOLDER_URI);intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME,"CountryCode");intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON,Intent.ShortcutIconResource.fromContext(this,R.drawable.icon));intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE, LiveFolders.DISPLAY_MODE_LIST);setResult(RESULT_OK, intent);} else {setResult(RESULT_CANCELED);}finish();}
}
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.studio.android.chp10.ex3"
- android:versionCode="1"
- android:versionName="1.0.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".SQLite2"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <provider android:name="MyProvider"
- android:authorities="com.studio.andriod.provider.countrycode" />
- <activity android:name=".CreateLiveFolder">
- <intent-filter>
- <action android:name=
- "android.intent.action.CREATE_LIVE_FOLDER" />
- <category android:name=
- "android.intent.category.DEFAULT" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
转载于:https://www.cnblogs.com/xiaochao1234/p/4241899.html
Android 实时文件夹相关推荐
- Android桌面组件开发之实时文件夹(Live_Folders)
转自:http://zhangkun716717-126-com.iteye.com/blog/813059 实时文件夹是一种用来显示由某个ContentProvider提供的数据信息的桌面组件.要创 ...
- android 获取文件夹下的所有文件
昨天,在做工作时,需要遍历所有一个文件夹下的所有文件夹,当时自己也不知道怎么做,后来在网上搜索了一些资料,发现其实也很简单. 1.获取SD是否可以读写,如果可以,则传入文件的路径 /*读取输入的某个文 ...
- android 删除目录下所有文件大小,Android 删除文件夹(文件夹以及文件夹下所有的文件)、文件...
1.Android 删除文件夹(文件夹以及文件夹下所有的文件) //删除文件夹和文件夹里面的文件 public static void deleteDirWihtFile(File dir) { if ...
- android data文件夹操作
1,看代码 public class DBTest extends Activity {SQLiteDatabase db;Button bn = null;ListView listView;@Ov ...
- android sdk文件位置,Android SDK文件夹位于何处?
我通过Air for Android用Adobe Flash创建了一个.apk应用程序.现在,我想通过这款Blackberry在线打包机为黑莓App World做好准备:https://bdsc.we ...
- Android获取文件夹路径
Android获取文件夹路径 应用程序在运行的过程中如果需要向手机上保存数据,一般是把数据保存在SDcard中的. 大部分应用是直接在SDCard的根目录下创建一个文件夹,然后把数据保存在该文件夹中. ...
- Android各个文件夹对应的分辨率?
#一. 描述 各个分辨率对应Android的图片文件夹? #二.对应分辨率 转载于:https://juejin.im/post/5b3ac699e51d4555687ab6b0
- android 浏览文件夹,如何浏览android中的文件夹并获取所选文件夹的路径
当我单击一个按钮,显示一个文件浏览器时,我可以选择一个文件夹并返回它的路径.我得到这个路径将文件复制到该路径. 但我不知道如何实现这一点. 我还在Stackoverflow中寻找这个问题,但我没有找到 ...
- android camera 检测,检测Android Camera文件夹
我想检测每个 Android设备上的相机文件夹.据我所知,这个文件夹与制造商不同,并且无法保证设备上甚至还有一个DCIM文件夹. 这是我现在用来获取文件的方法: private static fina ...
最新文章
- 2022-2028年中国玫瑰花行业市场研究及前瞻分析报告
- CRM 客户端程序开发:获取表单界面上各种字段的值及其他属性
- axios create拦截_Vue学习-axios
- html按钮按下效果_CSS+HTMLlt;水滴按钮效果gt;
- spring4.x(6)---SpringIOC的Scope配置
- 点阵字体显示系列补记:将字库文件转换成数组形式
- Python组合数据类型:容器类型转换,list←→tuple←→set
- python commands_python之commands模块
- SQL Server删除重复行的6个方法
- arrayvalue php,phparrayvalue
- 设置Response中的ContentType
- t470键盘拆解_Thinkpad 二手T470笔记本拆解|支持双硬盘|拆机教程
- 六、矩阵键盘的扫描原理与基本应用
- Android Studio开发记录
- iPhone没有声音,但插上耳机后声音正常的处理
- 2017年大工考博英语加试试题。给学弟学妹们一点帮助
- django的admin站点生成超级用户出错
- java:常见的日期转型,“Tue Oct 18 00:00:00 CST 2022“
- 动漫人物头发怎么画(头发画法步骤)
- PhysicalDrive
热门文章
- Java使用字节码和汇编语言同步分析volatile,synchronized的底层实现
- Java json转Map,转bean,转Listbean
- Ubuntu14.04上编译指定版本的protobuf源码操作步骤
- 用vs2010编译vigra静态库及简单使用举例
- Windows XP下vs2010中配置OpenCV2.4.3
- 【linux】用户和组的管理:添加、修改、删除(useradd usermod userdel groupadd groupdel)
- 【POCO】POCO学习总结(四)——MinGW编译poco
- 文件名转换为utf8 c语言,文件名编码转换:从 gb* 转向 utf8 必备工具 convmv
- php 单一入口 seo,网站结构分类(单一入口还是多入口)
- 鱼眼相机标定_鱼眼相机模型(二)