0

我正在编写我的第一个 Android 应用程序,一个音板/铃声应用程序,但我的 Toast 通知没有显示有问题。我有一个调用 DownloadManager 的按钮,从 Internet 下载 mp3,将其保存到 sdcard,并将其分配为铃声。这一切都很好,但我知道我必须在 DownloadManager 完成后取消注册接收器。一旦 DownloadManager.STATUS_SUCCESSFUL 出现,我想要一个 Toast 弹出说“铃声已设置”,如果我注释掉我的“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);



}


} 
4

2 回答 2

0
onStop();
{

    unregisterReceiver(receiver);
    super.onStop();
}

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

/** 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() 并取消注册接收器。我相信这只是语法错误。

于 2012-08-23T02:32:11.650 回答
0
@Override
public void onStop();
{
   unregisterReceiver(mReceiver);
   mReceiver = null;
   super.onStop();
}

绝对是正确的。我只需要弄清楚在我的代码中放置它的位置。它现在有效!谢谢你!

于 2012-08-23T19:14:26.030 回答