public class NotificationExtend {
private Activity context;
private final String TAG = "NotificationExtend";
public boolean flag = false;
RemoteViews notificationView;
Notification notification;
public NotificationExtend() {
}
public void setActivity(Activity context) {
this.context = context;
}
public void resetNotification(String msg) {
cancelNotification();
showNotification_rl(msg);
}
public void showNotification(String msg) {
if (!isAppOnBackground()) {
Logger.e("backgroudn");
} else {
showNotification_rl(msg);
}
}
public void togglePausePlayButton(boolean playing) {
if (playing)
notificationView.setImageViewResource(R.id.notification_pause,
R.drawable.mediacontroller_pause);
else
notificationView.setImageViewResource(R.id.notification_pause,
R.drawable.mediacontroller_play);
}
@SuppressWarnings("deprecation")
public void showNotification_rl(String msg) {
Logger.e("showNotification_rl " + msg);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
notification = new Notification(R.drawable.ic_logo, null,
System.currentTimeMillis());
notificationView = new RemoteViews(context.getPackageName(),
R.layout.notification_item);
notificationView.setTextViewText(R.id.notification_msg, msg);
Intent notificationIntent = new Intent(context, context.getClass());
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
context, 0, notificationIntent, 0);
notification.contentView = notificationView;
notification.contentIntent = pendingNotificationIntent;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent switchIntent = new Intent(context, PauseButtonListener.class);
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(context,
0, switchIntent, 0);
notificationView.setOnClickPendingIntent(R.id.notification_pause,
pendingSwitchIntent);
notificationManager.notify(TAG, 5, notification);
flag = true;
}
public void cancelNotification() {
if (flag) {
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
notificationManager.cancel(TAG, 5);
flag = false;
}
}
public boolean isAppOnBackground() {
ActivityManager activityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
// Returns a list of application processes that are running on the
// device
List<RunningAppProcessInfo> appProcesses = activityManager
.getRunningAppProcesses();
String packageName = context.getPackageName();
if (appProcesses == null || packageName == null)
return false;
for (RunningAppProcessInfo appProcess : appProcesses) {
// The name of the process that this object is associated with.
if (appProcess.processName.equals(packageName)
&& ((appProcess.importance == RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE))) {
return true;
}
}
return false;
}
}
public class PauseButtonListener extends BroadcastReceiver {
public PauseButtonListener(){
}
@Override
public void onReceive(Context context, Intent intent) {
Logger.e("test");
}
}
public class NotificationExtend { private Activity context; private final String TAG = "NotificationExtend"; public boolean flag = false; RemoteViews notificationView; Notification notification;