打电话,发送短信
2015-09-28 10:37
726 查看
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button phone = (Button) findViewById(R.id.phone); Button massage = (Button) findViewById(R.id.massage); final EditText mobileText = (EditText) findViewById(R.id.mobile); final EditText msgText = (EditText) findViewById(R.id.etmsg); phone.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String mobile = mobileText.getText().toString(); Intent intent = new Intent(); intent.setAction("android.intent.action.CALL"); intent.setData(Uri.parse("tel:"+ mobile)); startActivity(intent); } }); massage.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String phone=mobileText.getText().toString().trim(); String msg=msgText.getText().toString().trim(); send(phone,msg); } }); } private void send(String number, String message){ String SENT = "sms_sent"; String DELIVERED = "sms_delivered"; PendingIntent sentPI = PendingIntent.getActivity(this, 0, new Intent(SENT), 0); PendingIntent deliveredPI = PendingIntent.getActivity(this, 0, new Intent(DELIVERED), 0); registerReceiver(new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { switch(getResultCode()) { case Activity.RESULT_OK: Log.i("====>", "Activity.RESULT_OK"); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: Log.i("====>", "RESULT_ERROR_GENERIC_FAILURE"); break; case SmsManager.RESULT_ERROR_NO_SERVICE: Log.i("====>", "RESULT_ERROR_NO_SERVICE"); break; case SmsManager.RESULT_ERROR_NULL_PDU: Log.i("====>", "RESULT_ERROR_NULL_PDU"); break; case SmsManager.RESULT_ERROR_RADIO_OFF: Log.i("====>", "RESULT_ERROR_RADIO_OFF"); break; } } }, new IntentFilter(SENT)); registerReceiver(new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent){ switch(getResultCode()) { case Activity.RESULT_OK: Log.i("====>", "RESULT_OK"); break; case Activity.RESULT_CANCELED: Log.i("=====>", "RESULT_CANCELED"); break; } } }, new IntentFilter(DELIVERED)); SmsManager smsm = SmsManager.getDefault(); smsm.sendTextMessage(number, null, message, sentPI, deliveredPI); } }
layout中的界面布局代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.test.MainActivity$PlaceholderFragment" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请输入电话号码" /> <EditText android:id="@+id/mobile" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@+id/textView1" android:layout_marginTop="24dp" android:ems="10" android:hint="请输入号码" android:inputType="phone" > <requestFocus /> </EditText> <EditText android:id="@+id/etmsg" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/mobile" android:hint="请输入消息" android:ems="10" /> <RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/etmsg" android:layout_below="@+id/etmsg" > <Button android:id="@+id/phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="拨出此号码" /> <Button android:id="@+id/massage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginLeft="17dp" android:layout_toRightOf="@+id/phone" android:text="向此号发短信" /> </RelativeLayout> </RelativeLayout>
相关文章推荐
- 数据处理----Java数据处理之RMI
- 使用Ganglia监控Flume的性能指标数据
- Spring与Mybatis整合,jar版本要求
- Cocos2d-x 进度条倒计时实现
- TOMCAT端口和域名配置
- 贪心算法和动态规划算法
- 深入理解hashmap的resize
- 如何ios中间Safari在开发了类似的native app像全屏webapp
- 四类八种基本数据类型
- 150928错误认识
- 序列化和反序列化
- asp rs开启关闭问题
- SQL Server附加数据库出错:无法打开物理文件,操作系统错误5
- 文章标题
- CSS的定位原理
- RHL6.4 修改IP,主机名脚本
- kafka多线程消费
- CentOS 7关闭图形桌面开启文本界面
- Swift - 判端网络连接状态,连接类型(3G还是Wifi)
- ios 苹果官方的单例(学习)