android之Intent幾個基礎用法樣本

來源:互聯網
上載者:User

package cn.com.chenzheng_java;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class IntentActivity extends Activity {
 @Override
protected void onCreate(Bundle savedInstanceState) {
 
 super.onCreate(savedInstanceState);
 setContentView(R.layout.intent);
 Button button = (Button)findViewById(R.id.button1);
 button.setOnClickListener(new OnClickListener() {
  
  @Override
  public void onClick(View v) {
  
   sentSms2();
  }
 });
 
 
}
 
 Uri uri ;
 Intent intent ;
 
 /**
  * 開啟web網頁.隱式的Itent,有系統決定由誰來響應該intent(不用賦予許可權)
  */
 private void showWebPage(){
  uri = Uri.parse("http://www.baidu.com");
  intent = new Intent(Intent.ACTION_VIEW,uri);
  startActivity(intent);
 }
 /***
  * 開啟web地圖(不用賦予許可權)
  */
 private void showMap(){
  uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");
  intent = new Intent(Intent.ACTION_VIEW,uri);
 startActivity(intent);
 }
 /***
  * 調出來撥號主程式(不用賦予許可權)
  */
 private void caller(){
  uri = Uri.parse("tel:5556");
  intent = new Intent(Intent.ACTION_DIAL,uri);
  startActivity(intent);
 
 }
 /***
  * 直接打出去電話,注意這裡要先賦予許可權 android.permission.CALL_PHONE
  */
 private void call(){
  uri = Uri.parse("tel:5556");
  intent = new Intent(Intent.ACTION_CALL,uri);
  startActivity(intent);
 
 }
 /**
  * 調用傳送簡訊程式(不用賦予許可權)
  */
 private void sentSms(){
 
  intent = new Intent(Intent.ACTION_VIEW);
 intent.putExtra("sms_body", "The SMS text");
 intent.setType("vnd.android-dir/mms-sms");
 startActivity(intent);

 }
 /**
  * 直接傳送簡訊(需要聲明許可權android.permission.SEND_SMS)
  */
 private void sentSms2(){
  uri = Uri.parse("smsto:5556");
  intent = new Intent(Intent.ACTION_SENDTO,uri);
 intent.putExtra("sms_body", "The SMS text");
 // intent.setType("vnd.android-dir/mms-sms");
 startActivity(intent);

 }
 
 
}

-------------------------------------------------------

發送郵件

 private void sendEmail(String number){
     Intent emailIntent = new Intent();
     emailIntent.setAction(Intent.ACTION_SEND);
     emailIntent.setType("plain/text");
     // 設定地址
     emailIntent.putExtra(Intent.EXTRA_EMAIL, "chenzheng_java@163.com");
     emailIntent.putExtra(Intent.EXTRA_SUBJECT, "設定主題");
     emailIntent.putExtra(Intent.EXTRA_TITLE, "標題");
     emailIntent.putExtra(Intent.EXTRA_TEXT, "本文內容:來電了,電話號碼為"+number);
     startActivity(
     Intent.createChooser(emailIntent, "來妞,選擇個應用")
     );
     Log.i("通知", "email發送成功");
    }

方法二 Uri uri=Uri.parse("mailto:terryyhl@gmail.com");
            Intent MymailIntent=new Intent(Intent.ACTION_SEND,uri);
            startActivity(MymailIntent);

方法三 Intent testintent=new Intent(Intent.ACTION_SEND);
            String[] tos={"terryyhl@gmail.com"};
            String[] ccs={"kalaicheng@hotmail.com"};
            testintent.putExtra(Intent.EXTRA_EMAIL, tos);
            testintent.putExtra(Intent.EXTRA_CC, ccs);
            testintent.putExtra(Intent.EXTRA_TEXT, "這是內容");
            testintent.putExtra(Intent.EXTRA_SUBJECT, "這是標題");
            testintent.setType("message/rfc822");
            startActivity(Intent.createChooser(testintent, "發送"));
方法四,傳附件,這裡以SD卡的音樂檔案為例  Intent testN=new Intent(Intent.ACTION_SEND);
              testN.putExtra(Intent.EXTRA_SUBJECT, "標題");
              testN.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/music.mp3");
              startActivity(Intent.createChooser(testN, "發送"));    
使用javamail。這裡我就不介紹javamail的實現方法了,有興趣的話可以到這裡看一下,網上找到的一篇比較詳細的文章http://www.javaeye.com/topic/352753
由於目前模擬器未內建Gmail Client端程式,因此發送Email程式在送出資料後,模擬器上會發出 “No Application can perform this action”,本人沒有Android手機

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.