场景:在开发中,存在一个应用程序进入另外一个指定应用程序的指定界面,此时应该做的是指定该应用程序并知道该程序的包名和响应的Activity。
即将要跳转的App需要将启动的界面添加设置:android:exported="true",例如
在AndroidManifest.xml 设置设置android:exported="true
<activity android:name=".business.basic.ui.WelcomeActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
相关跳转代码
private void intentOtherAppWithBundle() {
try {
//跳转到工程信息化 , 参数格式:用户名_密码
ComponentName componetName = new ComponentName(
//这个是另外一个应用程序的包名eg:com.shuto.mobile
"packageName",
//这个参数是要启动的Activity eg:com.shuto.mobile.ui.LoginActivity
"className");
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("loginid", user.getUserAccount());
bundle.putString("password", user.getUserPassword());
intent.putExtras(bundle);
intent.setComponent(componetName);
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(HomeActivity.this, "没有安装工程信息化客户端", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(HomeActivity.this, "程序异常:" + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
如果没有设置android:exported="true",则会提示以下异常跳转到另外的APP时
![](https://img.haomeiwen.com/i2981395/e4ffae38b5833fd5.png)
解释:
Pid是进程ID,Uid是用户ID,只是Android和计算机不一样,计算机每个用户都具有一个Uid,哪个用户start的程序,这个程序的Uid就是那个那个用户,而Android中每个程序都有一个Uid,默认情况下,Android会给每个程序分配一个普通级别互不相同的Uid,如果用互相调用,只能是Uid相同才行,这就使得共享数据具有了一定安全性,每个软件之间是不能随意获得数据的。而同一个application只有一个Uid,所以application下的Activity之间不存在访问权限的问题。
之前的写法,判断。
/**
*处理打开集团客户端流程
*/
private voidhandleJiTuan() {
try{
//第一步:检测是否安装
PackageManager packageManager = getPackageManager();
List pinfo = packageManager.getInstalledPackages(0);//获取所有已安装程序的包信息
List pName =new ArrayList();//用于存储所有已安装程序的包名
//从pinfo中将包名字逐一取出,压入pName list中
if(pinfo !=null) {
for(inti =0;i < pinfo.size();i++) {
String pn = pinfo.get(i).packageName.trim();
pName.add(pn);
}
if(pName.contains("cn.wanda.app.gw")) {
Intent intent =newIntent();
intent.setAction(Intent.ACTION_VIEW);
// intent.setComponent(
// new ComponentName("cn.wanda.app.gw",
// "cn.wanda.app.gw.view.framework.home.LoadingActivity"));
intent.setComponent(
newComponentName("cn.wanda.app.gw",
"cn.wanda.app.gw.view.framework.home.MainEntrance"));
intent.setData(Uri.parse("wandaappoa://homeview"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}else{
//没有安装
CustomDialogManager.show(MainActivity.this,"尚未安装集团客户端,请先下载安装!");
}
}catch(Exception e) {
e.printStackTrace();
}
}
}
从别的APP跳转到自己的App。
- 首先一样在AndroidManifest.xml 设置设置android:exported="true" 同上
- 再次确认对方传递的参数写法
- 最后从我们要打开的入口获取对方传递的参数。
获取相关代码后我们可以正常传递数据到自己APP相关的界面中
Intent intent = new Intent(WelcomeActivity.this, HomeActivity.class);
intent.putExtra("gcLoginId",gcLoginId);
intent.putExtra("gcPassword",gcPassword);
startActivity(intent);
finish();
相关获取代码
//获取工程信息化传过来的参数
final String gcLoginId = getIntent().getStringExtra("loginid");
final String gcPassword = getIntent().getStringExtra("password");
自定义Intent中的getAction
参考链接:https://blog.csdn.net/cndrip/article/details/7191072以作笔记用
显示网页:Uri uri = Uri.parse("http://www.google.com");
Intent it = new Intent(Intent.ACTION_VIEW,uri);
startActivity(it);
显示地图:
Uri uri = Uri.parse("geo:38.899533,-77.036476");
Intent it = new Intent(Intent.Action_VIEW,uri);
startActivity(it);
路径规划:
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);
拨打电话:
调用拨号程序
Uri uri = Uri.parse("tel:xxxxxx");
Intent it = new Intent(Intent.ACTION_DIAL, uri);
startActivity(it);
Uri uri = Uri.parse("tel.xxxxxx");
Intent it =new Intent(Intent.ACTION_CALL,uri);
startActivity(it);
要使用这个必须在配置文件中加入<uses-permission id="android.permission.CALL_PHONE" />
发送SMS/MMS
调用发送短信的程序
Intent it = new Intent(Intent.ACTION_VIEW);
it.putExtra("sms_body", "The SMS text");
it.setType("vnd.android-dir/mms-sms");
startActivity(it);
发送短信
Uri uri = Uri.parse("smsto:0800000123");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "The SMS text");
startActivity(it);
发送彩信
Uri uri = Uri.parse("content://media/external/images/media/23");
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra("sms_body", "some text");
it.putExtra(Intent.EXTRA_STREAM, uri);
it.setType("image/png");
startActivity(it);
发送Email
Uri uri = Uri.parse("mailto:xxx@abc.com");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(it);
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");
it.putExtra(Intent.EXTRA_TEXT, "The email body text");
it.setType("text/plain");
startActivity(Intent.createChooser(it, "Choose Email Client"));
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, "The email body text");
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it.setType("message/rfc822");
startActivity(Intent.createChooser(it, "Choose Email Client"));
添加附件
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");
sendIntent.setType("audio/mp3");
startActivity(Intent.createChooser(it, "Choose Email Client"));
播放多媒体
Intent it = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/song.mp3");
it.setDataAndType(uri, "audio/mp3");
startActivity(it);
Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
Uninstall 程序
Uri uri = Uri.fromParts("package", strPackageName, null);
Intent it = new Intent(Intent.ACTION_DELETE, uri);
startActivity(it);
Install 程序
Uri installUri = Uri.fromParts("package", "xxx", null);
returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);
//搜索应用
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
//显示指定应用的详细页面(这个好像不支持了,找不到app_id)
Uri uri = Uri.parse("market://details?id=app_id");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
打开另一程序
Intent i = new Intent();
ComponentName cn = new ComponentName("com.drip.android2",
"com.drip.android2.AndroidSearch");
i.setComponent(cn);
i.setAction("android.intent.action.MAIN");
startActivityForResult(i, RESULT_OK);
打开联系人列表
<1>
Intent i = new Intent();
i.setAction(Intent.ACTION_GET_CONTENT);
i.setType("vnd.android.cursor.item/phone");
startActivityForResult(i, REQUEST_TEXT);
<2>
Uri uri = Uri.parse("content://contacts/people");
Intent it = new Intent(Intent.ACTION_PICK, uri);
startActivityForResult(it, REQUEST_TEXT);
打开录音机
Intent mi = new Intent(Media.RECORD_SOUND_ACTION);
startActivity(mi);
从gallery选取图片
Inten t i = new Intent();
i.setType("image/*");
i.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(i, 11);
打开照相机
<1>Intent i = new Intent(Intent.ACTION_CAMERA_BUTTON, null);
this.sendBroadcast(i);
<2>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);
播放多媒体
Intent it = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/song.mp3");
it.setDataAndType(uri, "audio/mp3");
startActivity(it);
或者
Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
或者
Uri playUri = Uri.parse("file:///sdcard/download/everything.mp3");
returnIt = new Intent(Intent.ACTION_VIEW, playUri);
发送Email
Uri uri = Uri.parse("mailto:xxx@abc.com");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(it);
或者:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, address);
intent.putExtra(Intent.EXTRA_SUBJECT, filename);
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + filename)); ;
intent.setType("text/csv");
startActivity(Intent.createChooser(intent, "EMail File"));
或者:
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");
it.putExtra(Intent.EXTRA_TEXT, "The email body text");
it.setType("text/plain");
startActivity(Intent.createChooser(it, "Choose Email Client"));
或者:
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, "The email body text");
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it.setType("message/rfc822");
startActivity(Intent.createChooser(it, "Choose Email Client"));
或者:
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");
sendIntent.setType("audio/mp3");
startActivity(Intent.createChooser(it, "Choose Email Client"));
后台发送短信
注册权限
<uses-permission android:name="android.permission.SEND_SMS" />
代码实现 :
// 获取信息内容
String message ;
// 移动运营商允许每次发送的字节数据有限,我们可以使用Android给我们提供 的短信工具。
if (message != null) {
SmsManager sms = SmsManager.getDefault();
// 如果短信没有超过限制长度,则返回一个长度的List。
List<String> texts = sms.divideMessage(message);
for (String text : texts) {
sms.sendTextMessage(“这里是接收者电话号码”, “这里是发送者电话号码”, “这里是短信内容”, null, null);
}
}
//说明
sms.sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent):
destinationAddress:接收方的手机号码
scAddress:发送方的手机号码
text:信息内容
sentIntent:发送是否成功的回执,
DeliveryIntent:接收是否成功的回执,
从google搜索内容
Intent intent = new Intent();
intent.setAction(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY,"searchString")
startActivity(intent);
APK 安装
Intent intent =new Intenet();
String filepath="/SDCard/XXX.apk";
intent.setDataAndType(Uri.parse("file://" + filepath),"application/vnd.android.package-archive");
starActivity(intent);
Activity之间的传递
Intent intent=new Intent();
intent.setClass(MainActivity.this, SecondActivity.class);
startActivity(intent);
网友评论