Back to project page LyricHere.
The source code is released under:
Apache License
If you think the Android project LyricHere listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package cn.zhaiyifan.lyrichere; //from w w w. java2 s . co m import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.support.v4.app.NotificationCompat; import android.text.TextUtils; import cn.zhaiyifan.lyrichere.ui.LyricExplorerActivity; import cn.zhaiyifan.lyrichere.utils.Util; /** * Created by yifan on 6/21/14. */ public class MusicBroadcastReceiver extends BroadcastReceiver { private static final String TAG = MusicBroadcastReceiver.class.getSimpleName(); private static final int NOTIFICATION_ID = 5657; private String mTitle = null; private String mArtist = null; private String mAlbum = null; @Override public void onReceive(Context context, Intent intent) { Util.log(TAG, "Action : " + intent.getAction()); String artist = intent.getStringExtra("artist"); String title = intent.getStringExtra("track"); String album = intent.getStringExtra("album"); // at least we should have a track title if (title == null) { return; } boolean isPlaying = intent.getBooleanExtra("isplaying", true); isPlaying = intent.getBooleanExtra("playing", isPlaying); isPlaying = intent.getBooleanExtra("spotifyPlaying", isPlaying); Util.log(TAG, String.valueOf("title: " + title)); Util.log(TAG, String.valueOf("isPlaying: " + isPlaying)); if (isPlaying && !isSameTrack(title, artist, album)) { addNotification(context); } else { removeNotification(context); } } public void addNotification(Context context) { NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationCompat.Builder mNotifyBuilder; Intent intent = new Intent(context, LyricExplorerActivity.class); intent.putExtra(Constants.Column.TITLE, mTitle); intent.putExtra(Constants.Column.ARTIST, mArtist); intent.putExtra(Constants.Column.ALBUM, mAlbum); mNotifyBuilder = new NotificationCompat.Builder(context) .setContentTitle(context.getString(R.string.notification_title)) .setContentText(makeNotificationText()) .setTicker(context.getString(R.string.notification_ticker)) .setContentIntent(PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT)) .setSmallIcon(R.drawable.play); notificationManager.notify(NOTIFICATION_ID, mNotifyBuilder.build()); } public void removeNotification(Context paramContext) { ((NotificationManager) paramContext.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(NOTIFICATION_ID); } private String makeNotificationText() { String text = null; if (!TextUtils.isEmpty(mTitle)) { if (TextUtils.isEmpty(mArtist)) { text = mTitle; } else { text = String.format("%s - %s", mArtist, mTitle); } } return text; } /** * Check if new received meta is the same track as before */ private synchronized boolean isSameTrack(String title, String artist, String album) { boolean isSame = true; if (title != null) { // mTitle is null or different from title, then different track if (!TextUtils.equals(title, mTitle) || !TextUtils.equals(artist, mArtist) || !TextUtils.equals(album, mAlbum)) { isSame = false; } else { isSame = true; } } if (!isSame) { mTitle = title; mArtist = artist; mAlbum = album; } Util.log(TAG, "isSameTrack: " + String.valueOf(isSame)); return isSame; } }