移动开发作业博客2

介绍关于手机桌面
下面是一个显示生词本的实时文件夹的实例

这个实时文件夹显示ContentProvider的数据,这要把之前开发DictProvider部署到模拟器中,才可以用实时文件显示ContentProvider返回的数据

创建项目:WordsLiveFolder

运行项目效果:
这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

package org.wwj.desktop;  

import android.app.Activity;  
import android.content.Intent;  
import android.net.Uri;  
import android.os.Bundle;  
import android.provider.LiveFolders;  

public class WordsLiveFolder extends Activity{  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        // TODO Auto-generated method stub  
        super.onCreate(savedInstanceState);  
        //如果该Intent的Action是创建实时文件夹的Action  
        if(getIntent().getAction().equals(LiveFolders.ACTION_CREATE_LIVE_FOLDER)){  
            Intent intent = new Intent();  
            //设置实时文件夹所显示ContentProvider提供的数据的uri  
            intent.setData(Uri.parse("content://org.crazyit.providers.dictprovider/words"));  
            //设置实时文件夹的base intent属性  
            intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT, new Intent(Intent.ACTION_VIEW  
                    , Uri.parse("content://org.crazyit.providers.dictprovider/word/")));  
            //设置实时文件夹的名称  
            intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME, "生词本");  
            //设置实时文件夹的图标  
            intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON, Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher));  
            //设置实时文件夹的显示模式  
            intent.putExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE, LiveFolders.DISPLAY_MODE_GRID);  
            setResult(RESULT_OK, intent);  
        }  
        else{  
            setResult(RESULT_CANCELED);  
        }  
        //结束该Activity  
        finish();  
    }  
}  



[html] view plain copy

<activity android:name=".WordsLiveFolder"  
         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>  





[java] view plain copy
package org.wwj.desktop;  

import android.app.Activity;  
import android.content.ContentUris;  
import android.database.Cursor;  
import android.net.Uri;  
import android.os.Bundle;  
import android.widget.EditText;  

public class ShowWordActivity extends Activity  
{  
    EditText etWord , etDescription;  
    @Override  
    public void onCreate(Bundle savedInstanceState)  
    {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.show);  
        // 获取界面组件  
        etWord = (EditText) findViewById(R.id.word);  
        etDescription = (EditText) findViewById(R.id.description);  
        Uri uri = getIntent().getData();  
        // 获取单词ID  
        long id = ContentUris.parseId(uri);  
        // 从ContentProvider查询指定单词  
        Cursor cursor = getContentResolver().query(  
            Uri.parse("content://org.crazyit.providers.dictprovider/words")  
            , null , "_id=?" , new String[]{id + ""} , null );  
        if(cursor.moveToNext())  
        {  
            // 使用界面组件显示查询得到的结果  
            etWord.setText(cursor.getString(1));  
            etDescription.setText(cursor.getString(2));  
        }  
    }  
}  



[html] view plain copy
<activity  
            android:name=".ShowWordActivity"  
            android:label="@string/title_activity_show_word" >  
            <intent-filter>  
                <action android:name="android.intent.action.VIEW"/>  
                <category android:name="android.intent.category.DEFAULT"/>  
                <data android:mimeType="vnd.android.cursor.item/org.crazyit.dict"/>  
            </intent-filter>  
        </activity>  



[html] view plain copy
<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    >  
<TextView  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:text="@string/word"  
/>  
<EditText  
    android:id="@+id/word"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:cursorVisible="false"  
    android:editable="false"  
/>  
<TextView  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:text="@string/description"  
/>  
<EditText  
    android:id="@+id/description"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:lines="3"  
    android:cursorVisible="false"  
    android:editable="false"  
/>  
</LinearLayout>  



猜你喜欢

转载自blog.csdn.net/SHEN_YI_LE/article/details/80760027