Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

app退到后台不展示 #3

Open
HelenChenVIP opened this issue Oct 30, 2017 · 5 comments
Open

app退到后台不展示 #3

HelenChenVIP opened this issue Oct 30, 2017 · 5 comments

Comments

@HelenChenVIP
Copy link

你好,我想请问下,这个Demo在程序按Home键回退到主界面的时候headsup就不再展示了,请问这个问题应该怎么解决呢?

@HelenChenVIP
Copy link
Author

希望得到您的回复,谢谢

@WuGang128
Copy link

可以显示的 你app 是不是已经关了 还有就是要放在ui 线程

@HelenChenVIP
Copy link
Author

` Handler handler = new Handler() {
@OverRide
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case ShowMsg:
if (BaseApplication.getJPStat().equals("Forground")) {
//程序在前台运行展示折叠收到的信息
showMsgF();
LogUtils.i("JP>>>>>>" + "Forground");
} else {
//程序在后台运行展示展开收到的信息
showMsgB();
LogUtils.i("JP>>>>>>" + "Background");
}
break;
}

private void showMsgB() {
    try {
        //获取NotificationManager实例
        Intent intent = new Intent(UIUtils.getContext(), DesActivity.class);
        intent.putExtra("key", "warning");
        PendingIntent contentIntent = PendingIntent.getActivity(UIUtils.getContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        SharedPreferences preferences = context.getSharedPreferences("MSG_HINT", Context.MODE_PRIVATE);
        boolean SOUND = false;
        boolean VIBRATE = false;
        boolean LIGHTS = true;
        if (preferences != null) {
            SOUND = preferences.getBoolean("SOUND", false);
            VIBRATE = preferences.getBoolean("VIBRATE", false);
            LIGHTS = preferences.getBoolean("LIGHTS", true);
        }
        int Defaults = Notification.DEFAULT_LIGHTS;
        if (VIBRATE && LIGHTS)
            Defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE;
        else if (!VIBRATE && LIGHTS)
            Defaults = Notification.DEFAULT_LIGHTS;
        else if (VIBRATE && !LIGHTS)
            Defaults = Notification.DEFAULT_VIBRATE;
        else
            Defaults = -111;
        final HeadsUpManager manage1 = HeadsUpManager.getInstant(UIUtils.getContext());
        LayoutInflater inflater = (LayoutInflater) UIUtils.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.alarm_receiver_headerup, null);
        TextView tv_alarm_content = (TextView) view.findViewById(R.id.tv_alarm_content);
        TextView tv_alarm_next = (TextView) view.findViewById(R.id.tv_alarm_next);
        AppSettingsBean spAppSettingsBean = (AppSettingsBean) SPUtil.getBean(UIUtils.getContext(), "AppSettings");
        String appName=spAppSettingsBean.getAppName();
        HeadsUp headsUp1 = new HeadsUp.Builder(UIUtils.getContext())
                .setContentTitle(appName)
                //要显示通知栏通知,这个一定要设置
                .setSmallIcon(R.drawable.desk_logo1)
                //2.3 一定要设置这个参数,负责会报错
                .setContentIntent(contentIntent)
                .setContentText(getShowmsg())// 展示形式
                .setAutoCancel(true)
                .setStyle(new NotificationCompat.BigTextStyle())
                .setWhen(System.currentTimeMillis())
                .buildHeadUp();
        tv_alarm_content.setText(getShowmsg());
        tv_alarm_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                UIUtils.getContext().startActivity(intent);
                manage1.cancel();
            }
        });
        headsUp1.setCustomView(view);
        if (Defaults != -111)
            builder.setDefaults(Defaults);
        Notification notify = builder.build();
        notify.ledARGB = Color.GREEN;
        notify.ledOnMS = 1000;
        notify.ledOffMS = 1000;
        if (SOUND)
            playSound();
        manage1.notify(notifyId++, headsUp1);
    } catch (Exception e) {
        LogUtils.e(e.getMessage());
    }
}

private void showMsgF() {
try {
//获取NotificationManager实例
if (mNotificationManager == null)
mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(notifyId);
// Intent intent2 = new Intent(context, AlarmJPushActivity.class);
//自定义通知栏-先设定RemoteViews
//设置对应IMAGEVIEW的ID的资源图片
// PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, LoginActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
Intent intent = new Intent(UIUtils.getContext(), DesActivity.class);
intent.putExtra("key", "warning");
// UIUtils.getContext().startActivity(intent);
PendingIntent contentIntent = PendingIntent.getActivity(UIUtils.getContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.desk_logo1);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context);
SharedPreferences preferences = context.getSharedPreferences("MSG_HINT", Context.MODE_PRIVATE);
boolean SOUND = false;
boolean VIBRATE = false;
boolean LIGHTS = true;
if (preferences != null) {
SOUND = preferences.getBoolean("SOUND", false);
VIBRATE = preferences.getBoolean("VIBRATE", false);
LIGHTS = preferences.getBoolean("LIGHTS", true);
}

        int Defaults = Notification.DEFAULT_LIGHTS;
        if (VIBRATE && LIGHTS)
            Defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE;
        else if (!VIBRATE && LIGHTS)
            Defaults = Notification.DEFAULT_LIGHTS;
        else if (VIBRATE && !LIGHTS)
            Defaults = Notification.DEFAULT_VIBRATE;
        else
            Defaults = -111;
        AppSettingsBean spAppSettingsBean = (AppSettingsBean) SPUtil.getBean(UIUtils.getContext(), "AppSettings");
        String appName=spAppSettingsBean.getAppName();
        builder.setTicker("您收到新的监测报警通知")
                .setOngoing(false)
                .setLargeIcon(bitmap)
                .setSmallIcon(R.drawable.desk_logo1)
                .setPriority(Notification.PRIORITY_DEFAULT)
                .setAutoCancel(true)
                .setStyle(new NotificationCompat.BigTextStyle())
                .setWhen(System.currentTimeMillis())
                .setContentTitle(appName)
                .setContentText(getShowmsg())// 展示形式
                .setContentIntent(contentIntent);
        if (Defaults != -111)
            builder.setDefaults(Defaults);
        Notification notify = builder.build();
        notify.ledARGB = Color.GREEN;
        notify.ledOnMS = 1000;
        notify.ledOffMS = 1000;
        if (SOUND)
            playSound();
        mNotificationManager.notify(notifyId, notify);
    } catch (Exception e) {
        LogUtils.e(e.getMessage());
    }
}

`

我已经判断出当我按下Home键的时候程序没有退出,只不过是从前台变为后台运行。但是,问题是:当程序退到后台时候,headsup就不展示了,只是通知栏上有消息推送的展示。
请大家有知道的给予点思路,非常感谢!

@WuGang128
Copy link

你把代码放在 ui线程试试

@l1x2d3
Copy link

l1x2d3 commented Jan 31, 2018

你好,这个悬浮窗效果在android 5.0 的手机上,一旦退出前台就无法展示了,而在6.0上如果设置了 .setFullScreenIntent(pendingIntent, false) 这个属性,不管是true,还是false,都没有通知,而是直接就跳转了,pendingIntent 设置的界面了,但是如果不设置就只是普通的通知效果而已。想问下,怎么解决
(测试手机,华为5.0,vivo 6.00)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants