Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't stop media player after entering app from a notification
    primarykey
    data
    text
    <p>I have an alarm clock app that plays music when the alarm goes off. This is my first Android app.I can display a button and stop the music when I am in the app. However, I have enabled a notification and would like to stop the music when the user clicks on the notification. I return to the application from the notification, but I can not stop the music. The button that I had displayed does not show when I return to the app from the notification unless I hit the back key. If I hit the back button the button shows up. I have reviewed several of the Media Player examples and the notification example, but none seem to have my problem. I understand how to stop the music if I could return to the proper location. Does the notification manager return a flag I could check? I have spent a lot of time on this and can't come up with a good solution. Also, my notification icon is extremely small. How can I enlarge it?</p> <p>Here is my code:</p> <pre><code> public void setAlarmPlaySong() { BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive( Context context, Intent _ ) { stopPlaying(); // Set up for playing the song selected switch(mPos) { case 0: // Play song - mPlayer = MediaPlayer.create(SetAlarmActivity.this, R.raw.calmyourstorm); mPlayer.start(); break; case 1: // Play song - //mPlayer = MediaPlayer.create(SetAlarmActivity.this, R.raw.gowiththeflow); //mPlayer.start(); break; case 2: // Play song - mPlayer = MediaPlayer.create(SetAlarmActivity.this, R.raw.freeyourmind); mPlayer.start(); break; case 3: // Play song - break; case 4: // Play song - break; } // end switch context.unregisterReceiver( this ); // this == BroadcastReceiver, not Activity showStopAlarmButton(); } // end on Receive }; // //Intent intent = new Intent(getApplicationContext(), SetAlarmActivity.class); createNotification(); //Create alarm manager AlarmManager manager = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE )); this.registerReceiver( receiver, new IntentFilter("STOP_MUSIC") ); PendingIntent sender = PendingIntent.getBroadcast( this, 0, new Intent("STOP_MUSIC"), 0 ); //PendingIntent sender = PendingIntent.getBroadcast( this, 0, intent, 0 ); //set the timer as a RTC Wakeup to alarm manager object manager.set( AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(), sender ); } </code></pre> <p>Here is createNotification</p> <pre><code>public void createNotification() { // Create the notification that will display when the app is // closed. Context ctx = getApplicationContext(); // The intent that is triggered if the notification // is trigger Intent notificationIntent = new Intent(ctx, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(ctx, YOUR_PI_REQ_CODE, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); NotificationManager nm = (NotificationManager) ctx .getSystemService(Context.NOTIFICATION_SERVICE); //Resources res = ctx.getResources(); // Build notification Notification.Builder builder = new Notification.Builder(ctx); builder.setContentIntent(contentIntent) .setSmallIcon(R.drawable.beasleybackground2) //.setTicker(res.getString(R.string.your_ticker)) //.setWhen(System.currentTimeMillis()) .setAutoCancel(true) .setContentTitle("Jazz Alarm") .setContentText("Good Morning!"); //.setContentTitle(res.getString(R.string.your_notif_title)) //.setContentText(res.getString(R.string.your_notif_text)); Notification n = builder.build(); // Hide the notification after its selected n.flags |= Notification.FLAG_AUTO_CANCEL; nm.notify(YOUR_NOTIF_ID, n); } // createNotification </code></pre> <p>Here is showStopAlarmButton</p> <pre><code> public void showStopAlarmButton() { final Button alarmOffButton = (Button) findViewById(R.id.btn_alarmOff); alarmOffButton.setVisibility(View.VISIBLE); alarmOffButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //When button is clicked stop the alarm and hide the button //when play is clicked show stop button and hide play button alarmOffButton.setVisibility(View.GONE); // Cancel song stopPlaying(); } }); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload