1

Which is better? To instantiate a Broadcast receiver inside an Activity or in Service class, or make a class that extends BroadcastReceiver?

Below is an example where I instantiate BroadcastReceiver inside a Service class.

   public BroadcastReceiver receiver = new BroadcastReceiver() {
    private String filename;

    @Override
    public void onReceive(Context context, Intent intent){
        String action = intent.getAction();
        Bundle extras = intent.getExtras();
        filename = extras.getString("AudioPath");
    Toast.makeText(AudioService.this, "the audio file name sent: " + filename , Toast.LENGTH_LONG).show();
    if(action.equals("com.porno.xxx.AudioPlay")){
            selectedAudioPath = audiopath;
            String state = intent.getExtras().getString("stringdata");
            playSong();
            Toast.makeText(AudioService.this, "play audio from service string data "+ state, Toast.LENGTH_LONG).show();

        }
        else if(action.equals("com.porno.xxx.AudioPause")){
            pauseSong();

            selectedAudioPath = audiopath;
            Toast.makeText(AudioService.this, "pause audio from service", Toast.LENGTH_LONG).show();
        }
        else if(action.equals("com.porno.xxx.AudioSelector")){
            Toast.makeText(AudioService.this, "music selector from service", Toast.LENGTH_LONG).show();
            Intent i = new Intent();
            audiopath = intent.getStringExtra("filename");
            Toast.makeText(AudioService.this, "selelcted audio path: " + audiopath, Toast.LENGTH_LONG).show();
        }
        else if(action.equals("com.porno.xxx.AudioRelease")){
            Toast.makeText(AudioService.this, "My Service Stopped and destoryed", Toast.LENGTH_LONG).show();
            player.stop();
            if (player != null) player.release();

                }   

        }

};
4

2 回答 2

1

我在打字时提交并接受了一个答案,所以这就是我所在的位置,很高兴您已经找到了答案!:)

根据您(明显)作为媒体播放器的用途,我建议将播放器实现为服务(如果用户离开,则可以继续在后台运行)或活动(如果不需要此功能或适合您的应用程序..)

a 的典型实现BroadcastReceiver是作为应用程序的独立组件,在 Manifest 中声明,即使应用程序已被终止,它也允许它接收广播;它将开始接收广播,然后在处理后停止。

BroadcastReceiver相反,正如您所做的那样,将其创建为内部类可能是有利的。当您只处理自己的操作时,这非常棒,因为它很容易启动和停止接收器。

在您ServiceonCreate()方法中,您可以创建操作过滤器,然后可以根据应用程序的状态轻松启用或禁用它:

/* service */

public static final ACTION_PAUSE = "com.example.action_pause";

@Override
public void onCreate() {
    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_SCREEN_ON); /* Android action example */
    filter.addAction(MyClass.ACTION_PAUSE); /* Custom action example*/
    registerReceiver(mIntentReceiver, filter);
}

private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.v(TAG, "mIntentReceiver.onReceive() action:"+ intent.getAction() );
        handleCommand(intent);
    } // end onReceive
}; /* end BroadcastReceiver */
于 2012-12-11T07:06:13.230 回答
1

首先,您计划要使用广播接收器做什么。然后你分析最佳可行的解决方案。如果您想在活动中注册和取消注册广播接收器,那么您的源代码就可以了。

例如,如果您想在应用程序未执行时调用广播接收器。(这意味着退出应用程序而不是强制关闭)。那么你不应该在编码中注册和注销广播接收器。为此,您创建/实现了一个从 BroadcastReceiver 扩展的单独类。在清单文件中,您要添加广播接收器。

于 2012-12-11T06:09:45.057 回答