初学《 NotificationBar》

来源:互联网 发布:淘宝商品名称字数限制 编辑:程序博客网 时间:2024/06/05 09:42

NotificationBar的实现代码参考,上代码:

public class MusicService extends Service implements MediaPlayer.OnCompletionListener {    private static final boolean D = true;    private static final String TAG = MusicService.class.getSimpleName();    private static final int HANDLER_MUSIC_PLAY = 1;    private MediaPlayer mMediaPlayer;    private MusicHandler mMusicHandler;    public MusicService() {    }    @Override    public void onCreate() {        super.onCreate();        mMediaPlayer = new MediaPlayer();        mMediaPlayer.setOnCompletionListener(this);        HandlerThread thread = new HandlerThread("music",Thread.NORM_PRIORITY);        thread.start();        Looper looper = thread.getLooper();        mMusicHandler = new MusicHandler(looper);    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        if (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {            Log.i(TAG, "Received Start Foreground Intent ");            Intent notificationIntent = new Intent(this, MainActivity.class);            notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK                    | Intent.FLAG_ACTIVITY_CLEAR_TASK);            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,                    notificationIntent, 0);            Intent previousIntent = new Intent(this, MusicService.class);            previousIntent.setAction(Constants.ACTION.STOP_ACTION);            PendingIntent ppreviousIntent = PendingIntent.getService(this, 0,                    previousIntent, 0);            Intent playIntent = new Intent(this, MusicService.class);            playIntent.setAction(Constants.ACTION.PLAY_ACTION);            PendingIntent pplayIntent = PendingIntent.getService(this, 0,                    playIntent, 0);            Intent nextIntent = new Intent(this, MusicService.class);            nextIntent.setAction(Constants.ACTION.PAUSE_ACTION);            PendingIntent pnextIntent = PendingIntent.getService(this, 0,                    nextIntent, 0);            Bitmap icon = BitmapFactory.decodeResource(getResources(),                    R.drawable.rating_favorite);            Notification notification = new NotificationCompat.Builder(this)                    .setContentTitle("Truiton Music Player")                    .setTicker("Truiton Music Player")                    .setContentText("My Music")                    .setSmallIcon(R.drawable.rating_favorite)                    .setLargeIcon(                            Bitmap.createScaledBitmap(icon, 128, 128, false))                    .setContentIntent(pendingIntent)                    .setOngoing(true)                    .addAction(android.R.drawable.ic_media_previous,                            "Stop", ppreviousIntent)                    .addAction(android.R.drawable.ic_media_play, "Play",                            pplayIntent)                    .addAction(android.R.drawable.ic_media_next, "Pause",                            pnextIntent).build();            startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,                    notification);        } else if (intent.getAction().equals(Constants.ACTION.STOP_ACTION)) {            Log.i(TAG, "Clicked Previous");            if(mMediaPlayer.isPlaying()) {                mMediaPlayer.stop();                mMediaPlayer.release();            }        } else if (intent.getAction().equals(Constants.ACTION.PLAY_ACTION)) {            Log.i(TAG, "Clicked Play");            if(!mMediaPlayer.isPlaying())                mMusicHandler.sendEmptyMessageDelayed(HANDLER_MUSIC_PLAY,3*1000);        } else if (intent.getAction().equals(Constants.ACTION.PAUSE_ACTION)) {            Log.i(TAG, "Clicked Next");            if(mMediaPlayer.isPlaying())                mMediaPlayer.pause();            else                mMediaPlayer.start();        } else if (intent.getAction().equals(                Constants.ACTION.STOPFOREGROUND_ACTION)) {            Log.i(TAG, "Received Stop Foreground Intent");            if(mMediaPlayer.isPlaying()) {                mMediaPlayer.release();            }            stopForeground(true);            stopSelf();        }        return START_STICKY;    }    @Override    public IBinder onBind(Intent intent) {        // TODO: Return the communication channel to the service.        return null;    }    @Override    public void onCompletion(MediaPlayer mp) {        mMediaPlayer.stop();    }    private class MusicHandler extends Handler {        public MusicHandler(Looper looper) {            super(looper);        }        @Override        public void handleMessage(Message msg) {            switch (msg.what) {                case HANDLER_MUSIC_PLAY:                    if(D) Log.d(TAG,"HANDLER_MUSIC_PLAY---");                    mMediaPlayer.reset();                    try {                        AssetFileDescriptor fileDescriptor = getAssets().openFd                                ("millions_of_sadness.mp3");                        mMediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(),                                fileDescriptor.getStartOffset(), fileDescriptor.getLength());                        mMediaPlayer.prepare();                        mMediaPlayer.start();                    } catch (IOException e) {                        e.printStackTrace();                    }                    break;                default:                    super.handleMessage(msg);                    break;            }        }    }}
0 0
原创粉丝点击