本文主要是介绍intent-filter中的三个子选项的使用 action----intent-filter-------------data的说明,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
android之Itent.ACTION_PICK Intent.ACTION_GET_CONTENT妙用
你是不是很多时候,想从弹出的电话本姓名列表中中查找到某个人,然后再获取该人的详细信息呢?
你是不是想选择从弹出的列表中选择一张图片,然后将其进行进一步的操作呢?
如果,你想,那你是不是很像知道,我们应该怎么让其弹出来一张选择列表,又应该怎么代码实现后边的操作呢?
Itent.ACTION_PICK Intent.ACTION_GET_CONTENT 两者都可以完成类似的功能,让我们一起来看下例子:
第一:Intent.ACTION_PICK
<uses-permission android:name="android.permission.READ_CONTACTS"/>
发起一个 Contact Picker
Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
重写方法
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data)
{
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndexOrThrow(People.NAME));
// TODO Whatever you want to do with the selected contact name.
}
}
break;
}
}
例如
String[] columns = new String[] {People.NAME};
int[] names = new int[] {R.id.row_entry};
mAdapter = new SimpleCursorAdapter(this, R.layout.mycontacts, C, columns, names);
setListAdapter(mAdapter);
- Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
- intent.setType("audio/*");
- startActivity(Intent.createChooser(intent, "Select music"));
- <span style="font-family: comic sans ms,sans-serif;"><span style="font-size: x-small;">Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
- intent.setType("audio/*");
- startActivity(Intent.createChooser(intent, "Select music"));</span></span>
执行之 会弹出一个对话框 效果为:
其实 对于这段代码 大家应该都能猜出什么意思 现自己模拟并理解之
[代码]
1. 定义TestActivity 用于根据传入Uri 播放目标
- public class TestActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- this.setTitle("TestActivity");
- Intent i = this.getIntent();
- Uri u = i.getData();
- try {
- playMusic(u);
- } catch (IllegalArgumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SecurityException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalStateException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void playMusic(Uri uri) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException{
- MediaPlayer mp = new MediaPlayer();
- mp.setDataSource(this, uri);
- mp.prepare();
- mp.start();
- }
- }
- <span style="font-family: comic sans ms,sans-serif;"><span style="font-size: x-small;">public class TestActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- this.setTitle("TestActivity");
- Intent i = this.getIntent();
- Uri u = i.getData();
- try {
- playMusic(u);
- } catch (IllegalArgumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SecurityException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalStateException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void playMusic(Uri uri) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException{
- MediaPlayer mp = new MediaPlayer();
- mp.setDataSource(this, uri);
- mp.prepare();
- mp.start();
- }
- }</span></span>
2. 在AndroidManifest 注册TestActivity
- <activity android:name=".TestActivity"
- android:label="TestActivity">
- <intent-filter>
- <action android:name="android.intent.action.GET_CONTENT" />
- <category android:name="android.intent.category.DEFAULT" />
- <category android:name="android.intent.category.OPENABLE" />
- <data android:mimeType="audio/music1" />
- </intent-filter>
- </activity>
- <span style="font-family: comic sans ms,sans-serif;"><span style="font-size: x-small;"><activity android:name=".TestActivity"
- android:label="TestActivity">
- <intent-filter>
- <action android:name="android.intent.action.GET_CONTENT" />
- <category android:name="android.intent.category.DEFAULT" />
- <category android:name="android.intent.category.OPENABLE" />
- <data android:mimeType="audio/music1" />
- </intent-filter>
- </activity></span></span>
3. 使用TestActivity
- public void sendChooser(){
- Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
- intent.setDataAndType(Uri.parse("file:///sdcard/DCIM/cc.mp3"), "audio/music1");
- startActivity(Intent.createChooser(intent, "Select music1 app"));
- }
- <span style="font-family: comic sans ms,sans-serif;"><span style="font-size: x-small;">public void sendChooser(){
- Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
- intent.setDataAndType(Uri.parse("file:///sdcard/DCIM/cc.mp3"), "audio/music1");
- startActivity(Intent.createChooser(intent, "Select music1 app"));
- }</span></span>
4. emulator 运行截图:
此外:
//选择图片 requestCode 返回的标识
Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT); //"android.intent.action.GET_CONTENT"
innerIntent.setType(contentType); //查看类型 String IMAGE_UNSPECIFIED = "image/*";
Intent wrapperIntent = Intent.createChooser(innerIntent, null);
((Activity) context).startActivityForResult(wrapperIntent, requestCode);
//视频
Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT);
innerIntent.setType(contentType); //String VIDEO_UNSPECIFIED = "video/*";
Intent wrapperIntent = Intent.createChooser(innerIntent, null);
((Activity) context).startActivityForResult(wrapperIntent, requestCode);
//添加音频
Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT);
innerIntent.setType(contentType); //String VIDEO_UNSPECIFIED = "video/*";
Intent wrapperIntent = Intent.createChooser(innerIntent, null);
((Activity) context).startActivityForResult(wrapperIntent, requestCode);
//录音
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContentType.AUDIO_AMR); //String AUDIO_AMR = "audio/amr";
intent.setClassName("com.android.soundrecorder",
"com.android.soundrecorder.SoundRecorder");
((Activity) context).startActivityForResult(intent, requestCode);
//拍摄视频
int durationLimit = getVideoCaptureDurationLimit(); //SystemProperties.getInt("ro.media.enc.lprof.duration", 60);
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, sizeLimit);
intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, durationLimit);
startActivityForResult(intent, REQUEST_CODE_TAKE_VIDEO);
//拍照 REQUEST_CODE_TAKE_PICTURE 为返回的标识
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //"android.media.action.IMAGE_CAPTURE";
intent.putExtra(MediaStore.EXTRA_OUTPUT, Mms.ScrapSpace.CONTENT_URI); // output,Uri.parse("content://mms/scrapSpace");
startActivityForResult(intent, REQUEST_CODE_TAKE_PICTURE);
=============================================================================================================
以下是常用到的Intent的URI及其示例,包含了大部分应用中用到的共用Intent。
一、打开一个网页,类别是Intent.ACTION_VIEW
Uri uri = Uri.parse(“http://blog.3gstdy.com/”);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
二、打开地图并定位到一个点
Uri uri = Uri.parse(“geo:52.76,-79.0342″);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
三、打开拨号界面 ,类型是Intent.ACTION_DIAL
Uri uri = Uri.parse(“tel:10086″);
Intent intent = new Intent(Intent.ACTION_DIAL, uri);
四、直接拨打电话,与三不同的是,这个直接拨打电话,而不是打开拨号界面
Uri uri = Uri.parse(“tel:10086″);
Intent intent = new Intent(Intent.ACTION_CALL, uri);
五、卸载一个应用,Intent的类别是Intent.ACTION_DELETE
Uri uri = Uri.fromParts(“package”, “xxx”, null);
Intent intent = new Intent(Intent.ACTION_DELETE, uri);
六、安装应用程序 ,Intent的类别是Intent.ACTION_PACKAGE_ADDED
Uri uri = Uri.fromParts(“package”, “xxx”, null);
Intent intent = new Intent(Intent.ACTION_PACKAGE_ADDED, uri);
七、播放音频文件
Uri uri = Uri.parse(“file:///sdcard/download/everything.mp3″);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setType(“audio/mp3″);
八、打开发邮件界面
Uri uri= Uri.parse(“mailto:admin@3gstdy.com”);
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
九、发邮件,与八不同这里是将邮件发送出去,
Intent intent = new Intent(Intent.ACTION_SEND);
String[] tos = { “admin@3gstdy.com ” };
String[] ccs = { “webmaster@3gstdy.com ” };
intent.putExtra(Intent.EXTRA_EMAIL, tos);
intent.putExtra(Intent.EXTRA_CC, ccs);
intent.putExtra(Intent.EXTRA_TEXT, “I come from http://blog.3gstdy.com ”);
intent.putExtra(Intent.EXTRA_SUBJECT, “http://blog.3gstdy.com”);intent.setType(“message/rfc882″);
Intent.createChooser(intent, “Choose Email Client”);
//发送带附件的邮件
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_SUBJECT, “The email subject text”);
intent.putExtra(Intent.EXTRA_STREAM, “file:///sdcard/mysong.mp3″);
intent.setType(“audio/mp3″);
startActivity(Intent.createChooser(intent, “Choose Email Client”));
十、发短信
Uri uri= Uri.parse(“tel:10086″);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.putExtra(“sms_body”, “I come from http://blog.3gstdy.com ”);
intent.setType(“vnd.Android -dir/mms-sms”);
十一、直接发邮件
Uri uri= Uri.parse(“smsto://100861″);
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra(“sms_body”, “3g android http://blog.3gstdy.com ”);
十二、发彩信
Uri uri= Uri.parse(“content://media/external/images/media/23″);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(“sms_body”, “3g android http://blog.3gstdy.com ”);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType(“image/png”);
十三、# Market 相关
1 //寻找某个应用
Uri uri = Uri.parse(“market://search?q=pname:pkg_name”);
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
//where pkg_name is the full package path for an application
2 //显示某个应用的相关信息
Uri uri = Uri.parse(“market://details?id=app_id”);
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
//where app_id is the application ID, find the ID
//by clicking on your application on Market home
//page, and notice the ID from the address bar
十四、路径规划
Uri uri = Uri.parse(“http://maps.google .com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en”);
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
//where startLat, startLng, endLat, endLng are a long with 6 decimals like: 50.123456
这篇关于intent-filter中的三个子选项的使用 action----intent-filter-------------data的说明的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!