gpt4 book ai didi

java - Android设置具体时间和在Alarmmanager

转载 作者:行者123 更新时间:2023-11-30 02:43:34 25 4
gpt4 key购买 nike

我想在警报管理器中设置具体的时间和日期。我已经尝试在日历中设置时间。但是在运行应用程序后,它会在特定日期正确发送电子邮件,但不会发送特定时间,无论我必须在警报管理器中设置什么。有人可以帮助我吗。提前致谢。

这是我的警报管理器代码

btnSubmit = (Button) findViewById(R.id.buttonSubmit);
btnSubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
cal.setTime(emailDate);
cal.set(Calendar.PM, 5);
cal.set(Calendar.MINUTE, 5);
cal.set(Calendar.SECOND, 0);
Date dt = cal.getTime();
Long longDate = (long) dt.getTime();
System.out.println("dt :=" + dt);
System.out.println("Long Date :=" + longDate);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Xyz.this, 1, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, longDate, pendingIntent);
}
}

这是AlarmManager代码

      strMinusDate = "22-08-2014";
String[] splitDate_Parts = strMinusDate.split("-");
saparated_Day = splitDate_Parts[0];
saparated_Month = splitDate_Parts[1];
saparated_Year = splitDate_Parts[2];

int convertDay = Integer.valueOf(saparated_Day);
int convertMonth = Integer.valueOf(saparated_Month);
int convertYear = Integer.valueOf(saparated_Year);
cal.set(convertDay,convertMonth,convertYear,11,30,00);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Xyz.this, 1, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

这是 AlarmManagerBroadcastReceiver 类代码。

public class AlarmManagerBroadcastReceiver extends BroadcastReceiver 
{
String strServiceName;
String strSuppliername;
String strEmail;
String strReplacementDate;
String strIntervalDays;
String strNextReplacementDate;
String strEmailDate;

@Override
public void onReceive(Context con, Intent arg1)
{
// TODO Auto-generated method stub
SharedPreferences prefs = con.getSharedPreferences("MyPref_Email", Context.MODE_PRIVATE);

strServiceName=prefs.getString("ServiceName",null);
strSuppliername=prefs.getString("Suppliername" , null);
strEmail=prefs.getString("Email" , null);
strReplacementDate=prefs.getString("ReplacementDate" , null);
strIntervalDays=prefs.getString("IntervalDays" , null);
strNextReplacementDate=prefs.getString("NextReplacementDate" ,null);
strEmailDate = prefs.getString("EmailDate", null);

System.out.println(strServiceName+" , "+strSuppliername+" , "+strEmail+" , "+strReplacementDate+" , "+strIntervalDays+" , "+strNextReplacementDate);


Log.e("ON CREATE" , "=" + strServiceName);
Log.e("ON CREATE" , "=" + strSuppliername);
Log.e("ON CREATE" , "=" + strEmail);
Log.e("ON CREATE" , "=" + strReplacementDate);
Log.e("ON CREATE" , "=" + strIntervalDays);
Log.e("ON CREATE" , "=" + strNextReplacementDate);
Log.e("ON CREATE" , "=" + strEmailDate);


String subject =strServiceName;
System.out.println(" subject = " + subject);


String body = strServiceName
+","
+strSuppliername
+","
+strEmail
+","
+strReplacementDate
+","
+strIntervalDays
+","
+strNextReplacementDate
+","
+strEmailDate;

Log.e("body " , " = " + body);
System.out.println(" body = " + body);


Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_EMAIL , new String[]{strEmail});
shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
shareIntent.putExtra(Intent.EXTRA_TEXT , body);
Intent new_intent = Intent.createChooser(shareIntent, "Share via");
new_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
con.startActivity(new_intent);
Log.e("Send Email ="," In My Service ");
System.out.println("Service start time");

}

}

最佳答案

某些ACTION(event) 需要由发件人触发,以便receiver响应该特定事件而醒来。您未在 intent 中提供任何事件你正在开火

//set the time in this way by giving proper values
calendar.set(selectedYear, selectedMonth, selectedDay,selectedHour, selectedMinute);

//create the AlarmManager in this way using that calender object
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC,
calendar.getTimeInMillis(), pendingIntent);

编辑:

创建时 intent在你的AlarmManager代码,

Intent myIntent = new Intent("com.abc.pqr"); //just a user defined action
PendingIntent pendingIntent = PendingIntent.getBroadcast(Xyz.this, 1, myIntent, 0);

在 list 中添加:

  <permission android:name="com.abc.pqr"></permission>
<uses-permission android:name="com.abc.pqr" />

<application>标记,添加

<receiver
android:name="yourFullPackagePath.AlarmManagerBroadcastReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="com.abc.pqr" />
</intent-filter>
</receiver>

关于java - Android设置具体时间和在Alarmmanager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25425590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com