I’m coding my very first Android app, a soundboard/ringtone app, and I’m having a problem with my Toast notification not showing up. I have a button that calls DownloadManager, downloads an mp3 from the internet, saves it to the sdcard, and assigns it as a ringtone. This all works fine, but I know I must unregisterReceiver after DownloadManager finishes. I want a Toast to pop up saying “Ringtone Has Been Set” once DownloadManager.STATUS_SUCCESSFUL comes thru, and it DOES work if I comment out my “onStop / unregisterReceiver” block. Once I reactivate that block, Toast does not display. I appreciate any help, and again, I am EXTREMELY new to android coding, or any programming at all. Thanks!
package com.gameringers.ffringers;
import java.io.File;
import android.app.Activity;
import android.app.DownloadManager;
import android.app.DownloadManager.Query;
import android.app.DownloadManager.Request;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Toast;
public class DownloadActivity extends Activity {
private long enqueue;
private DownloadManager dm;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String songfile = intent.getStringExtra(FFVI.SONG_FILE);
String songtitle = intent.getStringExtra(FFVI.SONG_TITLE);
setContentView(R.layout.activity_download);
BroadcastReceiver receiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
Query query = new Query();
Cursor c = dm.query(query);
if (c.moveToFirst()) {
int columnIndex = c
.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c
.getInt(columnIndex)) {
Toast.makeText(getBaseContext(), "Ringtone Has Been Set!",
Toast.LENGTH_LONG).show();
finish();
}
}
}
// }
};
registerReceiver(receiver, new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE));
String path=(Environment.getExternalStorageDirectory()+"/gameringers");
// String filename="Test11"+".mp3";
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(path+songfile)));
File k = new File(path, songfile);
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, (songtitle));
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, "N/A");
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
values.put(MediaStore.Audio.Media.IS_ALARM, false);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
//Insert it into the database
Uri newUri= this.getContentResolver().insert(MediaStore.Audio.Media.getContentUriForPath(k.getAbsolutePath()), values);
RingtoneManager.setActualDefaultRingtoneUri(
this,
RingtoneManager.TYPE_RINGTONE,
newUri);
onStop();
{
unregisterReceiver(receiver);
super.onStop();
}
}
public void setringtone(View v) {
Intent intent = getIntent();
String songfile = intent.getStringExtra(FFVI.SONG_FILE);
String songtitle = intent.getStringExtra(FFVI.SONG_TITLE);
Toast.makeText(getBaseContext(), songfile+songtitle,
Toast.LENGTH_LONG).show();
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(
Uri.parse("http://www.gameringers.com/ringers/ff/"+songfile));
request.setDestinationInExternalPublicDir("/gameringers",songfile);
enqueue = dm.enqueue(request);
}
}
Above code from the question looks weird.
The right one should be
So with the code You have currently You just call onStop() and unregister receiver right in Yours onCreate() method. I believe it’s just syntax error.