在android中使用BindServices的通知

时间:2014-01-22 08:55:08

标签: android service notifications smack

我正在使用smack在android中进行聊天应用程序。我有关于显示通知的问题。我在ListView中登录后显示一个状态为用户的好友列表。现在我希望向用户显示他获得的不同用户的聊天次数的通知,同时与同一活动的其他用户聊天。

如何实现这一目标?请帮我。 。任何示例代码或教程都会非常棒。

我正在使用绑定服务登录并从服务器获取好友列表。

1 个答案:

答案 0 :(得分:0)

like that ::->

public class WifiService extends Service {

private final IBinder mBinder = new MyBinder();

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub

    boolean networkStatus = haveNetworkConnection();
    ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    Notification not;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    if(networkStatus == true || netInfo.isConnected() == true){
         not = new Notification(R.drawable.on, "Wi-Fi Connector", System.currentTimeMillis());
    } else {
         not = new Notification(R.drawable.off, "Wi-Fi is not connector", System.currentTimeMillis());
    }


    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), Notification.FLAG_ONGOING_EVENT);        
    not.flags = Notification.FLAG_ONGOING_EVENT;
    if(networkStatus == true || netInfo.isConnected() == true){
        not.setLatestEventInfo(this, "Wi-Fi Connector" , "Wi-Fi is connected. You can download data.", contentIntent);
    } else {
        not.setLatestEventInfo(this, "Wi-Fi Connector" , "Wi-Fi is not connected. You can not download data.", contentIntent);
    }

    mNotificationManager.notify(1, not);

    return Service.START_NOT_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

public class MyBinder extends Binder {
    WifiService getService() {
        return WifiService.this;
    }
}

private boolean haveNetworkConnection() {
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo) {
        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                haveConnectedWifi = true;
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                haveConnectedMobile = true;
    }
    return haveConnectedWifi || haveConnectedMobile;
}
}