注册一般放在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) 利用代码发送短信,短信是不会被系统写入数据表