Intent系列之一:用intent打開網頁、電話號碼或其他內容

    如果我們希望一個應用程序中的某些實體交由另一個應用程序處理,而不需要知道或關心那個應用程序的具體實現,則可以調用Intent構造器,然後調用startActivity。

     Intent構造器有兩個參數:進行的操作和操作所在的實體。最常見的操作是Intent.ACTION_VIEW,其字符串表現形式是android.intent.action.VIEW。第二個參數通常是個URL,或者按照Android中較不精確(更通用)的說法------是一個URI。URI可以用URI類中的靜態方法parse()創建。

    Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(data));

    data包含的是以http:開頭的頁面URL、tel:開頭的電話號碼等。假定代碼在一個activity中,我們調用startActivity(intent),用戶將會看到Web瀏覽器、電話撥號器、地圖應用程序或其他應用。 


    1. 瀏覽網頁

Intent intent = new Intent();//或者是像下面這樣的寫,這裏是用了意圖(intent)裏面的標準action這裏面還有許多的標準意圖可以使用
//   intent.setAction("android.intent.action.VIEW"); //這裏面的意圖可以自已寫也可以像下面一句這樣直接用Intent裏面的常量 
   intent.setAction(Intent.ACTION_VIEW);
 intent.setData(Uri.parse("http://www.google.com"));//設置一個URI地址
 this.activity.startActivity(intent);//用startActivity打開這個指定的網頁。
 

    2.從google搜索內容
Intent intent = new Intent();
intent.setAction(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY,"searchString")
startActivity(intent);
 
    3.顯示地圖
Uri uri = Uri.parse("geo:38.899533,-77.036476");
Intent it = new Intent(Intent.Action_VIEW,uri);
startActivity(it);
 
    4.路徑規劃
Uri uri = Uri.parse("http://maps.google.com/maps?f=dsaddr=startLat startLng&daddr=endLat endLng&hl=en");
Intent it = new Intent(Intent.ACTION_VIEW,URI);
startActivity(it);
 
    5.撥打電話
Uri uri = Uri.parse("tel:xxxxxx");
Intent it = new Intent(Intent.ACTION_DIAL, uri); 
startActivity(it);


    6.調用發短信的程序
Intent it = new Intent(Intent.ACTION_VIEW);  
it.putExtra("sms_body", "The SMS text");  
it.setType("vnd.android-dir/mms-sms");  
startActivity(it);
 
 


    7.發送短信
Uri uri = Uri.parse("smsto:0800000123");  
Intent it = new Intent(Intent.ACTION_SENDTO, uri);  
it.putExtra("sms_body", "The SMS text");  
startActivity(it);
String body="this is sms demo";
Intent mmsintent = new Intent(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);
 


    8.發送彩信
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);
StringBuilder sb = new StringBuilder();
sb.append("file://");
sb.append(fd.getAbsoluteFile());
Intent intent = new Intent(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);
 


    9.發送Email
Uri uri = Uri.parse("mailto:[email protected]");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(it);
Intent it = new Intent(Intent.ACTION_SEND);  
it.putExtra(Intent.EXTRA_EMAIL, "[email protected]");  
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={"[email protected]"};    
String[] ccs={"[email protected]"};    
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"));
 
 
    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); 
 
 
    11.卸載apk
Uri uri = Uri.fromParts("package", strPackageName, null);  
Intent it = new Intent(Intent.ACTION_DELETE, uri);  
startActivity(it);
 
 
    12.安裝apk
Uri installUri = Uri.fromParts("package", "xxx", null);
returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章