【Android】使用Intent調用系統其它程式,使用onKeyDown對音量鍵的監聽,長按事件

來源:互聯網
上載者:User

標籤:android   簡訊   電話   intent   layout   

Intent在安卓編程中非常常見,在《【Android】多個Activity之間利用bundle傳遞數值》(點擊開啟連結)中已經展示了它可以喚醒其它Activity並在Activity之間傳遞資料。其實Intent的作用遠非於此,它還可以調用系統中其它固有程式,比如撥打到電話、傳送簡訊等。onKeyDown也是如此,不僅僅可以對裝置的菜單鍵進行監聽,這在《【Android】各式各樣的彈出框與對菜單鍵、返回鍵的監聽》(點擊開啟連結)中已經展示過的,對裝置的音量大小鍵也是可以監聽的, 下面用如下的一個小例子來說明上述兩個問題:


在本程式中,輸入要發送電信的號碼、要發送的簡訊,可以調用系統的簡訊畫面傳送簡訊。由於安卓系統的固有限制,必須是使用者自己點擊發送按鈕,才能進一步發信,只是發信內容、發送人已經為使用者,根據使用者自己的填寫的資訊填寫好了。

如,有兩個安卓模擬器,5556可以對5554發送一條aaasss的簡訊。

之後調節音量app會對其進行監聽,彈出提示資訊。

同時,我長按“長按返回案頭”按鈕可以返回案頭。

這裡撥打到電話同理,只是模擬器上沒有撥打到電話這個組件,這裡展示不了。

1、首先res\values\strings.xml中各個字元檔案如下,沒有什麼特別的:

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">intent的運用</string>    <string name="action_settings">Settings</string>   <string name="button1">長按返回案頭</string>      <string name="textview1">電話:</string>      <string name="button2">撥打到電話</string>    <string name="textview2">傳送簡訊的電話號碼:</string>        <string name="textview3">發送的簡訊:</string>        <string name="button3">傳送簡訊</string>  </resources>
2、之後對MainActivity進行布局,布局思想如下,按鈕、線性布局等一直往下擺,在水平線性布局中,用到《【Android】利用相對布局布置更新軟體的style為主題對話方塊的Activity,利用layout_weight屬性對錶格布局的行劃分》(點擊開啟連結)中的比例分割,這裡不再贅述。


因此res\layout\activity_main.xml的代碼如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/button1"        android:textSize="24sp" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <TextView            android:layout_width="0dip"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="@string/textview1"            android:textSize="24sp" />        <EditText            android:id="@+id/edittext1"            android:layout_width="0dip"            android:layout_height="wrap_content"            android:layout_weight="3"            android:inputType="text"            android:textSize="24sp" />    </LinearLayout>    <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/button2"        android:textSize="24sp" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <TextView            android:layout_width="0dip"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="@string/textview2"            android:textSize="24sp" />        <EditText            android:id="@+id/edittext2"            android:layout_width="0dip"            android:layout_height="wrap_content"            android:layout_weight="1"            android:inputType="text"            android:textSize="24sp" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <TextView            android:layout_width="0dip"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="@string/textview3"            android:textSize="24sp" />        <EditText            android:id="@+id/edittext3"            android:layout_width="0dip"            android:layout_height="wrap_content"            android:layout_weight="1"            android:inputType="text"            android:textSize="24sp" />    </LinearLayout>    <Button        android:id="@+id/button3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/button3"        android:textSize="24sp" /></LinearLayout>
3、之後是MainActivity.java中的代碼,同樣是分兩部分,一部分是各個按鈕的事件,按鈕的長按事件則使用setOnLongClickListener監聽器。另一部分是對物理按鈕的監聽,對不同的鍵位碼進行修改則是不同物理按鈕被按下的事件。使用Intent能夠輕鬆啟用各個內建的app與Activity等,這裡展示了如果調用系統固有的程式。啟用程式的時候,同時附帶相應的資訊,系統就會自動填滿相應的欄目,比如簡訊的收件者等。因此代碼如下:

package com.intent;import android.net.Uri;import android.os.Bundle;import android.view.KeyEvent;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnLongClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import android.app.Activity;import android.content.Intent;public class MainActivity extends Activity {private Button button1;private Button button2;private Button button3;private EditText editText1;private EditText editText2;private EditText editText3;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//擷取各個組件button1 = (Button) findViewById(R.id.button1);button2 = (Button) findViewById(R.id.button2);button3 = (Button) findViewById(R.id.button3);editText1 = (EditText) findViewById(R.id.edittext1);editText2 = (EditText) findViewById(R.id.edittext2);editText3 = (EditText) findViewById(R.id.edittext3);//長按返回案頭button1.setOnLongClickListener(new OnLongClickListener() {@Overridepublic boolean onLongClick(View arg0) {Intent intent = new Intent();intent.setAction(Intent.ACTION_MAIN);intent.addCategory(Intent.CATEGORY_HOME);startActivity(intent);return false;}});//調用撥打到電話的程式button2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {String phoneNumber = editText1.getText() + "";Intent intent = new Intent();intent.setAction(Intent.ACTION_CALL);intent.setData(Uri.parse("tel:" + phoneNumber));Toast.makeText(MainActivity.this, "開始撥打到電話!", Toast.LENGTH_LONG).show();startActivity(intent);}});//調用傳送簡訊的程式button3.setOnClickListener(new OnClickListener() {public void onClick(View arg0) {String phoneNumber = editText2.getText() + "";String message = editText3.getText() + "";Intent intent = new Intent();intent.setAction(Intent.ACTION_SENDTO);intent.setData(Uri.parse("smsto:" + phoneNumber));intent.putExtra("sms_body", message);Toast.makeText(MainActivity.this, "開始傳送簡訊!", Toast.LENGTH_LONG).show();startActivity(intent);}});}// 對物理按鈕的監聽@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {switch (keyCode) {case KeyEvent.KEYCODE_VOLUME_UP:Toast.makeText(MainActivity.this, "音量增加!", Toast.LENGTH_LONG).show();break;case KeyEvent.KEYCODE_VOLUME_DOWN:Toast.makeText(MainActivity.this, "音量減少!", Toast.LENGTH_LONG).show();break;}return super.onKeyDown(keyCode, event);}}

4、因為,這個程式能夠傳送簡訊、撥打到電話,我們還需要在AndroidManifest.xml中寫入這兩個許可權。因此AndroidManifest.xml修改之後如下。

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.intent"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="18" />    <uses-permission android:name="android.permission.CALL_PHONE" /><!-- 擷取撥打到電話的許可權 -->    <uses-permission android:name="android.permission.SEND_SMS" /><!-- 擷取發送資訊的許可權 -->    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.intent.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>


【Android】使用Intent調用系統其它程式,使用onKeyDown對音量鍵的監聽,長按事件

聯繫我們

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