标签: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; @Override protected 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() { @Override public 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() { @Override public 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); } }); } // 对物理按钮的监听 @Override public 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对音量键的监听,长按事件
标签:android 短信 电话 intent layout
原文地址:http://blog.csdn.net/yongh701/article/details/46646413