Intent用法实例

来源:互联网 发布:iPhone软件源 编辑:程序博客网 时间:2024/05/29 12:29

Intent用法很灵活,以下列出了Intent的一些常用实例,主要包括Activity的跳转及启动:

  • 无参数Activity跳转

[java] view plaincopy
  1. Intent intent = new Intent(Activity.Main.this, Activity2.class);  
  2. startActivity(intent);   

  • 向下一个Activity传递数据(使用Bundle和Intent.putExtras)

[java] view plaincopy
  1. Intent intent = new Intent(Activity.Main.this, Activity2.class);  
  2. Bundle bundle=new Bundle();  
  3. bundle.putString("name""This is from MainActivity!");  
  4. intent.putExtras(bundle);  
  5. startActivity(intent);  startActivityForResult(intent,REQUEST_CODE);  
对于数据的获取可以采用:
[java] view plaincopy
  1. Bundle bundle=getIntent().getExtras();  
  2. String name=bundle.getString("name");  

  • 向上一个Activity返回结果(使用setResult,针对 startActivityForResult(it,REQUEST_CODE)启动的Activity)

[java] view plaincopy
  1. Intent intent=getIntent();  
  2. Bundle bundle=new Bundle();  
  3. bundle.putString("name""This is from Activity2!");  
  4. intent.putExtras(bundle);  
  5. setResult(RESULT_OK, intent);  

注:setResult(int resultCode, Intent data);中的参数resultCode的作用是:在上一个Activity的onActivityResult函数中作为第二次判断用(第一次判断在startActivityForResult(Intent intent, int requestCode);中指定)

  • 回调上一个Activity的结果处理函数(onActivityResult)

[java] view plaincopy
  1. @Override  
  2.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  3.         // TODO Auto-generated method stub  
  4.         super.onActivityResult(requestCode, resultCode, data);  
  5.         if (requestCode==REQUEST_CODE){  
  6.             if(resultCode==RESULT_CANCELED)  
  7.                   setTitle("cancle");  
  8.             else if (resultCode==RESULT_OK) {  
  9.                  String temp=null;  
  10.                  Bundle bundle=data.getExtras();  
  11.                  if(bundle!=null)   temp=bundle.getString("name");  
  12.                  setTitle(temp);  
  13.             }  
  14.         }  
  15.     }  

  • 显示网页

[java] view plaincopy
  1. Uri uri = Uri.parse("http://google.com");   
  2. Intent intent = new Intent(Intent.ACTION_VIEW, uri);   
  3. startActivity(intent);   

  • 显示地图

 

[java] view plaincopy
  1. Uri uri = Uri.parse("geo:38.899533,-77.036476");   
  2. Intent intent = new Intent(Intent.ACTION_VIEW, uri);    
  3. startActivity(intent);    
  4.    
  5. //其他 geo URI 範例   
  6. //geo:latitude,longitude   
  7. //geo:latitude,longitude?z=zoom   
  8. //geo:0,0?q=my+street+address   
  9. //geo:0,0?q=business+near+city   
  10. //google.streetview:cbll=lat,lng&cbp=1,yaw,,pitch,zoom&mz=mapZoom   

  • 路径规划

  

[java] view plaincopy
  1. Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");   
  2. Intent intent = new Intent(Intent.ACTION_VIEW, uri);   
  3. startActivity(intent);   
  4. //where startLat, startLng, endLat, endLng are a long with 6 decimals like: 50.123456   

  • 打电话

 

[java] view plaincopy
  1. //叫出拨号程序    
  2. Uri uri = Uri.parse("tel:0800000123");   
  3. Intent intent = new Intent(Intent.ACTION_DIAL, uri);   
  4. startActivity(intent);   
  5. //直接打电话出去    
  6. Uri uri = Uri.parse("tel:0800000123");   
  7. Intent intent = new Intent(Intent.ACTION_CALL, uri);   
  8. startActivity(intent);   
  9. //用這個,要在 AndroidManifest.xml 中,加上   
  10. //<uses-permission id="android.permission.CALL_PHONE" />   

  • 传送SMS/MMS

