初学《 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
- 初学《 NotificationBar》
- 关于界面控件的学习3【spinner、autocompletetextview、NotificationBar】
- 初学...
- 初学
- 初学
- 初学
- 初学
- 初学
- 初学
- 初学
- 如何隐藏Android4.0及以上版本的ActionBar、NotificationBar、StatusBar、SystemBar、TitleBar
- 如何隐藏Android4.0及以上版本的ActionBar、NotificationBar、StatusBar、SystemBar、TitleBar .
- 如何隐藏Android4.0及以上版本的ActionBar、NotificationBar、StatusBar、SystemBar、TitleBar
- 如何隐藏Android4.0及以上版本的ActionBar、NotificationBar、StatusBar、SystemBar、TitleBar
- 初学数据仓库
- 初学Struts
- 初学Struts
- 初学触发器
- yii2 save方法只保存置顶列的参数
- 标识识别的注意点、dll缺失文件下载网站
- activity更新另一个activityUI方法(一)
- service起步操作
- 线性布局下最左/右问题
- 初学《 NotificationBar》
- win7下 Windows Process Activation Service 服务导致的 iis无法启动的解决方法
- Linux内核Socket CAN中文文档
- 今天刚刚认真区别了一下两个文件读写方式
- UFLDL Exercise: Softmax Regression
- 利用Freemarker生成doc文件(包含list循环,ifelse判断,合并单元格,嵌入表格单元格字数过多报错等)
- Asynchronous Programming with async and await (C#)用async和await实现异步编程
- 在Spring3中,配置DataSource的方法有6种
- [java]PropertiesUtils操作属性文件