创建通知的MediaPlayer与Android的前景服务 [英] Creating a notification for MediaPlayer with an Android Foreground Service

查看:266
本文介绍了创建通知的MediaPlayer与Android的前景服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是问题所在。

我目前工作的一个应用程序,必须提供:

I am currently working on an application that must provide :

A 电台播放器(AAC流媒体直播从URL) 和一个播客播放器(从URL MP3流媒体)

A Radio Player (AAC live streaming from an url) And a PodCast player (MP3 Streaming from an url)

应用程序必须能够为在后台运行(Android的服务),并接触到了持续性的通知的通知栏(Android的前景服务)用户

The application must be able to run in background (Android Service) and be exposed to the user with a persistant notification in the Notification Bar (Android Foreground Service)

(每个问题的问题,所以在这里,我会问的通知)

由于我有几个班级管理的球员,我虽然创建一个泛型类的noticication将是一个不错的主意。这是什么样的观点我想创建:

Since i have several class for managing players, i though creating a generic class for the noticication would be a good idea. Here is the kind of view i would like to create :

.

下面是我的通知类现在:

Here is my notification class for now :

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自定义通知?

推荐答案

问计权威人士是有点难度在这种情况下,因为大多数音乐播放器是闭源的,几乎没有人用那种扩展控制 - 中内式视图通知。

Asking for an authoritative source is a bit difficult in this case, since most music players are closed-source and virtually no one else uses that sort of expanded controls-in-the-view notification.

从外观风格来看,我会委托给NotificationCompat,而不是扩展它。这样的话,你可以在内部present一个简单的API,而不是暴露了整个NotificationCompat的。

From a stylistic point of view, I would delegate to NotificationCompat instead of extend it. That way, you can present a simpler API internally as opposed to exposing the whole of NotificationCompat.

至于RemoteViews,我不知道你问究竟。但是你这样做,只要你保持通知情况下,你可以保持实例RemoteViews(或个人意见),并根据需要更新它们。如果你去的代表团,而不是继承,那将是相当多的清洁剂,因为它是有意义的这一领域的通知,这个领域的意见还挺方式。

As for RemoteViews, I'm not sure what you're asking exactly. However you do it, as long as you keep the Notification instance, you can keep the instance to RemoteViews (or the individual views) and update them as needed. If you go with delegation instead of inheritance, it would be quite a bit cleaner as it makes sense in a "this field is the notification, this field is its Views" kinda way.

P.S。从一个纯粹的语法点,尝试使用该框架命名指南。最值得注意的是,字段是prefixed与'm'和使用驼峰(例如,mNotifTitle)。最后,私人领域都具有良好的测试氪石。

P.S. From a purely syntactical point of view, try to use the framework naming guidelines. Most notably, fields are prefixed with 'm' and use camelCase (e.g., mNotifTitle). Lastly, private fields are the kryptonite of good tests.

这篇关于创建通知的MediaPlayer与Android的前景服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