学习笔记之短消息(一)

注册一般放在onResume()里

<pre name="code" class="java"><pre name="code" class="java">protected void onResume() {
		super.onResume();
		IntentFilter filter = new IntentFilter();
		filter.addAction("com.Dinggrn.SENT");
		receiver = new MyReceiver();
		registerReceiver(receiver, filter );
		refresh();
	}

 
 在onPause()注销 

protected void onPause() {
		unregisterReceiver(receiver);
		super.onPause();
	}

刷新

private void refresh() {
		biz.asyncGetAllSmses(thread_id,new OnLoadSmsesFinishListener() {
			
			@Override
			public void onLoadFinish(List<Sms> smses) {
			//	Log.d("TAG",smses.toString());
				adapter.addAll(smses,true);
				listView.setSelection(adapter.getCount()-1);
			}
		});
	}

发送短消息的几种方式:

protected void sendSMS() {
		//1)发送隐式意图发送短消息
//		Intent intent = new Intent(Intent.ACTION_SENDTO);
//		Uri data = Uri.parse("smsto:"+ "15555215556");
//		intent.setData(data);
//		intent.putExtra("sms_body", etContent.getText().toString());
//		startActivity(intent);
		
//		Intent intent = new Intent(Intent.ACTION_VIEW);
//		intent.setType("vnd.android-dir/mms-sms");
//		intent.putExtra("address", "15555215556");
//		intent.putExtra("sms_body",  etContent.getText().toString());
//		startActivity(intent);
		
		//2)利用代码发送短信
		SmsManager manager = SmsManager.getDefault();
		Intent intent = new Intent("com.Dinggrn.SENT");
		PendingIntent pi1 = PendingIntent.getBroadcast(this,0,intent,0);
		Intent intent2 = new Intent("com.Dinggrn.DELIVERY");
		PendingIntent pi2 = PendingIntent.getBroadcast(this, 0, intent2 , 0);
		
		manager.sendTextMessage("15140678165", 
				null, 
				etContent.getText().toString(), 
				pi1, 
				pi2);
		
		
	}

利用代码发送短信,有两点问题要注意:
1)申请权限 SEND_SMS
2) 利用代码发送短信,短信是不会被系统写入数据表
 
短信的接收
安卓系统后台有一个服务(Service),专门用来接收短消息。当有新的消息达到时,Service
会发送一个广播,广播的Action是“android.provider.Telephony.SMS_RECEIVED”并且将收到短消息作为广播Intent的一部分(Intent的Extra)发送出去。先到先得!

咱们的程序如果想收到短信息:
1)注册广播接收器,接收android.provider.Telephony.SMS_RECEIVED
广播
2)必须设定权限 RECEIVE_SMS
3) 设定较高的优先级 (1000左右)
4)如果不希望系统的短信程序接收短信,就执行abortBroadcast,广播停止下发

问题:如果系统短信接收程序收不到短信了,那么这条短信也不会被写入数据库






利用代码发送短信,有两点问题要注意:
1)申请权限 SEND_SMS
2) 利用代码发送短信,短信是不会被系统写入数据表

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值