Android定时发短信功能 Android编程实现定时发短信功能示例
pku_android 人气:0本文实例讲述了Android编程实现定时发短信功能。分享给大家供大家参考,具体如下:
第一,要实现发短信的功能,必须要用到android系统中发短信的权限,即在AndoridManifest.xml中添加如下内容
<uses-permissionandroid:name="android.permission.SEND_SMS"/>
第二,使用AlarmManager来实现一个倒计时的功能,当时间到时发送短 信。AlarmManager有两个相似的用法:
1.在指定时常时候执行某项操作。
2.周期性的执行某项操作。AlarmManager对象需要配合Intent对象使用,可以定时开启一个Activity,发送一个Broadcast,或者开启一个Service。
以下是核心代码片段:
AlarmManager aManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE); Intent intent=new Intent(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 extends Activity { TextView onetextview; TextView twotextview; TextView threetextview; Button onebutton; Button twobutton; Dialog dialog = null; //新建日历对象,用来设置闹钟时间 Calendar calendar = Calendar.getInstance(); private SharedPreferences sharedPreferences; public void onCreate(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(new OnClickListener() { @Override public void onClick(View v) { dialog(); } }); twobutton=(Button) findViewById(R.id.twobutton); twobutton.setOnClickListener(new OnClickListener() { @Override public void onClick(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); AlarmManager aManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE); Intent intent=new Intent(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); } public void dialog(){ View view=getLayoutInflater().inflate(R.layout.shijian, null);// final TimePicker 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); new AlertDialog.Builder(this) .setTitle("设置") .setView(view) .setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(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 extends BroadcastReceiver { /** * 通过广播进行扫描,是否到达时间后再响起闹铃 * */ @Override public void onReceive(Context context, Intent intent) { SharedPreferences sharedPreferences = 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 void sendMsg(String number, String message) { SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(number, null, message, null, null); } }
希望本文所述对大家Android程序设计有所帮助。
加载全部内容