Android 取消注册的接收器会杀死我的吐司弹出窗口

Android 取消注册的接收器会杀死我的吐司弹出窗口,android,broadcastreceiver,toast,Android,Broadcastreceiver,Toast,我正在编写我的第一个Android应用程序,一个音板/铃声应用程序,我有一个问题,我的Toast通知没有出现。我有一个按钮,可以调用DownloadManager,从internet下载mp3,将其保存到SD卡,并将其指定为铃声。这一切都很好,但我知道我必须在DownloadManager完成后注销Receiver。我希望在下载Manager.STATUS_SUCCESSFUL成功后,弹出一个祝酒词,说“铃声已设置”,如果我评论掉我的“onStop/unregisterReceiver”块,它确

我正在编写我的第一个Android应用程序,一个音板/铃声应用程序,我有一个问题,我的Toast通知没有出现。我有一个按钮,可以调用DownloadManager,从internet下载mp3,将其保存到SD卡,并将其指定为铃声。这一切都很好,但我知道我必须在DownloadManager完成后注销Receiver。我希望在下载Manager.STATUS_SUCCESSFUL成功后,弹出一个祝酒词,说“铃声已设置”,如果我评论掉我的“onStop/unregisterReceiver”块,它确实有效。重新激活该块后,将不显示Toast。我感谢任何帮助,而且,我对android编码或任何编程都是非常陌生的。谢谢

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);



}


} 
上面的问题代码看起来很奇怪。 正确的应该是

/** Receiver for download completion */
BroadcastReceiver mReceiver;

/** 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);

    mReceiver = 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);
}

@Override
public void onStop();
{
   unregisterReceiver(mReceiver);
   mReceiver = null;
   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();
}
因此,使用当前的代码,只需在onCreate()方法中调用onStop()并注销receiver。我相信这只是语法错误

@Override
public void onStop();
{
   unregisterReceiver(mReceiver);
   mReceiver = null;
   super.onStop();
}

这绝对正确。我只是想知道把它放在我的代码里。现在可以用了!!谢谢大家!

谢谢你的回复!正如您所展示的,我在onCreate方法之后插入了它,Eclipse抱怨“receiver”无法解析为变量。再次感谢您的帮助。不幸的是,这并没有解决我的问题。Eclipse抱怨“receiver”无法解析为变量。ok。提供了更精确的代码片段。这只是小事。为了更好地理解,我真的建议不要从Android开始。
@Override
public void onStop();
{
   unregisterReceiver(mReceiver);
   mReceiver = null;
   super.onStop();
}