主要代码如下:
private void initNotify() {
manager = (NotificationManager) getActivity().getSystemService(
Context.NOTIFICATION_SERVICE);
notification = new Notification(R.drawable.ic_launcher, "title",
System.currentTimeMillis());
RemoteViews remoteViews = new RemoteViews(getActivity()
.getPackageName(), R.layout.music_notification);
if (isPlay) {
remoteViews.setImageViewResource(
R.id.paly_pause_music, R.drawable.pause_blue);
} else {
remoteViews.setImageViewResource(
R.id.paly_pause_music, R.drawable.play_blue);
}
Music music = getCurMusic();
if (music != null) {
remoteViews.setTextViewText(R.id.title_title, music.getName());
remoteViews.setTextViewText(R.id.title_album, music.getCategoryName());
}
Intent openIntent = new Intent();
openIntent.setAction(Common.MESSAGE_OPEN_APP);
PendingIntent openpi = PendingIntent.getBroadcast(getActivity(), 0,
openIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.left_section, openpi);
Intent closeIntent = new Intent();
closeIntent.setAction(Common.MESSAGE_CLOSE_APP);
PendingIntent closepi = PendingIntent.getBroadcast(getActivity(), 0,
closeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.close, closepi);
Intent nextIntent = new Intent();
nextIntent.setAction(Common.MESSAGE_MUSIC_NEXT_EVENT);
PendingIntent nextPi = PendingIntent.getBroadcast(getActivity(), 0,
nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.next_music, nextPi);
Intent palyIntent = new Intent();
palyIntent.setAction(Common.MESSAGE_MUSIC_PLAY_PAUSE_EVENT);
PendingIntent palyPi = PendingIntent.getBroadcast(getActivity(), 0,
palyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.paly_pause_music, palyPi);
notification.flags = Notification.FLAG_ONGOING_EVENT;
// notification.defaults = Notification.DEFAULT_SOUND;
notification.sound = null;
notification.contentView = remoteViews;
manager.notify(PandaApplication.MUSIC_NOTIFY_BAR, notification);
}
@SuppressLint("NewApi")
public void openApp(Context context) {
ActivityManager activityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> runningTaskInfos = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (RunningTaskInfo runningTaskInfo : runningTaskInfos) {
if (runningTaskInfo.topActivity.getPackageName().equals(context.getPackageName())) {
if (isForground(getActivity())) {
collapseStatusBar(getActivity());
return;
}
if (Integer.valueOf(Build.VERSION.SDK) >= 11) {
activityManager.moveTaskToFront(runningTaskInfo.id, ActivityManager.MOVE_TASK_WITH_HOME);
TipMsgHelper.ShowLMsg(getActivity(), "MOVE_TASK_WITH_HOME");
} else {
Intent intent = new Intent(getActivity(), runningTaskInfo.topActivity.getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
getActivity().startActivity(intent);
}
return;
}
}
}
private boolean isForground(Context context) {
ActivityManager activityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses = activityManager
.getRunningAppProcesses();
for (RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.processName.equals(context.getPackageName())) {
/*
BACKGROUND=400 EMPTY=500 FOREGROUND=100
GONE=1000 PERCEPTIBLE=130 SERVICE=300 ISIBLE=200
*/
Log.i(context.getPackageName(), "此appimportace ="
+ appProcess.importance
+ ",context.getClass().getName()="
+ context.getClass().getName());
if (appProcess.importance != RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
Log.i(context.getPackageName(), "处于后台"
+ appProcess.processName);
return false;
} else {
Log.i(context.getPackageName(), "处于前台"
+ appProcess.processName);
return true;
}
}
}
return false;
}
public static void collapseStatusBar(Context context) {
try {
Object statusBarManager = context.getSystemService("statusbar");
Method collapse;
if (Build.VERSION.SDK_INT <= 16) {
collapse = statusBarManager.getClass().getMethod("collapse");
} else {
collapse = statusBarManager.getClass().getMethod("collapsePanels");
}
collapse.invoke(statusBarManager);
} catch (Exception localException) {
localException.printStackTrace();
}
}
上述问题,在点击通知栏时,如果应用已经在后台了,切到前台会有延迟,可能是广播延迟的原因。
修改为:
Intent openIntent = new Intent(Intent.ACTION_MAIN);
openIntent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName componentName = getTopActivity(getActivity());
ComponentName cn = null;
if (componentName != null) {
cn = new ComponentName(componentName.getPackageName(), componentName.getClassName());
} else {
cn = new ComponentName(getActivity().getPackageName(), "com.iflytek.hipanda.view.AnimEntrance");
}
openIntent.setComponent(cn);
openIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
PendingIntent openpi = PendingIntent.getActivity(getActivity(), 0,
openIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.left_section, openpi);
private ComponentName getTopActivity(Context context) {
ActivityManager activityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> runningTaskInfos = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (RunningTaskInfo runningTaskInfo : runningTaskInfos) {
if (runningTaskInfo.topActivity.getPackageName().equals(context.getPackageName())) {
return runningTaskInfo.topActivity;
}
}
return null;
}
本文探讨了在Android应用中优化通知栏点击时应用快速切换到前台的问题,通过修改通知栏点击事件处理逻辑,实现了应用从后台快速切换到前台的功能,解决了延迟问题。
2343

被折叠的 条评论
为什么被折叠?



