5.Intent學習 執行個體

來源:互聯網
上載者:User

Intent學習

1        Intent:就是一次對執行的操作的抽象的描述,Intent是Android開發中的靈魂。通過幾個執行個體的學習,希望能夠學會Intent的使用。

1.1       撥打到電話執行個體

1)      在main.xml檔案中添加一個按鈕

<Button

      android:id="@+id/mainBtn"

      android:layout_width="wrap_content"

      android:layout_height="wrap_content"

      android:text="CALL"

    />

2)      在mainActivity.java檔案中,添加Intent

public class MainActivityextends Activity {

   private Button mainBtn=null;

  

    /** Calledwhen the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        mainBtn=(Button)findViewById(R.id.mainBtn);

        mainBtn.setOnClickListener(listener);

       

    }

   
private
OnClickListener listener=new OnClickListener()

   {

    

     @Override

     public void onClick(View v)

     {

        Intent intent=new Intent();

        intent.setAction(Intent.ACTION_CALL);

        intent.setData(Uri.parse("tel:110"));

        startActivity(intent);

       

     }

   };

}

如果完成以上步驟還不行,程式運行會有異常,原因是系統調用撥號程式需要有許可才可以。

3)      所以需要在AndroidManifest.xml檔案中加入語句:

<uses-permission android:name="android.permission.CALL_PHONE"/>

注意:語句加在 </application>標籤外部。

 

1.2 傳送簡訊

1) 修改上面的Intent部分代碼為:

public void onClick(View v)

     {

        Intent intent=new Intent();

       
intent.setAction(Intent.ACTION_SENDTO);

        intent.setData(Uri.parse("smsto:110"));

        intent.putExtra("sms_body", "Welcome toAndroid...");

        startActivity(intent);

       

    }

2) 添加許可權語句:

  <uses-permission android:name="android.permission.SEND_SMS"/>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.