Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AlarmReceiver extends BroadcastReceiver {
- String TAG = getClass().getSimpleName();
- String name = "", ringtone = "";
- int gender, mZodiacID;
- SharedPreferences shared;
- @Override
- public void onReceive(Context context, Intent intent) {
- shared = PreferenceManager.getDefaultSharedPreferences(context);
- showNotification(shared, context);
- }
- private void showNotification(SharedPreferences shared, Context context) {
- Log.d(TAG, gender + " : " + name);
- gender = Integer.parseInt(shared.getString(SHARED_GENERAL_GENDER, "0"));
- name = shared.getString(SHARED_GENERAL_NAME, "");
- Log.d(TAG, gender + " : " + name);
- NotificationCompat.Builder mBuilder =
- new NotificationCompat.Builder(context)
- .setSmallIcon(R.mipmap.ic_launcher)
- .setContentTitle(context.getResources().getString(R.string.app_name))
- .setContentText("Xin chào " + name)
- .setAutoCancel(true);
- // Create explicit intent for an Activity
- Intent notifyIntent = new Intent(context, AppDetailActivity.class);
- notifyIntent.putExtra(PARAM_NAME, name);
- notifyIntent.putExtra(PARAM_GENDER_ID, gender);
- TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
- stackBuilder.addParentStack(AppDetailActivity.class);
- stackBuilder.addNextIntent(notifyIntent);
- PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT
- | PendingIntent.FLAG_ONE_SHOT);
- mBuilder.setContentIntent(resultPendingIntent);
- // Set ringtone for notification
- Uri alarmSound;
- ringtone = shared.getString(SHARED_NOTI_RINGTONE, "false");
- if (ringtone.equals("false")) {
- Log.d(TAG, "Default ringtone: " + ringtone);
- alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
- } else {
- Log.d(TAG, "Custom ringtone: " + ringtone);
- alarmSound = Uri.parse(ringtone);
- }
- mBuilder.setSound(alarmSound);
- // Set vibrate
- if (shared.getBoolean(SHARED_NOTI_VIBRATE, false)) {
- mBuilder.setVibrate(new long[]{0, 100, 200, 300});
- Log.d(TAG, "Vibrate: YES");
- } else {
- mBuilder.setVibrate(new long[]{0, 0, 0, 0});
- Log.d(TAG, "Vibrate: NO");
- }
- NotificationManager mNotificationManager =
- (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
- mNotificationManager.notify(0, mBuilder.build());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement