0
我有一個接收gcm推送通知的應用程序。我也有一個檢查,如果應用程序當前打開,它不會創建通知。在Android中,我的推送服務可以告訴當前活動(如果可用/已連接),新通知已到達,請使用新內容刷新您的列表?如果這是可能的,我相信我在我的服務上使用IBinders是正確的道路。關於這一點的事情是我對服務如何調用活動感到困惑(我理解爲反之)。如果有人能提供幫助,請提前致謝!Android通過本地服務向活動發送數據
只是要清楚。我正試圖告訴活動有關新推送消息。
服務
public class LocalBinder extends Binder {
GcmIntentService getService() {
return GcmIntentService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
private final IBinder mBinder = new LocalBinder();
客戶端(活動)
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
// This is called when the connection with the service has been
// established, giving us the service object we can use to
// interact with the service. Because we have bound to a explicit
// service that we know is running in our own process, we can
// cast its IBinder to a concrete class and directly access it.
mBoundService = ((GcmIntentService.LocalBinder)service).getService();
// Tell the user about this for our demo.
Toast.makeText(MainActivity.this, "Connected", Toast.LENGTH_SHORT).show();
}
public void onServiceDisconnected(ComponentName className) {
// This is called when the connection with the service has been
// unexpectedly disconnected -- that is, its process crashed.
// Because it is running in our same process, we should never
// see this happen.
mBoundService = null;
Toast.makeText(MainActivity.this, "Disconnected",
Toast.LENGTH_SHORT).show();
}
};
void doBindService() {
// Establish a connection with the service. We use an explicit
// class name because we want a specific service implementation that
// we know will be running in our own process (and thus won't be
// supporting component replacement by other applications).
bindService(new Intent(MainActivity.this,
GcmIntentService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
}
void doUnbindService() {
if (mIsBound) {
// Detach our existing connection.
unbindService(mConnection);
mIsBound = false;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
doUnbindService();
}
謝謝!按預期工作。 – user516883