如何調用android系統自帶的一些方法(轉載)

轉載自android.tgbus.com/Android/tutorial/201104/349972.shtml

顯示網頁:

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);

從google搜索內容

Intent intent = new Intent();

intent.setAction(Intent.ACTION_WEB_SEARCH);

intent.putExtra(SearchManager.QUERY,"searchString")

startActivity(intent);

路徑規劃:

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);

要使用這個必須在配置文件中加入

發送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);

sdk 文檔 android_sdk/docs/guide/appendix/g-app-intents.html

打開照相機

<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);

從Gallery選取圖像

Intent i = new Intent();

i.setType("image/*");

i.setAction(Intent.ACTION_GET_CONTENT);

startActivityForResult(i, 11);

打開錄音機

Intent mi = new Intent(Media.RECORD_SOUND_ACTION);

startActivity(mi);

顯示應用詳細列表

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

剛才找app id未果,結果發現用package name也可以

Uri uri = Uri.parse("market://details?id=");

這個簡單多了

尋找應用

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

打開聯系人列表

<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 i = new Intent();

ComponentName cn = new ComponentName("com.yellowbook.android2",

"com.yellowbook.android2.AndroidSearch");

i.setComponent(cn);

i.setAction("android.intent.action.MAIN");

startActivityForResult(i, RESULT_OK);

調用android自帶的圖片瀏覽器

方法一

Java code

File file=newFile("/sdcard/IMG/1.jpg");

Intent it=newIntent(Intent.ACTION_VIEW);

Uri mUri=Uri.parse("file://"+file.getPath());

it.setDataAndType(mUri,"image/*");

startActivity(it);

方法二Java code

ComponentName componentName=newComponentName("com.cooliris.media","com.cooliris.media.Gallery"); Intent intent=newIntent();

intent.setComponent(componentName);

File file=newFile("/sdcard/IMG/1.jpg");

Uri mUri=Uri.parse("file://"+file.getPath());

intent.setData(mUri);

intent.setAction(Intent.ACTION_VIEW);

startActivity(intent);

調用android自帶錄音軟件

Intent intent = new Intent("android.provider.MediaStore.RECORD_SOUND");

startActivityForResult(intent, 0);

調用android系統自帶的程序安裝器安裝程序

Intent intent = new Intent(Intent.ACTION_VIEW);intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

//filePath為文件路徑

intent.setDataAndType(Uri.parse("file://"+filePath), "application/vnd.android.packagearchive");

startActivity(intent);

調用android自帶播放器播放音樂

Intent intent = new Intent(Intent.ACTION_VIEW);

intent.setDataAndType(Uri, MimeType);

startActivity(intent);

調用android自帶視頻播放

Android中除了利用VideoView播放視頻文件外,還可以用發送Intent來調用視頻播放模塊。方法如下:

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Intent intent = new Intent(Intent.ACTION_VIEW);

String type = "video/mp4";

Uri name = Uri.parse("file:///sdcard/test.mp4");

intent.setDataAndType(name, type);

intent.setClassName("com.cooliris.media", "com.cooliris.media.MovieView");

startActivity(intent);

}

代碼中的intent.setClassName("com.cooliris.media", "com.cooliris.media.MovieView"); 一句是選擇合適的視頻播放器,如果沒有這一句,當Android中有多個視頻播放器時可能會彈出個選擇框,添加上這一句直接進入選擇的媒體播放器。不同的媒體播放器存放的位置也有所不同,查找播放器位置較為簡單的方法為點擊視頻文件并選取所需的媒體播放器的同時查看Log信息,在Log信息中查看視頻播放器的位置,填上去就可以了。

這種方法對于只要求打開并播放視頻文件的應用是可以的,但如果需要對播放器進行控制還是用VideoView的好些,相對來說VideoView容易控制。

調用android系統自帶瀏覽器

方法1:

Uri?uri?=?Uri.parse("http://blog.csdn.net/u0fly");

Intent?it??=newIntent(Intent.ACTION_VIEW,uri);

startActivity(it);

方法2:

Intent?mIntent?=newIntent();ComponentName?comp?=newComponentName("com.android.browser","com.android.browser.BrowserActivity");mIntent.setComponent(comp);

mIntent.setAction("android.intent.action.MAIN");

startActivity(mIntent);

方法1可以指定打開的頁面。

還有一種方法是在當前程序的窗口中以WebView的形式打開一個網頁

注意:需要在AndroidManifest.xml中添加

main.xml

android:layout_height="fill_parent" android:id="@+id/wv" />

主程序:

final Activity activity = this;

WebView wv=(WebView)this.findViewById(R.id.wv);

wv.getSettings().setJavaScriptEnabled(true);

wv.setWebChromeClient(new WebChromeClient(){

public void onProgressChanged(WebView view, int progress) {

// Activities and WebViews measure progress with different scales.

// The progress meter will automatically disappear when we reach 100%

activity.setProgress(progress * 1000);

}

});

wv.setWebViewClient(new WebViewClient() {

public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();

}

});

wv.loadUrl("http://www.google.com");

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,882評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,208評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,746評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,666評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,477評論 6 407
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,960評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,047評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,200評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,726評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,617評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,807評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,327評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,049評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,425評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,674評論 1 281
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,432評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,769評論 2 372

推薦閱讀更多精彩內容