如题:要的效果就是像QQ那样,当程序在后台的时候,程序图标就出现在标题栏,同时还能接收消息

解决方案 »

  1.   

    google一下 “android service的使用”就是一个服务程序,service没有activity界面(在后台),可以只用来监听和数据处理。
      

  2.   

    接收消息是可以用service,我想知道怎么监听我们的程序没有画面显示手机屏幕上,然后让程序图标出现在状态栏上。
      

  3.   

    怎么监听我们的程序没有显示在屏幕上,然后我才能去notification啊?比如我点击home按键,或者别的程序界面突然显示在屏幕上导致屏幕上显示的不是我们的程序界面,这时候我怎么在程序中监听我们的程序界面没有显示在屏幕上,然后去notification?
      

  4.   

    根据activity的生命周期,在activity不显示时,会执行onStop函数,所以你在onStop函数(按退出键除外)里面把notification放在通知栏里,再此显示时,把notification从通知栏里去掉。或者,只要程序在运行就一直显示通知栏图标,这样简单点。 private void showNotification() {
    // 创建一个NotificationManager的引用
    NotificationManager notificationManager = (NotificationManager) 
    context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);

    // 定义Notification的各种属性
    Notification notification = new Notification(R.drawable.icon,
    "天籁之音播放器", System.currentTimeMillis());
    notification.flags |= Notification.FLAG_ONGOING_EVENT; // 将此通知放到通知栏的"Ongoing"即"正在运行"组中
    notification.flags |= Notification.FLAG_NO_CLEAR; // 表明在点击了通知栏中的"清除通知"后,此通知不清除,经常与FLAG_ONGOING_EVENT一起使用
    notification.flags |= Notification.FLAG_SHOW_LIGHTS;
    notification.defaults = Notification.DEFAULT_LIGHTS;
    notification.ledARGB = Color.BLUE;
    notification.ledOnMS = 5000;

    // 设置通知的事件消息
    CharSequence contentTitle = "天籁之音正在播放……"; // 通知栏标题
    CharSequence contentText = "ameyume"; // 通知栏内容
    Intent notificationIntent = new Intent(context, MusicDemo.class); // 点击该通知后要跳转的Activity
    PendingIntent contentItent = PendingIntent.getActivity(context, 0,
    notificationIntent, 0);
    notification.setLatestEventInfo(context, contentTitle, contentText,
    contentItent); // 把Notification传递给NotificationManager
    notificationManager.notify(0, notification);
    }
      

  5.   

    去掉通知栏通知的方法
    // 启动后删除之前我们定义的通知
    NotificationManager notificationManager = (NotificationManager) this
    .getSystemService(NOTIFICATION_SERVICE);
    notificationManager.cancel(0);
      

  6.   

    感谢帮忙,还有就是,是不是只能监听activity啊,我的程序里面有好多的activity,这样是不是不太方便,当然也是可以让所有的activity继承一个父类,统一设置。我想说的是有没有不要去监听activity的,就是说有没有直接监听我们的程序是否在屏幕上显示的接口。
      

  7.   

    大哥 这种方法怎么判断   多个activity程序的情况啊
      我现在也急需解决 这个问题啊  还有packageinfo 怎么用啊
      

  8.   

    顶一下 不想在每个activity设置
      

  9.   

    写在下面的方法中就可以,我试过了。@Override  
    public boolean dispatchKeyEvent(KeyEvent event) {  
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {  
            if (event.getAction() == KeyEvent.ACTION_DOWN  
                    && event.getRepeatCount() == 0) {  
                exit();  
                return true;  
            }  
        }  
        return super.dispatchKeyEvent(event);