[java] view plaincopy
  1. //调用短信程序    
  2. Intent intent = new Intent(Intent.ACTION_VIEW, uri);   
  3. intent.putExtra("sms_body""The SMS text");    
  4. intent.setType("vnd.android-dir/mms-sms");   
  5. startActivity(intent);  
  6. //传送消息    
  7. Uri uri = Uri.parse("smsto://0800000123");   
  8. Intent intent = new Intent(Intent.ACTION_SENDTO, uri);   
  9. intent.putExtra("sms_body""The SMS text");   
  10. startActivity(intent);  
  11. //传送 MMS    
  12. Uri uri = Uri.parse("content://media/external/images/media/23");   
  13. Intent intent = new Intent(Intent.ACTION_SEND);    
  14. intent.putExtra("sms_body""some text");    
  15. intent.putExtra(Intent.EXTRA_STREAM, uri);   
  16. intent.setType("image/png");    
  17. startActivity(it);   
  

  • 传送 Email

[java] view plaincopy
  1. Uri uri = Uri.parse("mailto:xxx@abc.com");   
  2. Intent intent = new Intent(Intent.ACTION_SENDTO, uri);   
  3. startActivity(intent);   
  4. Intent intent = new Intent(Intent.ACTION_SEND);   
  5. intent.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");   
  6. intent.putExtra(Intent.EXTRA_TEXT, "The email body text");   
  7. intent.setType("text/plain");   
  8. startActivity(Intent.createChooser(intent, "Choose Email Client"));   
  9. Intent intent=new Intent(Intent.ACTION_SEND);     
  10. String[] tos={"me@abc.com"};     
  11. String[] ccs={"you@abc.com"};     
  12. intent.putExtra(Intent.EXTRA_EMAIL, tos);     
  13. intent.putExtra(Intent.EXTRA_CC, ccs);     
  14. intent.putExtra(Intent.EXTRA_TEXT, "The email body text");     
  15. intent.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");     
  16. intent.setType("message/rfc822");     
  17. startActivity(Intent.createChooser(intent, "Choose Email Client"));  
  18. //传送附件  
  19. Intent intent = new Intent(Intent.ACTION_SEND);   
  20. intent.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");   
  21. intent.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");   
  22. sendIntent.setType("audio/mp3");   
  23. startActivity(Intent.createChooser(intent, "Choose Email Client"));   
  

  • 播放多媒体

[java] view plaincopy
  1. Uri uri = Uri.parse("file:///sdcard/song.mp3");   
  2. Intent intent = new Intent(Intent.ACTION_VIEW, uri);   
  3. intent.setType("audio/mp3");   
  4. startActivity(intent);  
  5. Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");   
  6. Intent intent = new Intent(Intent.ACTION_VIEW, uri);   
  7. startActivity(intent);   

  • Market 相关

[java] view plaincopy
  1. //寻找某个应用  
  2. Uri uri = Uri.parse("market://search?q=pname:pkg_name");  
  3. Intent intent = new Intent(Intent.ACTION_VIEW, uri);   
  4. startActivity(intent);   
  5. //where pkg_name is the full package path for an application  
  6. //显示某个应用的相关信息  
  7. Uri uri = Uri.parse("market://details?id=app_id");   
  8. Intent intent = new Intent(Intent.ACTION_VIEW, uri);  
  9. startActivity(intent);   
  10. //where app_id is the application ID, find the ID    
  11. //by clicking on your application on Market home    
  12. //page, and notice the ID from the address bar   

  • Uninstall 应用程序

[java] view plaincopy
  1. Uri uri = Uri.fromParts("package", strPackageName, null);  
  2. Intent intent = new Intent(Intent.ACTION_DELETE, uri);    
  3. startActivity(intent);    

0 0
原创粉丝点击