android 5. callphone and sendsms
3730 点击·0 回帖
![]() | ![]() | |
![]() | 简单的实现两个模拟器的拨打电话与发送短信功能 主界面: ![]() ![]() ![]() 布局文件: 01 简单布局文件: 02 03 <?xml version="1.0" encoding="utf-8"?> 04 <LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android" 05 Android:layout_width="fill_parent" 06 Android:layout_height="fill_parent" 07 Android:orientation="vertical" > 08 09 <TextView 10 Android:layout_width="fill_parent" 11 Android:layout_height="wrap_content" 12 Android:inputType="phone" 13 Android:numeric="decimal" 14 Android:text="拨打电话" /> 15 16 <EditText 17 Android:id="@+id/phoneText" 18 Android:layout_width="250dip" 19 Android:layout_height="wrap_content" 20 Android:hint="请输入电话号码..." 21 Android:phoneNumber="true" /> 22 23 <Button 24 Android:id="@+id/callButton" 25 Android:layout_width="wrap_content" 26 Android:layout_height="wrap_content" 27 Android:text="拨打" /> 28 29 <TextView 30 Android:layout_width="fill_parent" 31 Android:layout_height="wrap_content" 32 Android:text="发送短信" /> 33 34 <EditText 35 Android:id="@+id/smsText" 36 Android:layout_width="250dip" 37 Android:layout_height="120dip" 38 Android:hint="请输入短信内容..." /> 39 40 <Button 41 Android:id="@+id/smsButton" 42 Android:layout_width="wrap_content" 43 Android:layout_height="wrap_content" 44 Android:text="发送短信" /> 45 46 </LinearLayout> 程序主要代码: 01 package smh.demo; 02 03 import Android.app.Activity; 04 import Android.app.PendingIntent; 05 import Android.content.Intent; 06 import Android.net.Uri; 07 import Android.os.Bundle; 08 import Android.telephony.SmsManager; 09 import Android.view.View; 10 import Android.view.View.OnClickListener; 11 import Android.widget.Button; 12 import Android.widget.EditText; 13 import Android.widget.Toast; 14 15 public class IntentDemoActivity extends Activity { 16 /** Called when the activity is first created. */ 17 private EditText phoneNumberText; 18 19 @Override 20 public void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.main); 23 24 Button callButton = (Button) findViewById(R.id.callButton); 25 Button smsButton = (Button) findViewById(R.id.smsButton); 26 27 phoneNumberText = (EditText) findViewById(R.id.phoneText); 28 29 // 拨打电话 30 callButton.setOnClickListener(new OnClickListener() { 31 32 @Override 33 public void onClick(View arg0) { 34 String phoneNumber = phoneNumberText.getText().toString(); 35 36 if (!"".equals(phoneNumber)) { 37 Intent intent = new Intent(Intent.ACTION_CALL, Uri 38 .parse("tel:" + phoneNumber)); 39 startActivity(intent); 40 } else { 41 Toast.makeText(IntentDemoActivity.this, "对不起, 电话号码不能为空!", 42 Toast.LENGTH_LONG).show(); 43 } 44 } 45 }); 46 47 // 发送短信 48 smsButton.setOnClickListener(new OnClickListener() { 49 50 @Override 51 public void onClick(View arg0) { 52 EditText smsNumberText = (EditText) findViewById(R.id.smsText); 53 String smsMessage = smsNumberText.getText().toString(); 54 String phoneNumber = phoneNumberText.getText().toString(); 55 56 if (!"".equals(phoneNumber)) { 57 SmsManager sms = SmsManager.getDefault(); 58 PendingIntent mPI = PendingIntent.getBroadcast( 59 IntentDemoActivity.this, 0, new Intent(), 0); 60 sms.sendTextMessage(phoneNumber, null, smsMessage, mPI, 61 null); 62 63 Toast.makeText(IntentDemoActivity.this, "发送成功", 64 Toast.LENGTH_LONG).show(); 65 } else { 66 Toast.makeText(IntentDemoActivity.this, "对不起, 电话号码不能为空!", 67 Toast.LENGTH_LONG).show(); 68 } 69 } 70 }); 71 72 } 73 } AndroidManifest.xml 01 <?xml version="1.0" encoding="utf-8"?> 02 <manifest xmlns:Android="http://schemas.Android.com/apk/res/Android" 03 package="smh.demo" 04 Android:versionCode="1" 05 Android:versionName="1.0" > 06 07 <uses-sdk Android:minSdkVersion="8" /> 08 09 <application 10 Android:icon="@drawable/ic_launcher" 11 Android:label="@string/app_name" > 12 <activity 13 Android:name=".IntentDemoActivity" 14 Android:label="@string/app_name" > 15 <intent-filter> 16 <action Android:name="Android.intent.action.MAIN" /> 17 18 <category Android:name="Android.intent.category.LAUNCHER" /> 19 </intent-filter> 20 </activity> 21 </application> 22 23 <uses-permission Android:name="Android.permission.CALL_PHONE" /> 24 <uses-permission Android:name="Android.permission.SEND_SMS" /> 25 26 </manifest> | |
![]() | ![]() |