安卓:从TextView中添加文本状态栏 [英] Android: Adding text from textView to status bar

查看:241
本文介绍了安卓:从TextView中添加文本状态栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能从添加到TextView的状态栏采取的字符串。我想说明的文字不断,动态更新的TextView从应用程序的主要活动。

  TextView的消息=(的TextView)findViewById(R.id.textView1);
message.setText(这个我要添加到状态栏);


解决方案

是的,你可以

 私有静态无效generateNotification(上下文的背景下,字符串消息){
        INT图标= R.drawable.ic_status;
        时长= System.currentTimeMillis的();
        NotificationManager notificationManager =(NotificationManager)上下文
                .getSystemService(Context.NOTIFICATION_SERVICE);
        通知通知=新的通知(图标,消息,时);
        字符串title = context.getString(R.string.app_name); //在这里你可以通过你的TextView的价值
        意图notificationIntent =新意图(背景下,SplashScreen.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        的PendingIntent意图= PendingIntent.getActivity(上下文,0,
                notificationIntent,0);
        notification.setLatestEventInfo(上下文,标题,邮件,意图);
        notification.flags | = Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0,通知);
    }

Is it possible to add the String taken from textView to Status Bar. I want to show text constantly , dynamically updated as textView from apps main activity.

TextView message = (TextView) findViewById(R.id.textView1);
message.setText("This I want to add to status bar");

解决方案

Yes, you can

private static void generateNotification(Context context, String message) {
        int icon = R.drawable.ic_status;
        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);
        String title = context.getString(R.string.app_name); // Here you can pass the value of your TextView
        Intent notificationIntent = new Intent(context, SplashScreen.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(context, 0,
                notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0, notification);
    }

这篇关于安卓:从TextView中添加文本状态栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