您的位置:首页 > 移动开发 > Android开发

Android调启应用程序方法总结

2015-12-15 18:39 441 查看
总结一下最近常用的在app中调用其他软件的方法。

一、调用其他应用(通用方法)

ComponentNamecomponet=newComponentName("com.heshidai.HSD","com.heshidai.HSD.main.WelcomeActivity");Intentintent=newIntent();intent.setComponent(componet);intent.setAction("android.intent.action.VIEW");intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);mContext.startActivity(intent);
这里需要注意的有:1、"android.intent.action.VIEW"意图用于显示用户的数据。设置会后会根据用户的数据类型打开相应的Activity。(这篇文章收集了大部分常用的意图,/article/11756145.html)intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);是指在一个新的栈中开启activity(因为是开启新的程序所以需要在新的栈中开启,而不是在原来程序的栈中)。如果上面两个都没有可能会报错(不过我的demo中测试时,两个都不加,没报错):Causedby:android.util.AndroidRuntimeException:CallingstartActivity()fromoutsideofanActivitycontextrequirestheFLAG_ACTIVITY_NEW_TASKflag.Isthisreallywhatyouwant?2、newComponentName()方法中的两个参数,第一个是被调启应用的包名,第二个是主Activity,首先需要确认的是第三方软件是否可以被调启。(有的程序可能设置了不允许第三方软件调启它)其次,activity是主activity,即有<actionandroid:name="android.intent.action.MAIN"/>属性。

二、调用系统应用

