全部博文(685)
分类: 嵌入式
2014-08-20 13:49:07
第一, 要实现发短信的功能,必须要用到android系统中发短信的权限,即在AndoridManifest.xml中添加如下内容
第二, 使用AlarmManager来实现一个倒计时的功能,当时间到时发送短 信。AlarmManager有两个相似的用法:1.在指定时常时候执行某项操作。2.周期性的执行某项操作。AlarmManager对象需要配合Intent对象使用,可以定时开启一个Activity,发送一个Broadcast,或者开启一个Service。以下是核心代码片段:
AlarmManageraManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent=newIntent(this,AlarmReceiver.class);
intent.setAction("AlarmReceiver");
PendingIntent pendingIntent=PendingIntent.getBroadcast(this, 0,intent, 0);
//aManager.set(AlarmManager.RTC,calendar.getTimeInMillis(), pendingIntent);
aManager.setRepeating(AlarmManager.RTC, 0,60*1000, pendingIntent)
第三, 实现时间的设定方式。1.可以直接使用AlarmManager对象的set方法来设定具体的闹钟时间。2.可以使用TImePicker的方式来设定时间,这种方式比较灵活。
第四, 新建一个AlarmReceiver类,来对闹钟进行响应。
1. 现在AndroidMainfest.xml里添加Receiver的声明
<receiver
android:name=".AlarmReceiver"
android:label="@string/app_name">
<intent-filter>
<action android:name="AlarmReceiver"/>
intent-filter>
receiver>
在AlarmReceiver.java中实现对时间的获取以及发送短信的功能。发送短信需要用到SmsManager类,利用类 SmsManager 发送信息, smsManager 为 SmsManager 一个默认的实例.
SmsManager smsManager =SmsManager.getDefault();它的方法如下
smsManager.sendTextMessage(destinationAddress,scAddress, text, sentIntent, deliveryIntent)
其中各个参数的含义如下
destinationAddress: 收件人号码
scAddress: 短信中心服务号码, 这里设置为null
text: 发送内容
sentIntent: 发送短信结果状态信号(是否成功发送),new 一个Intent , 操作系统接收到信号后将广播这个Intent.此过程为异步.
deliveryIntent: 对方接收状态信号(是否已成功接收).
最后附上详细的代码
public class YoulainaozhongActivity extendsActivity {
TextView onetextview;
TextView twotextview;
TextView threetextview;
Button onebutton;
Button twobutton;
Dialog dialog = null;
//新建日历对象,用来设置闹钟时间
Calendar calendar =Calendar.getInstance();
privateSharedPreferences sharedPreferences;
public voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
calendar.setTimeInMillis(System.currentTimeMillis());
LinearLayout relativeLayout =(LinearLayout) findViewById(R.id.LinearLayout);
relativeLayout.setBackgroundResource(R.drawable.bejing);
onebutton=(Button)findViewById(R.id.onebutton);
onebutton.setOnClickListener(newOnClickListener() {
@Override
public voidonClick(View v) {
dialog();
}
});
twobutton=(Button)findViewById(R.id.twobutton);
twobutton.setOnClickListener(newOnClickListener() {
@Override
public voidonClick(View v) {
finish();
}
});
onetextview=(TextView)findViewById(R.id.onetextview);
twotextview=(TextView)findViewById(R.id.twotextview);
threetextview=(TextView)findViewById(R.id.threetextview);
sharedPreferences=getSharedPreferences("alarm_record", Activity.MODE_PRIVATE);
AlarmManageraManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent=newIntent(this,AlarmReceiver.class);
intent.setAction("AlarmReceiver");
PendingIntentpendingIntent=PendingIntent.getBroadcast(this, 0,intent, 0);
// aManager.set(AlarmManager.RTC,calendar.getTimeInMillis(), pendingIntent);
aManager.setRepeating(AlarmManager.RTC, 0,60*1000, pendingIntent);
}
public voiddialog(){
Viewview=getLayoutInflater().inflate(R.layout.shijian, null);//
finalTimePicker timePicker=(TimePicker)view.findViewById(R.id.timepicker);
final EditText oneeditext=(EditText)view.findViewById(R.id.oneeditext);
final EditText twoeditext=(EditText)view.findViewById(R.id.twoeditext);
timePicker.setIs24HourView(true);
newAlertDialog.Builder(this)
.setTitle("设置")
.setView(view)
.setPositiveButton("确定", newDialogInterface.OnClickListener() {
public voidonClick(DialogInterface dialog, int which) {
String timeStr=String.valueOf(timePicker.getCurrentHour())+":"+String.valueOf(timePicker.getCurrentMinute());
/*calendar.set(Calendar.HOUR_OF_DAY,timePicker.getCurrentHour());
calendar.set(Calendar.MINUTE,55);*/
onetextview.setText("您设置的时间为: "+timeStr);
twotextview.setText("您设置的号码为: "+oneeditext.getText().toString());
threetextview.setText("您设置的内容为: "+twoeditext.getText().toString());
sharedPreferences.edit().putString(timeStr, timeStr).commit();
sharedPreferences.edit().putString("haoma", oneeditext.getText().toString()).commit();
sharedPreferences.edit().putString("neirong", twoeditext.getText().toString()).commit();
}
}).setNegativeButton("取消", null).show();
}
}
public class AlarmReceiver extendsBroadcastReceiver {
/**
* 通过广播进行扫描,是否到达时间后再响起闹铃
**/
@Override
public voidonReceive(Context context, Intent intent) {
SharedPreferencessharedPreferences = context.getSharedPreferences(
"alarm_record", Activity.MODE_PRIVATE);
String hour = String.valueOf(Calendar.getInstance().get(
Calendar.HOUR_OF_DAY));
String minute = String.valueOf(Calendar.getInstance().get(
Calendar.MINUTE));
String time =sharedPreferences.getString(hour + ":" + minute, null);// 小时与分,
String haoma =sharedPreferences.getString("haoma", null);
String neirong =sharedPreferences.getString("neirong", null);
if (time!= null) {// 判断是否为空,然后通过创建,
// MediaPlayer mediaPlayer =MediaPlayer.create(context, R.raw.a);
Toast.makeText(context,"短信已经发送成功",Toast.LENGTH_LONG).show();
// mediaPlayer.start();// 开始 ;
sendMsg(haoma, neirong);
}
}
private voidsendMsg(String number, String message) {
SmsManager smsManager =SmsManager.getDefault();
smsManager.sendTextMessage(number,null, message, null, null);
}
}