Advertisement
huyhung94

AlarmReceiver

Dec 1st, 2015
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. public class AlarmReceiver extends BroadcastReceiver {
  2.     String TAG = getClass().getSimpleName();
  3.     String name = "", ringtone = "";
  4.     int gender, mZodiacID;
  5.     SharedPreferences shared;
  6.  
  7.     @Override
  8.     public void onReceive(Context context, Intent intent) {
  9.         shared = PreferenceManager.getDefaultSharedPreferences(context);
  10.         showNotification(shared, context);
  11.     }
  12.  
  13.     private void showNotification(SharedPreferences shared, Context context) {
  14.         Log.d(TAG, gender + " : " + name);
  15.  
  16.         gender = Integer.parseInt(shared.getString(SHARED_GENERAL_GENDER, "0"));
  17.         name = shared.getString(SHARED_GENERAL_NAME, "");
  18.  
  19.         Log.d(TAG, gender + " : " + name);
  20.  
  21.         NotificationCompat.Builder mBuilder =
  22.                 new NotificationCompat.Builder(context)
  23.                         .setSmallIcon(R.mipmap.ic_launcher)
  24.                         .setContentTitle(context.getResources().getString(R.string.app_name))
  25.                         .setContentText("Xin chào " + name)
  26.                         .setAutoCancel(true);
  27.  
  28.         // Create explicit intent for an Activity
  29.         Intent notifyIntent = new Intent(context, AppDetailActivity.class);
  30.         notifyIntent.putExtra(PARAM_NAME, name);
  31.         notifyIntent.putExtra(PARAM_GENDER_ID, gender);
  32.  
  33.  
  34.         TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
  35.         stackBuilder.addParentStack(AppDetailActivity.class);
  36.         stackBuilder.addNextIntent(notifyIntent);
  37.  
  38.         PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT
  39.                 | PendingIntent.FLAG_ONE_SHOT);
  40.         mBuilder.setContentIntent(resultPendingIntent);
  41.  
  42.  
  43.         // Set ringtone for notification
  44.         Uri alarmSound;
  45.         ringtone = shared.getString(SHARED_NOTI_RINGTONE, "false");
  46.         if (ringtone.equals("false")) {
  47.             Log.d(TAG, "Default ringtone: " + ringtone);
  48.             alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  49.         } else {
  50.             Log.d(TAG, "Custom ringtone: " + ringtone);
  51.             alarmSound = Uri.parse(ringtone);
  52.         }
  53.         mBuilder.setSound(alarmSound);
  54.  
  55.         // Set vibrate
  56.         if (shared.getBoolean(SHARED_NOTI_VIBRATE, false)) {
  57.             mBuilder.setVibrate(new long[]{0, 100, 200, 300});
  58.             Log.d(TAG, "Vibrate: YES");
  59.         } else {
  60.             mBuilder.setVibrate(new long[]{0, 0, 0, 0});
  61.             Log.d(TAG, "Vibrate: NO");
  62.         }
  63.  
  64.         NotificationManager mNotificationManager =
  65.                 (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  66.         mNotificationManager.notify(0, mBuilder.build());
  67.     }
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement