-
NotificationService Player[1]App/Android 2017. 2. 23. 16:13
리디북스나 멜론,지니뮤직 등에서 흔히 볼 수 있는 알림창 플레이어를 달아 보았다.
background mode에서도 Play 상태를 제어할 수 있는 기능이다.
//MainActivity.class
123456789101112131415161718192021222324252627282930313233343536import android.content.Intent;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.view.Menu;import android.view.MenuItem;import android.view.View;public class MainActivity extends ActionBarActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}public void startService(View v) {Intent serviceIntent = new Intent(MainActivity.this, NotificationService.class);serviceIntent.setAction(Constants.ACTION.STARTFOREGROUND_ACTION);startService(serviceIntent);}}cs //NotificationService.java
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125import android.os.IBinder;import android.app.Service;import android.content.Intent;import android.app.Notification;import android.app.PendingIntent;import android.util.Log;import android.view.View;import android.widget.RemoteViews;import android.widget.Toast;public class NotificationService extends Service {Notification status;private final String LOG_TAG = "NotificationService";@Overridepublic void onDestroy() {super.onDestroy();}@Overridepublic IBinder onBind(Intent intent) {return null;}}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {if (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {showNotification();Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();} else if (intent.getAction().equals(Constants.ACTION.PREV_ACTION)) {Toast.makeText(this, "Clicked Previous", Toast.LENGTH_SHORT).show();Log.i(LOG_TAG, "Clicked Previous");} else if (intent.getAction().equals(Constants.ACTION.PLAY_ACTION)) {Toast.makeText(this, "Clicked Play", Toast.LENGTH_SHORT).show();Log.i(LOG_TAG, "Clicked Play");} else if (intent.getAction().equals(Constants.ACTION.NEXT_ACTION)) {Toast.makeText(this, "Clicked Next", Toast.LENGTH_SHORT).show();Log.i(LOG_TAG, "Clicked Next");} else if (intent.getAction().equals(Constants.ACTION.STOPFOREGROUND_ACTION)) {Log.i(LOG_TAG, "Received Stop Foreground Intent");Toast.makeText(this, "Service Stoped", Toast.LENGTH_SHORT).show();stopForeground(true);stopSelf();}return START_STICKY;}private void showNotification() {// Using RemoteViews to bind custom layouts into NotificationRemoteViews views = new RemoteViews(getPackageName(),R.layout.status_bar);RemoteViews bigViews = new RemoteViews(getPackageName(),R.layout.status_bar_expanded);// showing default album imageviews.setViewVisibility(R.id.status_bar_icon, View.VISIBLE);views.setViewVisibility(R.id.status_bar_album_art, View.GONE);bigViews.setImageViewBitmap(R.id.status_bar_album_art,Constants.getDefaultAlbumArt(this));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, NotificationService.class);previousIntent.setAction(Constants.ACTION.PREV_ACTION);PendingIntent ppreviousIntent = PendingIntent.getService(this, 0,previousIntent, 0);Intent playIntent = new Intent(this, NotificationService.class);playIntent.setAction(Constants.ACTION.PLAY_ACTION);PendingIntent pplayIntent = PendingIntent.getService(this, 0,playIntent, 0);Intent nextIntent = new Intent(this, NotificationService.class);nextIntent.setAction(Constants.ACTION.NEXT_ACTION);PendingIntent pnextIntent = PendingIntent.getService(this, 0,nextIntent, 0);Intent closeIntent = new Intent(this, NotificationService.class);closeIntent.setAction(Constants.ACTION.STOPFOREGROUND_ACTION);PendingIntent pcloseIntent = PendingIntent.getService(this, 0,closeIntent, 0);views.setOnClickPendingIntent(R.id.status_bar_play, pplayIntent);bigViews.setOnClickPendingIntent(R.id.status_bar_play, pplayIntent);views.setOnClickPendingIntent(R.id.status_bar_next, pnextIntent);bigViews.setOnClickPendingIntent(R.id.status_bar_next, pnextIntent);views.setOnClickPendingIntent(R.id.status_bar_prev, ppreviousIntent);bigViews.setOnClickPendingIntent(R.id.status_bar_prev, ppreviousIntent);views.setOnClickPendingIntent(R.id.status_bar_collapse, pcloseIntent);bigViews.setOnClickPendingIntent(R.id.status_bar_collapse, pcloseIntent);views.setImageViewResource(R.id.status_bar_play,R.drawable.apollo_holo_dark_pause);bigViews.setImageViewResource(R.id.status_bar_play,R.drawable.apollo_holo_dark_pause);views.setTextViewText(R.id.status_bar_track_name, "Song Title");bigViews.setTextViewText(R.id.status_bar_track_name, "Song Title");views.setTextViewText(R.id.status_bar_artist_name, "Artist Name");bigViews.setTextViewText(R.id.status_bar_artist_name, "Artist Name");bigViews.setTextViewText(R.id.status_bar_album_name, "Album Name");status = new Notification.Builder(this).build();status.contentView = views;status.bigContentView = bigViews;status.flags = Notification.FLAG_ONGOING_EVENT;status.icon = R.drawable.ic_launcher;status.contentIntent = pendingIntent;startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, status);}cs //Constants.java
123456789101112131415161718192021222324252627282930313233343536import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;public class Constants {public interface ACTION {public static String MAIN_ACTION = "main";public static String INIT_ACTION = "init";public static String PREV_ACTION = "prev";public static aString PLAY_ACTION = "play";public static String NEXT_ACTION = "next";public static String STARTFOREGROUND_ACTION = "startforeground";public static String STOPFOREGROUND_ACTION = "stopforeground";}public interface NOTIFICATION_ID {public static int FOREGROUND_SERVICE = 101;}public static Bitmap getDefaultAlbumArt(Context context) {Bitmap bm = null;BitmapFactory.Options options = new BitmapFactory.Options();try {bm = BitmapFactory.decodeResource(context.getResources(),R.drawable.default_album_art, options);} catch (Error ee) {} catch (Exception e) {}return bm;}}cs 출처 : http://www.tutorialsface.com/2015/08/android-custom-notification-tutorial/
'App > Android' 카테고리의 다른 글
NotificationService Player[2] (0) 2017.02.23