Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sifb.sme.ewallet.net.reminder;
- import android.app.Activity;
- import android.app.AlarmManager;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.os.PowerManager;
- import android.support.v4.app.NotificationCompat;
- import com.sifb.sme.ewallet.R;
- import com.sifb.sme.ewallet.base.MyActivity;
- import com.sifb.sme.ewallet.net.main.Activity_Main;
- import com.sifb.sme.ewallet.net.util.MyLogsUtil;
- public class ReminderGenerator {
- private static final String TAG = MyActivity.PROJ + "ReminderGenerator";
- public static final String PARC_ALARM = "PARC_ALARM";
- public static AlarmParcel setReminderInfo(int aID, String aType, String aTitle, String aImgStr) {
- MyLogsUtil.loggi(TAG, "setReminderInfo");
- AlarmParcel aAlarm = new AlarmParcel();
- aAlarm.setaID(aID);
- aAlarm.setaType(aType);
- aAlarm.setaTitle(aTitle);
- aAlarm.setaImgStr(aImgStr);
- return aAlarm;
- }
- public static void setReminderAlarm(Activity act, Long aTime, AlarmParcel iAlarm) {
- MyLogsUtil.loggi(TAG, "setReminderAlarm");
- Bundle iBundle = new Bundle();
- iBundle.putParcelable(PARC_ALARM, iAlarm);
- Intent alarmIntent = new Intent(act, AlarmReceiver.class);
- alarmIntent.putExtras(iBundle);
- PendingIntent pendingIntent = PendingIntent.getBroadcast(act, iAlarm.getaID(), alarmIntent, 0);
- AlarmManager manager = (AlarmManager) act.getSystemService(Context.ALARM_SERVICE);
- manager.set(AlarmManager.RTC_WAKEUP, aTime, pendingIntent);
- }
- public static void delReminderAlarm(Activity act, AlarmParcel iAlarm) {
- MyLogsUtil.loggi(TAG, "delReminderAlarm");
- Bundle iBundle = new Bundle();
- iBundle.putParcelable(PARC_ALARM, iAlarm);
- Intent alarmIntent = new Intent(act, AlarmReceiver.class);
- alarmIntent.putExtras(iBundle);
- PendingIntent pendingIntent = PendingIntent.getBroadcast(act, iAlarm.getaID(), alarmIntent, 0);
- AlarmManager manager = (AlarmManager) act.getSystemService(Context.ALARM_SERVICE);
- manager.cancel(pendingIntent);
- }
- public static void generateNotification(Context ctx, Bundle iBundle) {
- MyLogsUtil.loggi(TAG, "generateNotification");
- AlarmParcel nAlarm = (AlarmParcel) iBundle.getParcelable(PARC_ALARM);
- if (nAlarm != null) {
- int nID = nAlarm.getaID();
- String nTitle = nAlarm.getaTitle();
- String nLabel = ctx.getResources().getString(R.string.mysnapshot_notification_lb);
- String nContent = ctx.getResources().getString(R.string.mysnapshot_notification_ct);
- Intent nIntent = new Intent(ctx, Activity_Main.class);
- nIntent.putExtras(iBundle);
- nIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
- PendingIntent piNotification = PendingIntent.getActivity(ctx, nID, nIntent, PendingIntent.FLAG_UPDATE_CURRENT);
- NotificationCompat.Builder bNotification = new NotificationCompat.Builder(ctx);
- bNotification.setSmallIcon(R.drawable.gen_ic_launcher).setContentTitle(nLabel + ": " + nTitle).setContentText(nContent).setContentIntent(piNotification);
- bNotification.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL);
- NotificationManager mNotification = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
- mNotification.notify(nID, bNotification.build());
- PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
- PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "ExpiryWakeLock");
- // This will make the screen and power stay on
- // This will release the wakelook after 1000 ms
- wakeLock.acquire(3000);
- // Alternative you can request and / or release the wakelook via:
- // wakeLock.acquire(); wakeLock.release();
- } else {
- MyLogsUtil.loggi(TAG, "generateNotification failed: parcelable null");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement