安卓intent调用系统应用,传递数据和接收响应数据

来源:互联网 发布:晋城行知教育怎么样 编辑:程序博客网 时间:2024/05/29 17:49

全栈工程师开发手册 (作者:栾鹏)

安卓教程全解

安卓intent调用系统应用,同时传递数据,并对部分系统应用获取操作响应数据

安卓intent切换到系统应用,同时传递参数

调用系统应用往往需要在manifest中添加对应的权限,了解权限,可以参考xxxx

实例代码,这里以调用系统浏览器,打开指定网页为例

    public void to_system_app(){        Uri myuri = Uri.parse("http://www.baidu.com");        Intent myintent=new Intent(Intent.ACTION_VIEW,myuri);//ACTION_VIEW打开浏览器        PackageManager pm=getPackageManager();        if (myintent.resolveActivity(pm)!=null) {   //判断外部窗口是否可以调用            startActivity(myintent);   //启动应用程序        }    }

除此之外还有诸多的系统应用供我们调用

调用浏览器搜索内容

调用seach_web(this,"腾讯");函数定义public static void seach_web(Context context,String searchkey){    Intent intent = new Intent();     intent.setAction(Intent.ACTION_WEB_SEARCH);     intent.putExtra(SearchManager.QUERY,searchkey);    context.startActivity(intent); }

浏览指定网页

调用brower_web(this,"http://www.baidu.com");函数定义public static void brower_web(Context context,String webpath){     Uri uri = Uri.parse(webpath);     Intent it  = new Intent(Intent.ACTION_VIEW,uri);     context.startActivity(it); }

显示地图

调用map(this,38.899533,-77.036476);函数定义public static void map(Context context,Double lat,double lng){      Uri uri = Uri.parse("geo:"+String.valueOf(lat)+","+String.valueOf(lng));     Intent it = new Intent(Intent.ACTION_VIEW,uri);     context.startActivity(it); }

拨打电话

调用 phone(this,"12345678911"); 函数定义public static void phone(Context context,String number){    Uri uri = Uri.parse("tel:"+number);     Intent it = new Intent(Intent.ACTION_DIAL, uri);       context.startActivity(it); }

调用发短信的程序,没有接收人

调用sms(this,"这是一个代码发送短信");函数定义public static void sms(Context context,String body){    Intent it = new Intent(Intent.ACTION_VIEW);        it.putExtra("sms_body", body);        it.setType("vnd.android-dir/mms-sms");        context.startActivity(it); }

调用发短信的程序,有接收人

调用sms1(this,"12345678911","这是一个代码发送短信");函数定义public static void sms1(Context context,String number,String body){         Uri uri = Uri.parse("smsto:"+number);        Intent it = new Intent(Intent.ACTION_SENDTO, uri);        it.putExtra("sms_body", body);        context.startActivity(it); }

发送彩信

调用smsm(this,"content://media/external/images/media/23","这是一个代码发送彩信");函数定义public static void smsm(Context context,String imgpath,String body){       Uri uri = Uri.parse(imgpath);        Intent it = new Intent(Intent.ACTION_SEND);        it.putExtra("sms_body", body);        it.putExtra(Intent.EXTRA_STREAM, uri);        it.setType("image/png");        context.startActivity(it); }

发送邮件

调用email(this,"825485697@qq.com");函数定义public static void email(Context context,String toemail){    Uri uri = Uri.parse("mailto:"+toemail);     Intent it = new Intent(Intent.ACTION_SENDTO, uri);     context.startActivity(it); }调用email(this,"825485697@qq.com","这是代码创建的一个邮件");函数定义public static void email(Context context,String toemail,String body){    Intent it = new Intent(Intent.ACTION_SEND);     //Intent.EXTRA_EMAIL,Intent.EXTRA_CC,Intent.EXTRA_BCC。这三个分别用于传递“接收人地址列表”、“抄送人地址列表”和“密送人地址列表”    it.putExtra(Intent.EXTRA_EMAIL, toemail);        it.putExtra(Intent.EXTRA_TEXT, body);        it.setType("text/plain");        context.startActivity(Intent.createChooser(it, "邮件出错"));  }调用email(this,"825485697@qq.com","这是代码创建的一个邮件","这里是主题");函数定义public static void email(Context context,String toemail,String body,String subject){    Intent it=new Intent(Intent.ACTION_SEND);          it.putExtra(Intent.EXTRA_EMAIL, toemail);  //toemail可以是一个数组            it.putExtra(Intent.EXTRA_TEXT, body);          it.putExtra(Intent.EXTRA_SUBJECT, subject);          it.setType("message/rfc822");    context.startActivity(Intent.createChooser(it, "邮件出错"));    }调用email(this,"825485697@qq.com","这是代码创建的一个邮件","这里是主题","/sdcard/test.mp4");函数定义public static void email(Context context,String toemail,String body,String subject,String filepath){    Intent it = new Intent(Intent.ACTION_SEND);     it.putExtra(Intent.EXTRA_EMAIL, toemail);  //toemail可以是一个数组            it.putExtra(Intent.EXTRA_TEXT, body);          it.putExtra(Intent.EXTRA_SUBJECT, subject);        it.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(filepath)));         if (filepath.endsWith(".gz")) {         it.setType("application/x-gzip"); //如果是gz使用gzip的mime     } else if (filepath.endsWith(".txt")) {         it.setType("text/plain"); //纯文本则用text/plain的mime     } else {         it.setType("application/octet-stream"); //其他的均使用流当做二进制数据来发送     }     context.startActivity(Intent.createChooser(it, "邮件出错")); }

播放多媒体

调用multimedia(this,"/sdcard/test.mp4","video/mp4");函数定义public static void multimedia(Context context,String filepath,String type){    Intent it = new Intent(Intent.ACTION_VIEW);     Uri uri = Uri.parse("file://"+filepath);     it.setDataAndType(uri,type); //如果是图片,则为"image/*",如果是音频,则为"audio/*",如果是视频,则为"video/*"      context.startActivity(it); }

卸载 apk

调用uninstall_apk(this,"com.lp.app");函数定义public static void uninstall_apk(Context context,String packagename){       Uri uri = Uri.fromParts("package",packagename, null);     //要使用app的包名     Intent it = new Intent(Intent.ACTION_DELETE, uri);        context.startActivity(it); }

安装 apk

调用install_apk(this,"com.lp.app");函数定义public static void install_apk(Context context,String packagename){     Uri installUri = Uri.fromParts("package",packagename, null);     Intent it = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);     context.startActivity(it);  }

显示应用详细信息

调用appinfo(this,"111");函数定义public static void appinfo(Context context,String appid){           Uri uri = Uri.parse("market://details?id="+appid);   //appid为app在应用超市的id号          Intent it = new Intent(Intent.ACTION_VIEW, uri);             context.startActivity(it);}

寻找应用

调用findapp(this,"com.lp.app");函数定义 public static void findapp(Context context,String packagename){     Uri uri = Uri.parse("market://search?q=pname:"+packagename);             Intent it = new Intent(Intent.ACTION_VIEW, uri);             context.startActivity(it);   }

发送文件

调用sendfile(this,"video/mp4","/sdcard/test.mp4");函数定义public static void sendfile(Context context,String mimeType,String filePath){    Intent intent = new Intent();      intent.setAction(Intent.ACTION_SEND);      intent.setType(mimeType);  //mimeType: 如果是图片,则为"image/*",如果是音频,则为"audio/*",如果是视频,则为"video/*"      intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(filePath)));      context.startActivity(intent);  }

调用系统裁剪图片

调用cutimg(this,"/sdcard/test.jpg","/sdcard/test1.jpg");函数定义public static void cutimg(Context context,String sfile,String dfile){    Uri photoUri = Uri.fromFile(new File(sfile));         Intent intent = new Intent("com.android.camera.action.CROP");         intent.setDataAndType(photoUri, "image/*");         intent.putExtra("crop", "true");        intent.putExtra("output",  Uri.fromFile(new File(dfile)));         intent.putExtra("outputFormat", "JPEG");        context.startActivity(intent);  }

调用分享功能

调用share(this,"http://baidu.iqiyi.com/kan/aBBmf?fr=v.duba.com/dhfx_picM","video/mp4");函数定义public static void share(Context context,String urlpath,String type){    Intent mIntent = new Intent(Intent.ACTION_SEND);      //根据分享的内容设置不同的mimeType              mIntent.setType(type);      mIntent.putExtra(Intent.EXTRA_TEXT,urlpath);      mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);      context.startActivity(Intent.createChooser(mIntent, "分享"));  }

打开录音机

调用recoder(this);函数定义public static void recoder(Context context){       Intent mi = new Intent(Media.RECORD_SOUND_ACTION);    context.startActivity(mi); }

intent切换到系统应用,获取响应数据

在窗口activity中调用如下函数切换到需要的系统应用,其中使用不同的请求码表示不同的系统应用,或不同情况下的请求

这里以获取选择一个mp4文件为例

private static final int system_app_type1 = 2;   private void to_system_app_result() {      Intent intent = new Intent(Intent.ACTION_PICK);      intent.setType("video/mp4");//这里是设置显示的文件类型      startActivityForResult(intent, system_app_type1);}

在启动系统应用后,用户进行操作后,应用系统会自动关闭,向源activity发送响应结果。源activity重写onActivityResult函数实现响应数据的接收

 @Override    protected void onActivityResult(int requestCode,int resultCode,Intent data){  //requestCode打开子窗口时的请求码,resultCode子窗口返回的响应码,data目标窗口返回的intent        super.onActivityResult(requestCode, resultCode, data);        if (requestCode==system_app_type1) {            if (resultCode==Activity.RESULT_OK) {                Log.v("系统应用返回", "操作成功");                Uri uri=data.getData();                Log.v("系统应用返回数据",uri.toString());            }            else {                Log.v("系统应用返回", "操作失败");            }        }    }

除此之外还有诸多的系统应用供我们调用获取响应结果。

1、调用系统图片选择,获取返回的图片数据

调用selectpicture(this, requestCode);函数定义public static void selectpicture(Context context,int requestcode){      Intent i = new Intent();      i.setType("image/*");      i.setAction(Intent.ACTION_GET_CONTENT);      ((Activity)context).startActivityForResult(i, requestcode);}在onActivityResult响应函数中调用public static Bitmap getpicture(Context context,Uri uri) {   //根据返回结果获取图片bitmap    Bitmap bitmap=null;    File file = new File(uri.getPath());    if (file.exists()) {        bitmap = BitmapFactory.decodeFile(uri.getPath());    }    return bitmap;  }

2、调用系统通讯录应用,获取选择的联系人信息

调用linkman(this, 2);函数定义public static void linkman(Context context,int requestcode) {     Uri uri = Uri.parse("content://contacts/people");     Intent intent = new Intent(Intent.ACTION_PICK, uri);     ((Activity)context).startActivityForResult(intent, requestcode);}在onActivityResult响应函数中调用public static String[] getPhoneContacts(Context context,Uri uri)  //根据返回结果的uri获取联系(姓名、号码){    String[] contact=new String[2];    //得到ContentResolver对象    ContentResolver cr = context.getContentResolver();    //取得电话本中开始一项的光标    Cursor cursor=cr.query(uri,null,null,null,null);    if(cursor!=null)    {        cursor.moveToFirst();        //取得联系人姓名        int nameFieldColumnIndex=cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);        contact[0]=cursor.getString(nameFieldColumnIndex);        //取得电话号码        String ContactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));        Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId, null, null);        if(phone != null){            phone.moveToFirst();            contact[1] = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));        }        phone.close();        cursor.close();    }    else    {        return null;    }    return contact;}
阅读全文
0 0
原创粉丝点击