这是我每天下午3点拨打电话的代码:
long INTERVAL_MSEC = 24 * 60 * 60 * 1000;
Date date2am = new java.util.Date();
date2am.setHours(15);
date2am.setMinutes(0);
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
String finalPhonenumber = Globals.sms1PhoneNumbers.get(rad).number + getResources().getString(R.string.extention);
callIntent.setData(Uri.parse("tel:" + finalPhonenumber));
startActivity(callIntent);
}
};
timer.scheduleAtFixedRate(task, date2am, INTERVAL_MSEC);
现在我需要每天上午12点到下午6点间隔60分钟打电话 - 我怎么能实现这个目标?
答案 0 :(得分:1)
您需要创建一个在下午12点或00:00运行的警报:
private void startat12() {
AlarmManager alarmManager;
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 00);
c.set(Calendar.MINUTE, 00);
Long milliseconds = c.getTimeInMillis();
Long daily = 24L * 60L * 60L * 1000L;
//check if the time is already passed
if (milliseconds < System.currentTimeMillis()) {
//if already passed then push it for next day by adding just 24 hrs
milliseconds = milliseconds + daily;
}
Intent intent = new Intent(YourActivity.this, MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(YourActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, milliseconds, pendingIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, milliseconds, pendingIntent);
}
String dateFormat = "dd/MM/yyyy HH:mm";
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
Toast.makeText(this, "Alarm is set for " + formatter.format(milliseconds), Toast.LENGTH_SHORT).show();
}
现在,您的MyReceiver
将在第二天00:00收到广播
您的接收器的代码将是:
public class MyReceiver extends BroadcastReceiver {
PowerManager powerManager;
PowerManager.WakeLock wakeLock;
AlarmManager alarmManager;
Calendar c;
SimpleDateFormat formatter;
String dateFormat;
@Override
public void onReceive(final Context context, Intent intent) {
//acquire wake lock
powerManager = (PowerManager) context.getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakelockTag");
wakeLock.acquire();
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 12);
c.set(Calendar.MINUTE, 00);
Long daily = 24L * 60L * 60L * 1000L;
//Set Unlocked notification broadcast
Intent intentnew= new Intent(context, MyService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentnew, PendingIntent.FLAG_UPDATE_CURRENT);
dateFormat = "dd/MM/yyyy HH:mm";
formatter = new SimpleDateFormat(dateFormat);
Toast.makeText(context, "Alarm is set for " + formatter.format(c.getTimeInMillis()+daily), Toast.LENGTH_SHORT).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, (c.getTimeInMillis() + daily), pendingIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, (c.getTimeInMillis() + daily), pendingIntent);
}
final Handler mHandler = new Handler();
Runnable mRunnable = new Runnable() {
@Override
public void run() {
//DO your Work for each Hour
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
String finalPhonenumber = Globals.sms1PhoneNumbers.get(rad).number + context.getResources().getString(R.string.extention);
callIntent.setData(Uri.parse("tel:" + finalPhonenumber));
context.startActivity(callIntent);
Toast.makeText(context, "It Ran", Toast.LENGTH_SHORT).show();
//Also post your mHandler for next hour
if (System.currentTimeMillis() < c.getTimeInMillis()) {
mHandler.postDelayed(this, 60L * 60L * 1000L);
Toast.makeText(context, "Task will repeat after an hour", Toast.LENGTH_SHORT).show();
}
}
};
c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 18);
c.set(Calendar.MINUTE, 00);
mHandler.post(mRunnable);
wakeLock.release();
}
}
并且在清单中包含唤醒锁定权限:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
也是你的接收者:
<receiver android:name=".Notificationtwo.MyReceiver"/>