使用Android前台服务为MediaPlayer创建通知

时间:2013-07-08 14:29:41

标签: java android media-player android-service android-notifications

这是问题所在。

我目前正在开发一个必须提供的应用程序:

广播播放器(来自网址的AAC直播) 还有一个 PodCast播放器(来自网址的MP3流媒体)

应用程序必须能够在后台运行(Android服务),并在通知栏(Android前台服务)中通过持续通知向用户公开< / p>

(每个问题一个问题,所以我会要求通知)

由于我有几个用于管理玩家的课程,我虽然为通知创建一个通用类是个好主意。以下是我想要创建的视图:

enter image description here

以下是我的通知课程:

public class StreamingNotification extends NotificationCompat {
    /**
     * PRIVATE ATTRIBUTES
     */
    // log
    private static final String         TAG             = StreamingNotification.class.getSimpleName();
    // notification
    private NotificationManager         _notificationManager;
    private NotificationCompat.Builder  _builder        = null;
    private Notification                _notification;
    // data
    public static final int             NOTIFICATION_ID = 1;
    private Class                       _notifActivity;
    private Context                     _context;
    private String                      _notifTitle;
    private String                      _notifText;
    private int                         _notifLayout;

    public StreamingNotification(String _notifActivity, Context _context, String _notifTitle, String _notifText, int _notifLayout) {

        super();
        try {
            this._notifActivity = Class.forName(_notifActivity);
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        this._context = _context;
        this._notifTitle = _notifTitle;
        this._notifText = _notifText;
        this._notifLayout = _notifLayout;
        // manager
        _notificationManager = (NotificationManager)_context.getSystemService(Context.NOTIFICATION_SERVICE);
        // notif builder
        _builder = new NotificationCompat.Builder(_context);
        buildSimpleNotification();
    }

    private void buildSimpleNotification() {

        // notif intent
        final Intent notificationIntent = new Intent(_context, _notifActivity);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        // remote view
        RemoteViews contentView = new RemoteViews(_context.getPackageName(), _notifLayout);
        // pending intent
        final PendingIntent contentIntent = PendingIntent.getActivity(_context, NOTIFICATION_ID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        _builder.setContentIntent(contentIntent).setContent(contentView).setOngoing(true).setWhen(System.currentTimeMillis()).setAutoCancel(false).setContentTitle(_notifTitle)
            .setContentText(_notifText);
        // notification build
        _notification = _builder.getNotification();
        _notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE | Notification.FLAG_NO_CLEAR;
        _notificationManager.notify(NOTIFICATION_ID, _notification);
    }
    [GETTERS AND SETTERS]
}

我做得对吗?您将如何管理RemoteViews自定义通知?

1 个答案:

答案 0 :(得分:5)

在这种情况下要求权威来源有点困难,因为大多数音乐播放器都是闭源的,几乎没有其他人使用这种扩展的控制视图通知。

从风格的角度来看,我会委托NotificationCompat而不是扩展它。这样,您可以在内部呈现更简单的API,而不是暴露整个NotificationCompat。

至于RemoteViews,我不确定你究竟在问什么。但是,只要保留Notification实例,就可以将实例保留到RemoteViews(或单个视图)并根据需要更新它们。如果你使用委托而不是继承,它会更加清晰,因为它在“这个字段是通知,这个字段是它的视图”有点意义。

P.S。从纯粹的语法角度来看,尝试使用框架命名准则。最值得注意的是,字段以'm'为前缀并使用camelCase(例如,mNotifTitle)。最后,私人领域是良好测试的氪星石。

相关问题