我想在提交之后构建一些在后台运行的应用程序。
所以对于第一个我会输入一些id。
接下来当我按下提交按钮时,它会将值传递给Web服务,Web服务会以json_encode格式给出响应。
那个json数组中的内容,我想根据每天从json数组中设置闹钟。
这是我在网络服务中的json结果:
{
"length":
[
{
"kode_schedule":"sch0001",
"kode_matkul":"TIB01",
"title":"Basis Data",
"ruang":"501",
"latitude":"-6.18861653446272",
"longitude":"106.801122526546",
"lantai":"5",
"day":"Tuesday",
"hour":"17:45:00"
},
{
"kode_schedule":"sch0001",
"kode_matkul":"TIB02",
"title":"Pemrograman Berorientasi Objek",
"ruang":"LABB",
"latitude":"-6.18864706134991",
"longitude":"106.801161122636",
"lantai":"5",
"day":"Tuesday",
"hour":"19:30:00"
}
]
}
因此,根据该结果,它显示" hour"和" day"。从那以后,我想根据时间和日期设置闹钟。
你能帮我解决逻辑/代码/任何可能性吗?
这是我的闹钟代码:
public void startAt10() {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 1000 * 60 * 20;
/* Set the alarm to start at 10:30 AM */
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.DAY_OF_WEEK, 2);
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 30);
/* Repeating on every 20 minutes interval */
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 20, pendingIntent);
}
这是我的jsonparsing:
try {
// Building Parameters
List<NameValuePair> param = new ArrayList<NameValuePair>();
param.add(new BasicNameValuePair("username", txtNim.getText().toString()));
JSONObject json = jParser.makeHttpRequest("http://studentstracking.hol.es/installation.php", "POST", param);
JSONArray array = json.getJSONArray("length");
谢谢你
答案 0 :(得分:1)
就这样(我从themoviedb解析json)。 并会在上午08:00完整地通知我,这里:https://gist.github.com/im-o/fad883b73c7a56c3ce0150800e6acf3b
@Override
public void onReceive(final Context context, Intent intent) {
String currentDate = AllOtherMethod.getCurrentDates();
Log.d(TAG, "Running date : " + currentDate);
AsyncHttpClient client = new AsyncHttpClient();
String url = "https://api.themoviedb.org/3/discover/movie?api_key=" + API_KEY + "&primary_release_date.gte=" + currentDate + "&primary_release_date.lte=" + currentDate;
Log.e(TAG, "getCurrentthemoviedb: " + url);
client.get(url, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
String result = new String(responseBody);
Log.d(TAG, result);
try {
JSONObject responseObject = new JSONObject(result);
int totalResult = responseObject.getInt("total_results");
for (int i = 0; i < totalResult; i++) {
String titleMovie = responseObject.getJSONArray("results").getJSONObject(i).getString("title");
String strRelease = context.getResources().getString(R.string.str_release);
String message = titleMovie + ", " + strRelease;
showAlarmNotification(context, titleMovie, message, notifId+i);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
}
});
}
这是我的alarmManager
public void setReleaseAlarm(Context context) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiverRelease.class);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notifId, intent, 0);
if (alarmManager != null) {
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
}
}
这是我的通知
private void showAlarmNotification(Context context, String title, String message, int notifyId) {
String CHANNEL_ID = "Channel_2";
String CHANNEL_NAME = "AlarmManagerRelease channel";
NotificationManager notificationManagerCompat = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_date_range_black_24dp)
.setContentTitle(title)
.setContentText(message)
.setColor(ContextCompat.getColor(context, android.R.color.transparent))
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
.setSound(alarmSound);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
CHANNEL_NAME,
NotificationManager.IMPORTANCE_DEFAULT);
channel.enableVibration(true);
channel.setVibrationPattern(new long[]{1000, 1000, 1000, 1000, 1000});
builder.setChannelId(CHANNEL_ID);
if (notificationManagerCompat != null) {
notificationManagerCompat.createNotificationChannel(channel);
}
}
Notification notification = builder.build();
if (notificationManagerCompat != null) {
notificationManagerCompat.notify(notifyId, notification);
}
}
然后在ACTIVITY上调用方法,单击eventListener或其他
private AlarmReceiverRelease alarmReceiverRelease;
...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminder);
alarmReceiverRelease = new AlarmReceiverRelease();
}
...
private void setAlarmDaily(){
alarmReceiverDaily.setOneTimeAlarm(this);
}
答案 1 :(得分:0)
首先,您必须弄清楚如何将日期和小时转换为int,以便将其传递给日历。第一个是星期几,所以你可以使用这个功能
private int parseDayOfWeek(String day, Locale locale)
throws ParseException {
SimpleDateFormat dayFormat = new SimpleDateFormat("E", locale);
Date date = dayFormat.parse(day);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
return dayOfWeek;
}
然后要解析你的小时,你可以使用String.split(String regex)将小时字符串分成三个部分(小时,分钟和秒)。
这是您的代码的概述
int size = array.length();
for (int i = 0; i < size; i++) {
JSONObject data = array.getJSONObject(i);
int dayOfWeek = parseDayOfWeek(data.getString("day"), Locale.US);
String[] time = data.getString("hour").split(":");
startAlarm(dayOfWeek, time[0], time[1]):
}
其中startAlarm是你的startAt10()函数,但有3个参数(星期几,小时和分钟),所以你可以把它传递给你的Calendar对象。