`
wang_peng1
  • 浏览: 3954814 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

intent的方法使用

阅读更多

1.Pick File

void pickFile(File aFile) {
    Intent theIntent = new Intent(Intent.ACTION_PICK);
    theIntent.setData(Uri.fromFile(aFile));  //default file / jump directly to this file/folder
    theIntent.putExtra(Intent.EXTRA_TITLE,"A Custom Title"); //optional
    theIntent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); //optional
    try {
        startActivityForResult(theIntent,PICK_FILE_RESULT_CODE);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    switch (requestCode) {
        case PICK_FILE_RESULT_CODE: {
            if (resultCode==RESULT_OK && data!=null && data.getData()!=null) {
                String theFilePath = data.getData().getPath();
                ...
            }
            break;
        }
    }
}

Uri.fromFile(aFile) == Uri.parse("file://"+aFile.getPath())

2.

void pickFiles(File aFolder) {
    Intent theIntent = new Intent(Intent.ACTION_PICK);
    theIntent.setData(Uri.fromFile(aFolder));  //jump directly to this folder
    theIntent.putExtra("com.blackmoonit.extra.ALLOW_MULTIPLE",true); //required
    theIntent.putExtra(Intent.EXTRA_TITLE,"A Custom Title"); //optional
    theIntent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); //optional
    try {
        startActivityForResult(theIntent,PICK_FILES_RESULT_CODE);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    switch (requestCode) {
        case PICK_FILES_RESULT_CODE: {
            if (resultCode==RESULT_OK && data!=null && data.getExtras()!=null) {
                ArrayList<Uri> theFileUriList = data.getExtras().get(Intent.EXTRA_STREAM);
                ...
            }
            break;
        }
    }
}

void pickFolder(File aFolder) {
    Intent theIntent = new Intent(Intent.ACTION_PICK);
    theIntent.setData(Uri.parse("folder://"+aFolder.getPath()));  //default folder / jump directly to this folder
    theIntent.putExtra(Intent.EXTRA_TITLE,"A Custom Title"); //optional
    theIntent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); //optional
    try {
        startActivityForResult(theIntent,PICK_FOLDER_RESULT_CODE);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    switch (requestCode) {
        case PICK_FOLDER_RESULT_CODE: {
            if (resultCode==RESULT_OK && data!=null && data.getData()!=null) {
                String theFolderPath = data.getData().getPath();
                ...
            }
            break;
        }
    }
}

2.

void saveToFile(File aFile) {
    Uri theUri = Uri.fromFile(aFile).buildUpon().scheme("file.new").build();
    Intent theIntent = new Intent(Intent.ACTION_PICK);
    theIntent.setData(theUri);
    theIntent.putExtra(Intent.EXTRA_TITLE,"A Custom Title"); //optional
    theIntent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); //optional
    try {
        startActivityForResult(theIntent,SAVE_FILE_RESULT_CODE);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    switch (requestCode) {
        case SAVE_FILE_RESULT_CODE: {
            if (resultCode==RESULT_OK && data!=null && data.getData()!=null) {
                String theFilePath = data.getData().getPath();
                ...
            }
            break;
        }
    }
}

void saveToFolder(File aFolder) {
    Uri theUri = Uri.fromFile(aFolder).buildUpon().scheme("folder.new").build();
    Intent theIntent = new Intent(Intent.ACTION_PICK);
    theIntent.setData(theUri);
    theIntent.putExtra(Intent.EXTRA_TITLE,"A Custom Title"); //optional
    theIntent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); //optional
    try {
        startActivityForResult(theIntent,SAVE_FOLDER_RESULT_CODE);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    switch (requestCode) {
        case SAVE_FOLDER_RESULT_CODE: {
            if (resultCode==RESULT_OK && data!=null && data.getData()!=null) {
                String theFolderPath = data.getData().getPath();
                ...
            }
            break;
        }
    }
}

3.

private static final String EXTRA_DIRECTORY = "com.blackmoonit.intent.extra.DIRECTORY";

void createPlaylist(ArrayList<Uri> aFileList) {
    String theDefaultFolderPath = "/sdcard/playlists"; 
    Intent theIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
    theIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,aFileList);
    theIntent.setType("audio/*");
    theIntent.putExtra(EXTRA_DIRECTORY,theDefaultFolderPath); //optional
    try {
        startActivity(theIntent);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
4.

Create Zip file

private static final String EXTRA_DIRECTORY = "com.blackmoonit.intent.extra.DIRECTORY";

void createZipFile(ArrayList<Uri> aFileList) {
    Intent theIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
    theIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,aFileList);
    theIntent.setType("multipart/mixed");  //this should be a good faith attempt at determining the MIME type
    String theFolderPath = "/sdcard/some_folder";  //all files in the Zip will be stored relative to this path
    theIntent.putExtra(EXTRA_DIRECTORY,theFolderPath);
    try {
        startActivity(theIntent);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

void createZipFile(File aFile) {
    Intent theIntent = new Intent(Intent.ACTION_SEND);
    theIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(aFile));
    try {
        startActivity(theIntent);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

void unpackZipFile(File aFile) {
    Intent theIntent = new Intent(Intent.ACTION_VIEW);
    theIntent.setDataAndType(Uri.fromFile(aFile),"application/zip");
    try {
        startActivity(theIntent);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static final String MIME_AUTHORITY = "com.blackmoonit.FileBrowser.mimeType";    
public static final Uri MIMETYPE_URI = Uri.parse("content://" + MIME_AUTHORITY );

private String getMIMEtypeFromProvider(String aFilename) {
    Uri theContentTypeRequest = Uri.withAppendedPath(MIMETYPE_URI,aFilename);
    Cursor theTypeResult = managedQuery(theContentTypeRequest, null, null, null, null);
    theTypeResult.moveToFirst();
    if (!theTypeResult.isNull(0)) {
        return theTypeResult.getString(0);
    } else {
        return null;
    }
}

分享到:
评论

相关推荐

    singleTask以及newIntent方法的使用

    ### newIntent方法 `onNewIntent()`方法是在`Activity`的生命周期中被调用的,当`Activity`已经是栈顶且接收到新的`Intent`时,系统不会重新创建`Activity`,而是调用这个方法传递新的`Intent`。在`singleTask`模式...

    Android Studio 实验二:Intent的使用

    使用putExtra()方法可以将数据附加到Intent中,然后在接收端使用getExtra()系列方法获取这些数据: ```java // 发送端 Intent intent = new Intent(this, TargetActivity.class); intent.putExtra("key", ...

    实验八 使用Intent回传数据

    在新启动的Activity中,可以通过getIntent()获取启动Intent,并使用getExtras()或直接使用get()方法来提取数据。例如: ```java Intent intent = getIntent(); String value = intent.getStringExtra("key"); `...

    intent的常用方法

    除了上述方法外,还可以使用以下方法来设定Intent的属性: 1. **setComponent**:指定一个具体的组件来处理Intent。 ```java Intent setComponent = new Intent().setComponent(new ComponentName("package", ...

    Android中intent的使用

    本篇文章将深入探讨Intent的基本概念、类型、构造方法以及如何在Android中有效地使用Intent。 Intent是一种意图声明,表达了应用程序想要执行的操作。在Android系统中,Intent分为显式Intent和隐式Intent两种类型。...

    Intent的简单使用

    5. **使用 extras Bundle 的 putXXX() 方法**:Intent的extras Bundle还提供了许多其他putXXX()方法,如putString(), putInt(), putBoolean()等,用于传递不同类型的基础数据。 示例代码: ```java // 发送端 ...

    Android-Intent使用方法详解

    Android-Intent使用方法详解 配合(http://blog.csdn.net/daiyibo123/article/details/51227160)博客查看。使用Android stdio编写。

    Intent使用示例(一)

    在标题提到的“Intent使用示例(一)”中,我们将重点关注`startActivityForResult`方法。这个方法通常用于启动一个Activity,并期望在新Activity执行完某些操作后返回结果。当用户在新Activity中完成任务,如选择照片...

    Android的Intent实验

    使用`sendBroadcast(Intent)`、`sendOrderedBroadcast(Intent, String)`或`sendBroadcastAsUser(Intent, UserHandle, String)`方法发送广播。注册广播接收器有两种方式:在AndroidManifest.xml中静态注册或在代码中...

    使用Intent打开网页

    使用startActivity()方法启动Intent,系统会自动寻找能够处理此Intent的组件(在这里通常是系统浏览器)并打开网页。 二、使用WebView打开网页 如果不想依赖外部的系统浏览器,而是希望在应用内部展示网页,可以...

    android中intent使用示例

    总结,Intent是Android系统中连接各个组件的桥梁,理解并熟练使用Intent对于开发Android应用至关重要。在实际项目中,Intent不仅可以用于启动Activity和Service,还可以用于启动BroadcastReceiver,实现各种组件间的...

    使用Intent拨打电话

    本教程将详细介绍如何使用Intent来拨打电话。 首先,我们需要了解Intent的基本构造。Intent对象有两个主要构造函数:`Intent(String action)`和`Intent(Context packageContext, Class&lt;?&gt; clazz)`。在拨打拨号场景...

    Intent数据传递实用方法

    在Activity间传递数据,我们通常使用Intent的putExtra()和getExtra()方法。例如,我们可以传递基本数据类型如int、String,也可以传递复合数据类型如Parcelable、Serializable对象。以下是一些常用的数据传递示例: ...

    通过Intent实现Activity之间的切换,并传递数据

    启动Activity时,可以通过Intent的setFlags()方法设置不同的启动模式,如标准模式、单实例模式、单任务模式和单栈顶模式。这些模式会影响Activity的生命周期和栈管理,比如决定新Activity是否创建新的任务栈,或者...

    Android中Intent的使用

    创建Intent通常使用以下构造方法: - `Intent(Context packageContext, Class&lt;?&gt; cls)`: 创建显式Intent,传入上下文和目标组件的Class。 - `Intent(String action)`: 创建隐式Intent,只设置Action。 - `Intent...

    实验七 使用Intent在Activity间传输数据

    在“实验七 使用Intent在Activity间传输数据”中,我们将深入理解Intent的工作原理及其在不同Activity间传输数据的方法。这个实验旨在帮助开发者掌握如何在Android应用程序的不同界面之间有效地传递信息。 首先,...

    android用于打开各种文件的intent.pdf

    我们首先创建了一个 Intent 对象,并指定了动作为 "android.intent.action.VIEW",然后使用 setDataAndType 方法设置了 URI 和类型为 "application/pdf",最后使用 startActivity 方法启动该 Intent。 下面是打开...

    intent使用源码

    在深入理解Intent的使用源码之前,我们需要先了解Intent的基本概念和组成部分。 Intent主要包含两部分:Action(动作)和Data(数据)。Action定义了Intent想要执行的操作,比如ACTION_VIEW、ACTION_CALL等。Data则...

    android Intent的用法

    - 使用putExtra()方法添加键值对:`intent.putExtra("key", "value");` - 获取数据:在目标组件中使用`getExtras()`或`getStringExtra("key")`等方法获取。 4. 使用Intent创建意图过滤器(Intent Filter): - ...

    Android中Intent使用、数据回写(显)

    - 使用`putExtra()`方法可以在Intent中添加额外的数据,如`intent.putExtra("key", "value");` - 数据类型包括基本类型(int、String等)、Parcelable(自定义对象、Bitmap等)和Serializable(复杂对象)。 3. *...

Global site tag (gtag.js) - Google Analytics