estoy creando una alarma y tengo un problema cuando añado .setSound()
este no funciona.
¿Alguien tiene alguna idea de por qué no funciona? Además me gustaría que cuando llegue la hora este invoque una activity llamada stop_alarm
para parar la alarma o para retrasarla y en tal caso, la app esté cerrada muestre una notificación o ejecute esa activity
Mi plan es que cuando se abra esta activity stop_alarm
suene una cadena de sonidos personalizados y hasta que no se pare o se retrase estén en bucle un ejemplo en js seria
var sound = ["01.MP3","02.MP3"];
este es mi AlarmReceiver.java
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;
import static android.provider.Settings.System.getString;
import static com.crashlytics.android.answers.Answers.TAG;
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int soundResId = R.raw.cat;
Uri soundUri = Uri.parse("android.resource://" + context.getPackageName() + "/" + soundResId);
String message = intent.getStringExtra("todo");
Uri notification = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.cat);
Ringtone r = RingtoneManager.getRingtone(context.getApplicationContext(), notification);
Intent mainIntent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0 /* Request code */, mainIntent,
PendingIntent.FLAG_ONE_SHOT);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int notificationId = intent.getIntExtra("notificationId", 0);
String channelId = "channel-01";
String channelName = "Alarm Notification";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(
channelId, channelName, importance);
notificationManager.createNotificationChannel(mChannel);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
.build();
mChannel.setSound(soundUri, audioAttributes);
}
Bitmap largicon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.drawable.ic_alarm)
.setContentTitle("Ya es hora dormilon!")
.setContentText(message)
.setLargeIcon(largicon)
.setBadgeIconType(R.mipmap.ic_notification_round)
.setCategory(NotificationCompat.CATEGORY_ALARM)
.setWhen(System.currentTimeMillis())
.setPriority(importance)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setOnlyAlertOnce(true);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
//mBuilder.setContentIntent(pendingIntent);
//mBuilder.setSound(soundUri, AudioManager.STREAM_ALARM);
//vibrate
long[] v = {500,1000};
mBuilder.setVibrate(v);
notificationManager.notify(notificationId, mBuilder.build());
}
}
aqui esta el apartado donde se configura la alarma
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.content.Context;
import java.util.Calendar;
public class alarmset extends AppCompatActivity implements View.OnClickListener {
private static final Object NOTIFICATION_NAME = "Alarm Notification";
private ImageView btncmenu;
private int notificationId = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarmset);
// Set Onclick Listener.
findViewById(R.id.setBtn).setOnClickListener(this);
findViewById(R.id.cancelBtn).setOnClickListener(this);
btncmenu = findViewById(R.id.menuclose);
Animation trans = AnimationUtils.loadAnimation(this,R.anim.trans);
btncmenu.startAnimation(trans);
btncmenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent m = new Intent(alarmset.this, menu.class);
startActivity(m);
}
});
}
@Override
public void onClick(View view) {
EditText editText = findViewById(R.id.editText);
TimePicker timePicker = findViewById(R.id.timePicker);
// Set notificationId & text.
Intent intent = new Intent(alarmset.this, AlarmReceiver.class);
intent.putExtra("notificationId", notificationId);
intent.putExtra("todo", editText.getText().toString());
// getBroadcast(context, requestCode, intent, flags)
PendingIntent alarmIntent = PendingIntent.getBroadcast(alarmset.this, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
switch (view.getId()) {
case R.id.setBtn:
int hour = timePicker.getHour();
int minute = timePicker.getMinute();
// Create time.
Calendar startTime = Calendar.getInstance();
startTime.set(Calendar.HOUR_OF_DAY, hour);
startTime.set(Calendar.MINUTE, minute);
startTime.set(Calendar.SECOND, 0);
long alarmStartTime = startTime.getTimeInMillis();
// Set alarm.
// set(type, milliseconds, intent)
alarm.set(AlarmManager.RTC_WAKEUP, alarmStartTime, alarmIntent);
Toast.makeText(this, "Se Guardo la alarma", Toast.LENGTH_LONG).show();
break;
case R.id.cancelBtn:
alarm.cancel(alarmIntent);
Toast.makeText(this, "Se cancelo la alarma.", Toast.LENGTH_LONG).show();
break;
}
}
}
y por último la activity stop_alarm
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="446dp"
android:layout_height="wrap_content"
android:gravity="top"
android:orientation="horizontal"
android:translationY="300dp">
<Button
android:layout_width="229dp"
android:layout_height="wrap_content"
android:text="Parar alarma" />
<Button
android:layout_width="179dp"
android:layout_height="wrap_content"
android:text="Aplazar" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Me gustaría que incluyeran código explicando cómo puedo realizar esto