我需要使用警报管理器安排任务(发送短信),并且需要以1 分钟的间隔重复发送。
我真的很感激任何有关相同的帮助。提前致谢。
这是我写的代码,
AndroidManifest.xml
<uses-permission android:name="android.permission.WAKE_LOCK"/> <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/> <uses-permission android:name="android.permission.SEND_SMS"> </uses-permission> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".SMSScheduler" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <service android:name=".MyAlarmService" android:enabled="true" /> <receiver android:process=":remote" android:name=".MyReceiver" />
SMSScheduler.java
package com.example.smsmessaging;
public class SMSScheduler extends Activity {
Button btnSendSMS;
Calendar calendar = Calendar.getInstance();
private PendingIntent pendingIntent;
public void startNotification () {
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(2015, 1, 13, 10, 12, 00); calendar.getTime();
Intent myIntent = new Intent(SMSScheduler.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(SMSScheduler.this, 0, myIntent,PendingIntent.FLAG_UPDATE_CURRENT);
long trigger = calendar.getTimeInMillis();
long delay = 1*60*1000; // 1 minute delay needed
// TEST CODE
String time = calendar.getTime().toString();
Log.v("TEST", time);
Toast.makeText(getBaseContext(), time, Toast.LENGTH_LONG).show();
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, trigger, delay, pendingIntent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_smsscheduler);
btnSendSMS = (Button)findViewById(R.id.buttonSchedule);
btnSendSMS.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
startNotification();
}
});
}
}
MyReceiver.java
public class MyReceiver extends BroadcastReceiver {
int count = 1;
Map<Integer, String> oMessages = new HashMap<Integer, String>();
public void updateMessages() {
oMessages.put(1, "TEST 1");
oMessages.put(2, "TEST 2");
oMessages.put(3, "TEST 3");
oMessages.put(4, "TEST 4");
}
public String getMessage(Integer i) {
return oMessages.get(i);
}
@Override
public void onReceive(Context context, Intent intent)
{
// TODO Auto-generated method stub
updateMessages();
String phoneNumberReciver="123456789";// phone number to which SMS to be send
String message=getMessage(count);// message to send
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumberReciver, null, message, null, null);
// Show the toast like in above screen shot
Log.v("TEST", "Alarm Triggered and SMS Sent");
Toast.makeText(context, "Alarm Triggered and SMS Sent", Toast.LENGTH_LONG).show();
}
}
SMSMessagingManifest.XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.smsmessaging"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.SEND_SMS"> </uses-permission>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".SMSScheduler"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<service android:name=".MyAlarmService" android:enabled="true" />
<receiver android:process=":remote" android:name=".MyReceiver" />
</manifest>