注意:①这些方法都应该需要做try/catch处理,防止找不到对应的程序而报错②很多系统的方法需要对应的权限,这里有常用的权限/article/11756146.html1、开启浏览器
Uriuri=Uri.parse("http://www.google.com");Intentit=newIntent(Intent.ACTION_VIEW,uri);startActivity(it);
2、打电话
try{			if(null!=phone&&phone.length()>0&&!"null".equalsIgnoreCase(phone)){				Intentintent=newIntent(Intent.ACTION_DIAL,Uri.parse(String.format("tel:%s",phone)));				if(intent!=null){					context.startActivity(intent);			}		}	}catch(Exceptione){			e.printStackTrace();	}
3、调用系统邮箱APP发送邮件到指定的邮箱
/方法1:
Uriuri=Uri.parse("mailto:xxx@abc.com");
Intentit=newIntent(Intent.ACTION_SENDTO,uri);
startActivity(it);
/方法2:
Intentit=newIntent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL,"me@abc.com");
it.putExtra(Intent.EXTRA_TEXT,"Theemailbodytext");
it.setType("text/plain");
startActivity(Intent.createChooser(it,"ChooseEmailClient"));
/方法3:
Intentit=newIntent(Intent.ACTION_SEND);
String[]tos={"me@abc.com"};
String[]ccs={"you@abc.com"};
it.putExtra(Intent.EXTRA_EMAIL,tos);
it.putExtra(Intent.EXTRA_CC,ccs);
it.putExtra(Intent.EXTRA_TEXT,"Theemailbodytext");
it.putExtra(Intent.EXTRA_SUBJECT,"Theemailsubjecttext");
it.setType("message/rfc822");
startActivity(Intent.createChooser(it,"ChooseEmailClient"));
/方法4:
Intentit=newIntent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_SUBJECT,"Theemailsubjecttext");
it.putExtra(Intent.EXTRA_STREAM,"file:///sdcard/mysong.mp3");
sendIntent.setType("audio/mp3");
startActivity(Intent.createChooser(it,"ChooseEmailClient"));
4、发短信
/方法1:
Intentit=newIntent(Intent.ACTION_VIEW);
it.putExtra("sms_body","TheSMStext");
it.setType("vnd.android-dir/mms-sms");
startActivity(it);
//方法2:
Uriuri=Uri.parse("smsto:0800000123");
Intentit=newIntent(Intent.ACTION_SENDTO,uri);
it.putExtra("sms_body","TheSMStext");
startActivity(it);
//方法三:
Stringbody="thisissmsdemo";
Intentmmsintent=newIntent(Intent.ACTION_SENDTO,Uri.fromParts("smsto",number,null));
mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY,body);
mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE,true);
mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT,true);
startActivity(mmsintent);
使用短信管理器
	//获取输入的内容
	Stringphone="123";
	Stringcontent="发送内容";
	
	//获取短信管理器的对象
	SmsManagersm=SmsManager.getDefault();
	//如果短信太长,需要分割短信(不能超过运营商限定的最长字符数)
	List<String>smss=sm.divideMessage(content);
	
	//将每条短信分别发送出去
	for(inti=0;i<smss.size();i++){
//发送短信
//arg0:对方的号码
//arg1:短信服务中心的号码,不要设置
//arg2:短信内容
	sm.sendTextMessage(phone,null,smss.get(i),null,null);
}
5、发送彩信
/方法1:
Uriuri=Uri.parse("content://media/external/images/media/23");
Intentit=newIntent(Intent.ACTION_SEND);
it.putExtra("sms_body","sometext");
it.putExtra(Intent.EXTRA_STREAM,uri);
it.setType("image/png");
startActivity(it);
//方法2:
StringBuildersb=newStringBuilder();
sb.append("file://");
sb.append(fd.getAbsoluteFile());
Intentintent=newIntent(Intent.ACTION_SENDTO,Uri.fromParts("mmsto",number,null));
//Belowextradatasarealloptional.
intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_SUBJECT,subject);
intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY,body);
intent.putExtra(Messaging.KEY_ACTION_SENDTO_CONTENT_URI,sb.toString());
intent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE,composeMode);
intent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT,exitOnSent);
startActivity(intent);
6、播放多媒体
//方法1:
Intentit=newIntent(Intent.ACTION_VIEW);
Uriuri=Uri.parse("file:///sdcard/song.mp3");
it.setDataAndType(uri,"audio/mp3");
startActivity(it);
/
/方法2:
Uriuri=Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,"1");
Intentit=newIntent(Intent.ACTION_VIEW,uri);
startActivity(it);
7、安装APK
Intentintent=newIntent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://"+filepath),"application/vnd.android.package-archive");
startActivity(intent);//安装
8、卸载APK
Uriuri=Uri.fromParts("package",strPackageName,null);
Intentit=newIntent(Intent.ACTION_DELETE,uri);
startActivity(it);
9、打开照相机
//方法1
Intentintent=newIntent("android.media.action.STILL_IMAGE_CAMERA");//调用照相机
startActivity(intent);
//方法2
Intenti=newIntent(Intent.ACTION_CAMERA_BUTTON,null);
this.sendBroadcast(i);
//方法3
longdateTaken=System.currentTimeMillis();
Stringname=createName(dateTaken)+".jpg";
fileName=folder+name;
ContentValuesvalues=newContentValues();
values.put(Images.Media.TITLE,fileName);
values.put("_data",fileName);
values.put(Images.Media.PICASA_ID,fileName);
values.put(Images.Media.DISPLAY_NAME,fileName);
values.put(Images.Media.DESCRIPTION,fileName);
values.put(Images.ImageColumns.BUCKET_DISPLAY_NAME,fileName);
UriphotoUri=getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values);
IntentinttPhoto=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);
inttPhoto.putExtra(MediaStore.EXTRA_OUTPUT,photoUri);
startActivityForResult(inttPhoto,10);
9、打开图片裁切工具
 Intentin=newIntent("com.android.camera.action.CROP");
  //需要裁减的图片格式
  in.setDataAndType(photoUriInMedia,"image/*");
  //允许裁减
  in.putExtra("crop","true");
  //剪裁后ImageView显时图片的宽
  in.putExtra("outputX",250);
  //剪裁后ImageView显时图片的高
  in.putExtra("outputY",250);
  //设置剪裁框的宽高比例
  in.putExtra("aspectX",1);
  in.putExtra("aspectY",1);
  in.putExtra("return-data",true);
  startActivityForResult(in,CAMERA_RESULT_CUT_OVER);
10、打开地图
//调起地图(所有)
try{
//geo:latitude,longitude
//geo:latitude,longitude?z=zoom,z表示zoom级别,值为数字1到23
//geo:0,0?q=my+street+address
//geo:0,0?q=business+near+city
//"geo:39.940409,116.355257?q=西直门"
UrimUri=Uri.parse(String.format("geo:%s,%s?q=%s",entity.getLatitude(),entity.getLongitude(),entity.getPopContent()));
IntentmIntent=newIntent(Intent.ACTION_VIEW,mUri);
startActivity(mIntent);}
catch(BaiduMapAppNotSupportNaviExceptione){
e.printStackTrace();
}
11、打开手机应用市场
StringmAddress="market://details?id="+getPackageName();
IntentmarketIntent=newIntent("android.intent.action.VIEW");
marketIntent.setData(Uri.parse(mAddress));
startActivity(marketIntent);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: