Android - ayuda codigo java implementar notificaciones con sonido

   
Vista:

ayuda codigo java implementar notificaciones con sonido

Publicado por Manuel (1 intervención) el 07/08/2016 23:37:43
Hola tengo este codigo que muestra notificaciones en telefonos android pero solo vibra quisiera que tambien sonara con el sonido que por default tiene el teléfono en el momento, no tengo idea de como hacer eso alguien puede ayudar?

_______________________________________________________________________________

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package jp.co.recruit_lifestyle.android.floatingview;
 
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Vibrator;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import java.util.ArrayList;
import java.util.Iterator;
 
public class FloatingViewManager implements ScreenChangedListener, OnTouchListener, TrashViewListener {
    public static final int DISPLAY_MODE_HIDE_ALWAYS = 2;
    public static final int DISPLAY_MODE_HIDE_FULLSCREEN = 3;
    public static final int DISPLAY_MODE_SHOW_ALWAYS = 1;
    public static final int MOVE_DIRECTION_DEFAULT = 0;
    public static final int MOVE_DIRECTION_LEFT = 1;
    public static final int MOVE_DIRECTION_NONE = 3;
    public static final int MOVE_DIRECTION_RIGHT = 2;
    public static final float SHAPE_CIRCLE = 1.0f;
    public static final float SHAPE_RECTANGLE = 1.4142f;
    private static final long VIBRATE_INTERSECTS_MILLIS = 15;
    private final Context mContext;
    private int mDisplayMode;
    private final ArrayList<FloatingView> mFloatingViewList;
    private final FloatingViewListener mFloatingViewListener;
    private final Rect mFloatingViewRect;
    private final FullscreenObserverView mFullscreenObserverView;
    private boolean mIsMoveAccept;
    private FloatingView mTargetFloatingView;
    private final TrashView mTrashView;
    private final Rect mTrashViewRect;
    private final Vibrator mVibrator;
    private final WindowManager mWindowManager;
 
    public FloatingViewManager(Context context, FloatingViewListener listener) {
        this.mContext = context;
        this.mWindowManager = (WindowManager) context.getSystemService("window");
        this.mFloatingViewListener = listener;
        this.mFloatingViewRect = new Rect();
        this.mTrashViewRect = new Rect();
        this.mVibrator = (Vibrator) context.getSystemService("vibrator");
        this.mIsMoveAccept = false;
        this.mDisplayMode = MOVE_DIRECTION_NONE;
        this.mFloatingViewList = new ArrayList();
        this.mFullscreenObserverView = new FullscreenObserverView(context, this);
        this.mTrashView = new TrashView(context);
    }
 
    private boolean isIntersectWithTrash() {
        if (!this.mTrashView.isTrashEnabled()) {
            return false;
        }
        this.mTrashView.getWindowDrawingRect(this.mTrashViewRect);
        this.mTargetFloatingView.getWindowDrawingRect(this.mFloatingViewRect);
        return Rect.intersects(this.mTrashViewRect, this.mFloatingViewRect);
    }
 
    public void onScreenChanged(boolean isFullscreen) {
        if (this.mDisplayMode == MOVE_DIRECTION_NONE) {
            this.mIsMoveAccept = false;
            int state = this.mTargetFloatingView.getState();
            if (state == 0) {
                int size = this.mFloatingViewList.size();
                for (int i = MOVE_DIRECTION_DEFAULT; i < size; i += MOVE_DIRECTION_LEFT) {
                    int i2;
                    FloatingView floatingView = (FloatingView) this.mFloatingViewList.get(i);
                    if (isFullscreen) {
                        i2 = 8;
                    } else {
                        i2 = MOVE_DIRECTION_DEFAULT;
                    }
                    floatingView.setVisibility(i2);
                }
                this.mTrashView.dismiss();
            } else if (state == MOVE_DIRECTION_LEFT) {
                this.mTargetFloatingView.setFinishing();
                this.mTrashView.dismiss();
            }
        }
    }
 
    public void onTrashAnimationStarted(int animationCode) {
        if (animationCode == MOVE_DIRECTION_RIGHT || animationCode == MOVE_DIRECTION_NONE) {
            int size = this.mFloatingViewList.size();
            for (int i = MOVE_DIRECTION_DEFAULT; i < size; i += MOVE_DIRECTION_LEFT) {
                ((FloatingView) this.mFloatingViewList.get(i)).setDraggable(false);
            }
        }
    }
 
    public void onTrashAnimationEnd(int animationCode) {
        if (this.mTargetFloatingView.getState() == MOVE_DIRECTION_RIGHT) {
            removeViewToWindow(this.mTargetFloatingView);
        }
        int size = this.mFloatingViewList.size();
        for (int i = MOVE_DIRECTION_DEFAULT; i < size; i += MOVE_DIRECTION_LEFT) {
            ((FloatingView) this.mFloatingViewList.get(i)).setDraggable(true);
        }
    }
 
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        if (action == 0 || this.mIsMoveAccept) {
            int state = this.mTargetFloatingView.getState();
            this.mTargetFloatingView = (FloatingView) v;
            if (action == 0) {
                this.mIsMoveAccept = true;
            } else if (action == MOVE_DIRECTION_RIGHT) {
                boolean isIntersecting = isIntersectWithTrash();
                boolean isIntersect;
                if (state == MOVE_DIRECTION_LEFT) {
                    isIntersect = true;
                } else {
                    isIntersect = false;
                }
                if (isIntersecting) {
                    this.mTargetFloatingView.setIntersecting((int) this.mTrashView.getTrashIconCenterX(), (int) this.mTrashView.getTrashIconCenterY());
                }
                if (isIntersecting && !isIntersect) {
                    this.mVibrator.vibrate(VIBRATE_INTERSECTS_MILLIS);
                    this.mTrashView.setScaleTrashIcon(true);
                } else if (!isIntersecting && isIntersect) {
                    this.mTargetFloatingView.setNormal();
                    this.mTrashView.setScaleTrashIcon(false);
                }
            } else if (action == MOVE_DIRECTION_LEFT || action == MOVE_DIRECTION_NONE) {
                if (state == MOVE_DIRECTION_LEFT) {
                    this.mTargetFloatingView.setFinishing();
                    this.mTrashView.setScaleTrashIcon(false);
                }
                this.mIsMoveAccept = false;
            }
            if (state == MOVE_DIRECTION_LEFT) {
                this.mTrashView.onTouchFloatingView(event, (float) this.mFloatingViewRect.left, (float) this.mFloatingViewRect.top);
            } else {
                LayoutParams params = this.mTargetFloatingView.getWindowLayoutParams();
                this.mTrashView.onTouchFloatingView(event, (float) params.x, (float) params.y);
            }
        }
        return false;
    }
 
    public void setFixedTrashIconImage(int resId) {
        this.mTrashView.setFixedTrashIconImage(resId);
    }
 
    public void setActionTrashIconImage(int resId) {
        this.mTrashView.setActionTrashIconImage(resId);
    }
 
    public void setFixedTrashIconImage(Drawable drawable) {
        this.mTrashView.setFixedTrashIconImage(drawable);
    }
 
    public void setActionTrashIconImage(Drawable drawable) {
        this.mTrashView.setActionTrashIconImage(drawable);
    }
 
    public void setDisplayMode(int displayMode) {
        this.mDisplayMode = displayMode;
        Iterator i$;
        if (this.mDisplayMode == MOVE_DIRECTION_LEFT || this.mDisplayMode == MOVE_DIRECTION_NONE) {
            i$ = this.mFloatingViewList.iterator();
            while (i$.hasNext()) {
                ((FloatingView) i$.next()).setVisibility(MOVE_DIRECTION_DEFAULT);
            }
        } else if (this.mDisplayMode == MOVE_DIRECTION_RIGHT) {
            i$ = this.mFloatingViewList.iterator();
            while (i$.hasNext()) {
                ((FloatingView) i$.next()).setVisibility(8);
            }
            this.mTrashView.dismiss();
        }
    }
 
    public void setTrashViewEnabled(boolean enabled) {
        this.mTrashView.setTrashEnabled(enabled);
    }
 
    public boolean isTrashViewEnabled() {
        return this.mTrashView.isTrashEnabled();
    }
 
    @Deprecated
    public void addViewToWindow(View view, float shape, int overMargin) {
        Options options = new Options();
        options.shape = shape;
        options.overMargin = overMargin;
        addViewToWindow(view, options);
    }
 
    public void addViewToWindow(View view, Options options) {
        boolean isFirstAttach = this.mFloatingViewList.isEmpty();
        FloatingView floatingView = new FloatingView(this.mContext);
        floatingView.setInitCoords(options.floatingViewX, options.floatingViewY);
        floatingView.setOnTouchListener(this);
        floatingView.setShape(options.shape);
        floatingView.setOverMargin(options.overMargin);
        floatingView.setMoveDirection(options.moveDirection);
        floatingView.getViewTreeObserver().addOnPreDrawListener(new 1(this, floatingView));
        floatingView.addView(view);
        if (this.mDisplayMode == MOVE_DIRECTION_RIGHT) {
            floatingView.setVisibility(8);
        }
        this.mFloatingViewList.add(floatingView);
        this.mTrashView.setTrashViewListener(this);
        this.mWindowManager.addView(floatingView, floatingView.getWindowLayoutParams());
        if (isFirstAttach) {
            this.mWindowManager.addView(this.mFullscreenObserverView, this.mFullscreenObserverView.getWindowLayoutParams());
            this.mTargetFloatingView = floatingView;
        } else {
            this.mWindowManager.removeViewImmediate(this.mTrashView);
        }
        this.mWindowManager.addView(this.mTrashView, this.mTrashView.getWindowLayoutParams());
    }
 
    private void removeViewToWindow(FloatingView floatingView) {
        int matchIndex = this.mFloatingViewList.indexOf(floatingView);
        if (matchIndex != -1) {
            this.mWindowManager.removeViewImmediate(floatingView);
            this.mFloatingViewList.remove(matchIndex);
        }
        if (this.mFloatingViewList.isEmpty() && this.mFloatingViewListener != null) {
            this.mFloatingViewListener.onFinishFloatingView();
        }
    }
 
    public void removeAllViewToWindow() {
        this.mWindowManager.removeViewImmediate(this.mFullscreenObserverView);
        this.mWindowManager.removeViewImmediate(this.mTrashView);
        int size = this.mFloatingViewList.size();
        for (int i = MOVE_DIRECTION_DEFAULT; i < size; i += MOVE_DIRECTION_LEFT) {
            this.mWindowManager.removeViewImmediate((FloatingView) this.mFloatingViewList.get(i));
        }
        this.mFloatingViewList.clear();
    }
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

ayuda codigo java implementar notificaciones con sonido

Publicado por Esmeralda (11 intervenciones) el 15/08/2016 20:35:00
Hola puedes utilizar un list
View para emitir las notificaciones
1
2
3
4
5
6
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:id="@+id/lista" />



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
private ArrayAdapter adapter;
private ListView list;
 
...
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 
    // Obtener la lista
    list= (ListView) findViewById(R.id.lista);
 
    // Crear contenido de los items
    String items[]={
            "Notificación Simple",
            "Notificación + Acción",
            "Notificación + Actualización",
            "Notificación + Aviso",
            "Notificación + Progreso",
            "Notificación Personalizada",
            "Notificación Big View"
    };
 
    // Crear adaptador
    adapter= new ArrayAdapter<String>(
            this,
            android.R.layout.simple_list_item_1,
            items);
 
    // Relacionar el adaptador a la lista
    list.setAdapter(adapter);
    list.setOnItemClickListener(this);
 
}


Crear el llamado de la notificacion
public void notification1(int id, int iconId, String titulo, String contenido) {

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
NotificationCompat.Builder builder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(iconId)
                    .setLargeIcon(BitmapFactory.decodeResource(
                            getResources(),
                            R.drawable.girl_avatar_48x48
                            )
                    )
                    .setContentTitle(titulo)
                    .setContentText(contenido)
                    .setColor(getResources().getColor(R.color.Dark));
 
 
    // Construir la notificación y emitirla
    notifyMgr.notify(id, builder.build());
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar