1.无参数Activity跳转
Intent it = new Intent(Activity.this, Activity2.class);
startActivity(it);
2.向下一个Activity传递数据(使用Bundle或Intent.putExtras)
Intent it = new Intent(Activity.this, Activity2.class);
Bundle bundle=new Bundle();
bundle.putString("name", "This is from MainActivity!");
it.putExtras(bundle); // it.putExtra(“test”, "shuju”);
startActivity(it); // startActivityForResult(it,REQUEST_CODE);
相应的对于数据的获取可以采用:
Bundle bundle=getIntent().getExtras();
String name=bundle.getString("name");
如果activity设为singletask,那么接收数据时也要实现onNewIntent()方法
3.向上一个Activity返回结果(使用setResult,针对startActivityForResult(it,REQUEST_CODE)启动的
Activity)
Intent intent=getIntent();
Bundle bundle2=new Bundle();
bundle2.putString("name", "This is from ShowMsg!");
intent.putExtras(bundle2);
setResult(RESULT_OK, intent);
4.回调上一个Activity的结果处理函数(onActivityResult)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==REQUEST_CODE){
if(resultCode==RESULT_CANCELED)
setTitle("cancle");
else if (resultCode==RESULT_OK) {
String temp=null;
Bundle bundle=data.getExtras();
if(bundle!=null) temp=bundle.getString("name");
setTitle(temp);
}
}
}
5、显示网页
Uri uri = Uri.parse("http://google.com");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
6、显示地图
Uri uri = Uri.parse("geo:38.899533,-77.036476");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
//其他 geo URI
//geo:latitude,longitude
//geo:latitude,longitude?z=zoom
//geo:0,0?q=my+street+address
//geo:0,0?q=business+near+city
//google.streetview:cbll=lat,lng&cbp=1,yaw,,pitch,zoom&mz=mapZoom
7、路径规划
Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%0startLng&daddr=endLat%
0endLng&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
8、打电话
// 叫出拨号程序
Uri uri = Uri.parse("tel:0800000123");
Intent it = new Intent(Intent.ACTION_DIAL, uri);
startActivity(it);
// 直接打电话出去
Uri uri = Uri.parse("tel:0800000123");
Intent it = new Intent(Intent.ACTION_CALL, uri);
startActivity(it);
9、传送SMS/MMS
// 传送SMS
Uri uri = Uri.parse("smsto://0800000123");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "The SMS text");
startActivity(it);
// 传送 MMS
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);
10、发送 Email(三种方法)
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"));
Uri uri = Uri.parse("mailto:fengsheng.studio@hotmail.com");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
11、传送附件
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType(mimeType);
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(intent);
// 以上有两个变量需要说明:
// 1. mimeType: 如果是图片,则为"image/*",如果是音频,则为"audio/*",如果是视频,则为"video/*"
// 2. uri:如果是直接读取到文件路径并发送,则为Uri.fromFile(new File(filePath));
// 如果是从数据库中查循出来的uri,则直接传入即可.
12、播放多媒体(两种方法)
Uri uri = Uri.parse("file:///sdcard/song.mp3");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
it.setType("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);
Market 相关
13、寻找某个应用/显示某个应用的相关信息
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
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
14、打开一个应用程序
ComponentName 两个参数一个是包名 一个是包下的主类
final Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
final ComponentName cn = new ComponentName("com.android.settings",
"com.android.settings.fuelgauge.PowerUsageSummary");
intent.setComponent(cn);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Uninstall 应用程序
Uri uri = Uri.fromParts("package", strPackageName, null);
Intent it = new Intent(Intent.ACTION_DELETE, uri);
startActivity(it);
14、返回桌面
Intent unMyIntent = new Intent(Intent.ACTION_MAIN);
unMyIntent.addCategory(Intent.CATEGORY_HOME);
startActivity(unMyIntent);
15、调用程序打开相应文件
在写文件管理系统时会用到各种打开不同格式的文件的需求,由于Android系统默认内置了一些可以打开
的系统应用,但还是不能满足需求,比如打开视频文件、word等,需要安装相应的播放
软件才可以使用,这时程序会通过Intent查找可以使用的软件
实现通过代码打开一个文件需要2部分,一部分是要获取到不同文件的后缀,以便根据需求匹配相应的
Intent,另一个就是不同格式的文件打开的Intent不同
1、在values目录下定义后缀数组文件fileendings
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="fileEndingImage">
<item>.png</item>
<item>.gif</item>
<item>.jpg</item>
<item>.jpeg</item>
<item>.bmp</item>
</array>
<array name="fileEndingAudio">
<item>.mp3</item>
<item>.wav</item>
<item>.ogg</item>
<item>.midi</item>
</array>
<array name="fileEndingVideo">
<item>.mp4</item>
<item>.rmvb</item>
<item>.avi</item>
<item>.flv</item>
</array>
<array name="fileEndingPackage">
<item>.jar</item>
<item>.zip</item>
<item>.rar</item>
<item>.gz</item>
<item>.apk</item>
<item>.img</item>
</array>
<array name="fileEndingWebText">
<item>.htm</item>
<item>.html</item>
<item>.php</item>
<item>.jsp</item>
</array>
<array name="fileEndingText">
<item>.txt</item>
<item>.java</item>
<item>.c</item>
<item>.cpp</item>
<item>.py</item>
<item>.xml</item>
<item>.json</item>
<item>.log</item>
</array>
<array name="fileEndingWord">
<item>.doc</item>
<item>.docx</item>
</array>
<array name="fileEndingExcel">
<item>.xls</item>
<item>.xlsx</item>
</array>
<array name="fileEndingPPT">
<item>.ppt</item>
<item>.pptx</item>
</array>
<array name="fileEndingPdf">
<item>.pdf</item>
</array>
</resources>
2、定义OpenFiles工具类,只需传输File参数即可,然后通过返回的Intent打开文件
public class OpenFiles {
//android获取一个用于打开HTML文件的intent
public static Intent getHtmlFileIntent(File file)
{
Uri uri = Uri.parse(file.toString()).buildUpon().encodedAuthority
("com.android.htmlfileprovider").scheme("content").encodedPath(file.toString()).build();
Intent intent = new Intent("android.intent.action.VIEW");
intent.setDataAndType(uri, "text/html");
return intent;
}
//android获取一个用于打开图片文件的intent
public static Intent getImageFileIntent(File file)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "image/*");
return intent;
}
//android获取一个用于打开PDF文件的intent
public static Intent getPdfFileIntent(File file)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
return intent;
}
//android获取一个用于打开文本文件的intent
public static Intent getTextFileIntent(File file)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "text/plain");
return intent;
}
//android获取一个用于打开音频文件的intent
public static Intent getAudioFileIntent(File file)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("oneshot", 0);
intent.putExtra("configchange", 0);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "audio/*");
return intent;
}
//android获取一个用于打开视频文件的intent
public static Intent getVideoFileIntent(File file)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("oneshot", 0);
intent.putExtra("configchange", 0);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "video/*");
return intent;
}
//android获取一个用于打开CHM文件的intent
public static Intent getChmFileIntent(File file)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/x-chm");
return intent;
}
//android获取一个用于打开Word文件的intent
public static Intent getWordFileIntent(File file)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/msword");
return intent;
}
//android获取一个用于打开Excel文件的intent
public static Intent getExcelFileIntent(File file)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/vnd.ms-excel");
return intent;
}
//android获取一个用于打开PPT文件的intent
public static Intent getPPTFileIntent(File file)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
return intent;
}
//android获取一个用于打开apk文件的intent
public static Intent getApkFileIntent(File file)
{
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-
archive");
return intent;
}
}
3、定义用于检查要打开的文件的后缀是否在遍历后缀数组中
private boolean checkEndsWithInStringArray(String checkItsEnd,
String[] fileEndings){
for(String aEnd : fileEndings){
if(checkItsEnd.endsWith(aEnd))
return true;
}
return false;
}
4、通过调用OpenFiles类返回的Intent,打开相应的文件
if(currentPath!=null&¤tPath.isFile())
{
String fileName = currentPath.toString();
Intent intent;
if(checkEndsWithInStringArray(fileName, getResources().
getStringArray(R.array.fileEndingImage))){
intent = OpenFiles.getImageFileIntent(currentPath);
startActivity(intent);
}else if(checkEndsWithInStringArray(fileName, getResources().
getStringArray(R.array.fileEndingWebText))){
intent = OpenFiles.getHtmlFileIntent(currentPath);
startActivity(intent);
}else if(checkEndsWithInStringArray(fileName, getResources().
getStringArray(R.array.fileEndingPackage))){
intent = OpenFiles.getApkFileIntent(currentPath);
startActivity(intent);
}else if(checkEndsWithInStringArray(fileName, getResources().
getStringArray(R.array.fileEndingAudio))){
intent = OpenFiles.getAudioFileIntent(currentPath);
startActivity(intent);
}else if(checkEndsWithInStringArray(fileName, getResources().
getStringArray(R.array.fileEndingVideo))){
intent = OpenFiles.getVideoFileIntent(currentPath);
startActivity(intent);
}else if(checkEndsWithInStringArray(fileName, getResources().
getStringArray(R.array.fileEndingText))){
intent = OpenFiles.getTextFileIntent(currentPath);
startActivity(intent);
}else if(checkEndsWithInStringArray(fileName, getResources().
getStringArray(R.array.fileEndingPdf))){
intent = OpenFiles.getPdfFileIntent(currentPath);
startActivity(intent);
}else if(checkEndsWithInStringArray(fileName, getResources().
getStringArray(R.array.fileEndingWord))){
intent = OpenFiles.getWordFileIntent(currentPath);
startActivity(intent);
}else if(checkEndsWithInStringArray(fileName, getResources().
getStringArray(R.array.fileEndingExcel))){
intent = OpenFiles.getExcelFileIntent(currentPath);
startActivity(intent);
}else if(checkEndsWithInStringArray(fileName, getResources().
getStringArray(R.array.fileEndingPPT))){
intent = OpenFiles.getPPTFileIntent(currentPath);
startActivity(intent);
}else
{
showMessage("无法打开,请安装相应的软件!");
}
}else
{
showMessage("对不起,这不是文件!");
}