Android调启应用程序方法总结

总结一下最近常用的在app中调用其他软件的方法。

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

 
   
   
ComponentName componet = new ComponentName ( "com.heshidai.HSD" , "com.heshidai.HSD.main.WelcomeActivity" );
Intent intent = new Intent ();
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。(这篇文章收集了大部分常用的意图,http://blog.csdn.net/bihansheng2010/article/details/50319235)

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 是指在一个新的栈中开启activity(因为是开启新的程序所以需要在新的栈中开启,而不是在原来程序的栈中)。

如果上面两个都没有可能会报错(不过我的demo中测试时,两个都不加,没报错): 

Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

2、new ComponentName()方法中的两个参数,第一个是被调启应用的包名,第二个是主Activity ,

首先需要确认的是第三方软件是否可以被调启。(有的程序可能设置了不允许第三方软件调启它)

其次,activity是主activity,即有<action android:name="android.intent.action.MAIN" /> 属性。


二、调用系统应用

注意:①这些方法都应该需要做try/catch 处理,防止找不到对应的程序而报错
          ② 很多系统的方法需要对应的权限,这里有常用的权限 http://blog.csdn.net/bihansheng2010/article/details/50319245
         

    1、开启浏览器
   
   
Uri uri = Uri . parse ( "http://www.google.com" );
Intent it = new Intent ( Intent . ACTION_VIEW , uri );
startActivity ( it );

2、打电话

  
  
        try {
if ( null != phone && phone . length () > 0 && ! "null" . equalsIgnoreCase ( phone )) {
Intent intent = new Intent ( Intent . ACTION_DIAL , Uri . parse ( String . format ( "tel:%s" , phone )));
if ( intent != null ) {
context . startActivity ( intent );
}
}
} catch ( Exception e ) {
e . printStackTrace ();
}

3、调用系统邮箱APP发送邮件到指定的邮箱
  
  
  
/方法1
Uri uri =Uri.parse("mailto:xxx@abc.com");
Intent it = newIntent(Intent.ACTION_SENDTO, uri);
startActivity(it);
/方法2
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL,"me@abc.com");
it.putExtra(Intent.EXTRA_TEXT, "Theemail body text");
it.setType("text/plain");
startActivity(Intent.createChooser(it,"Choose Email Client"));
 
/方法3
Intent it=new Intent(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, "Thee mail body text");
it.putExtra(Intent.EXTRA_SUBJECT, "Theemail subject text");
it.setType("message/rfc822");
startActivity(Intent.createChooser(it,"Choose Email Client"));
/方法4
Intent it = newIntent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_SUBJECT, "Theemail subject text");
it.putExtra(Intent.EXTRA_STREAM,"file:///sdcard/mysong.mp3");
sendIntent.setType("audio/mp3");
startActivity(Intent.createChooser(it,"Choose Email Client"));

4、 发短信
   
   
/方法1
Intent it = newIntent(Intent.ACTION_VIEW);
it.putExtra("sms_body", "TheSMS text");
it.setType("vnd.android-dir/mms-sms");
startActivity(it);
//方法2:
Uri uri =Uri.parse("smsto:0800000123");
Intent it = newIntent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "TheSMS text");
startActivity(it);
//方法三:
String body="this is sms demo";
Intent mmsintent = 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);
使用短信管理器
   
   
//获取输入的内容
String phone = "123";
String content ="发送内容";
//获取短信管理器的对象
SmsManager sm = SmsManager.getDefault();
//如果短信太长,需要分割短信(不能超过运营商限定的最长字符数)
List<String> smss= sm.divideMessage(content);
//将每条短信分别发送出去
for (int i = 0; i < smss.size(); i++) {
 
//发送短信
//arg0:对方的号码
//arg1:短信服务中心的号码,不要设置
//arg2:短信内容
sm.sendTextMessage(phone, null, smss.get(i), null,null);
}

5、 发送彩信
   
   
/方法1
Uri uri =Uri.parse("content://media/external/images/media/23");
Intent it = newIntent(Intent.ACTION_SEND);
it.putExtra("sms_body","some text");
it.putExtra(Intent.EXTRA_STREAM, uri);
it.setType("image/png");
startActivity(it);
 
//方法2
StringBuilder sb = new StringBuilder();
sb.append("file://");
sb.append(fd.getAbsoluteFile());
Intent intent = newIntent(Intent.ACTION_SENDTO, Uri.fromParts("mmsto", number, null));
// Below extra datas are all optional.
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
Intent it = new Intent(Intent.ACTION_VIEW);
Uri uri =Uri.parse("file:///sdcard/song.mp3");
it.setDataAndType(uri,"audio/mp3");
startActivity(it); 
/ /方法2
Uri uri =Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,"1");
Intent it = new Intent(Intent.ACTION_VIEW,uri);
startActivity(it);

7、 安装APK
   
   
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + filepath),"application/vnd.android.package-archive");
startActivity(intent);// 安装


8、卸载APK
   
   
Uri uri =Uri.fromParts("package", strPackageName, null);
Intent it = newIntent(Intent.ACTION_DELETE, uri);
startActivity(it);


9、打开照相机 
   
   
//方法1
Intent intent = new Intent("android.media.action.STILL_IMAGE_CAMERA"); //调用照相机
startActivity(intent);
//方法2
Intent i = new Intent(Intent.ACTION_CAMERA_BUTTON, null);
this.sendBroadcast(i);
//方法3
long dateTaken = System.currentTimeMillis();
String name = createName(dateTaken) + ".jpg";
fileName = folder + name;
ContentValues values = new ContentValues();
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);
Uri photoUri = getContentResolver().insert( MediaStore . Images . Media . EXTERNAL_CONTENT_URI , values );
Intent inttPhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
inttPhoto.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(inttPhoto, 10);

9、打开图片裁切工具
   
   
 Intent in = new Intent("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=西直门"
Uri mUri = Uri.parse(String.format("geo:%s,%s?q=%s",entity.getLatitude(),entity.getLongitude(),entity.getPopContent()));
Intent mIntent = new Intent(Intent.ACTION_VIEW,mUri);
startActivity(mIntent);}
catch (BaiduMapAppNotSupportNaviException e) {
e.printStackTrace();
}

11、打开手机应用市场
   
   
String mAddress = "market://details?id=" + getPackageName();
Intent marketIntent = new Intent("android.intent.action.VIEW");
marketIntent .setData(Uri.parse(mAddress ));
startActivity(marketIntent );










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值