[RemoteServiceController.java]
private OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
startService(new Intent(
"com.example.android.apis.app.REMOTE_SERVICE"));
}
};
private OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
stopService(new Intent(
"com.example.android.apis.app.REMOTE_SERVICE"));
}
};
}
[RemoteService.java]
final RemoteCallbackList<IRemoteServiceCallback> mCallbacks
= new RemoteCallbackList<IRemoteServiceCallback>();
int mValue = 0;
NotificationManager mNM;
@Override
public void onCreate() {
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
showNotification();
mHandler.sendEmptyMessage(REPORT_MSG);
}
@Override
public void onDestroy() {
mNM.cancel(R.string.remote_service_started);
Toast.makeText(this, R.string.remote_service_stopped, Toast.LENGTH_SHORT).show();
mCallbacks.kill();
mHandler.removeMessages(REPORT_MSG);
}
@Override
public IBinder onBind(Intent intent) {
if (IRemoteService.class.getName().equals(intent.getAction())) {
return mBinder;
}
if (ISecondary.class.getName().equals(intent.getAction())) {
return mSecondaryBinder;
}
return null;
}
private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {
public void registerCallback(IRemoteServiceCallback cb) {
if (cb != null) mCallbacks.register(cb);
}
public void unregisterCallback(IRemoteServiceCallback cb) {
if (cb != null) mCallbacks.unregister(cb);
}
};
private final ISecondary.Stub mSecondaryBinder = new ISecondary.Stub() {
public int getPid() {
return Process.myPid();
}
public void basicTypes(int anInt, long aLong, boolean aBoolean,
float aFloat, double aDouble, String aString) {
}
};
private static final int REPORT_MSG = 1;
private final Handler mHandler = new Handler() {
@Override public void handleMessage(Message msg) {
switch (msg.what) {
case REPORT_MSG: {
int value = ++mValue;
final int N = mCallbacks.beginBroadcast();
for (int i=0; i<N; i++) {
try {
mCallbacks.getBroadcastItem(i).valueChanged(value);
} catch (RemoteException e) {
}
}
mCallbacks.finishBroadcast();
sendMessageDelayed(obtainMessage(REPORT_MSG), 1*1000);
} break;
default:
super.handleMessage(msg);
}
}
};
private void showNotification() {
CharSequence text = getText(R.string.remote_service_started);
Notification notification = new Notification(R.drawable.stat_sample, text,
System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, LocalServiceController.class), 0);
notification.setLatestEventInfo(this, getText(R.string.remote_service_label),
text, contentIntent);
mNM.notify(R.string.remote_service_started, notification);
}
}
[Manifest.xml]
<intent-filter>
<action android:name="com.example.android.apis.app.IRemoteService" />
<action android:name="com.example.android.apis.app.ISecondary" />
<action android:name="com.example.android.apis.app.REMOTE_SERVICE" />
</intent-filter>
</service>
[IRemoteService.aidl]
package com.example.android.apis.app;
import com.example.android.apis.app.IRemoteServiceCallback;
interface IRemoteService {
void registerCallback(IRemoteServiceCallback cb);
void unregisterCallback(IRemoteServiceCallback cb);
}
[IRemoteServiceCallback.aidl]
package com.example.android.apis.app;
oneway interface IRemoteServiceCallback {
void valueChanged(int value);
}
[ISecondary.aidl]
interface ISecondary {
int getPid();
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
}
'old > API_Demo' 카테고리의 다른 글
APP_Service_ServiceStartArgumentsController (0) | 2010.03.25 |
---|---|
APP_Service_RemoteServiceBinding (0) | 2010.03.25 |
APP_Service_LocalServiceController (0) | 2010.03.25 |
APP_Service_LocalServiceBinding (0) | 2010.03.25 |
APP_Service_ForegroundServiceController (0) | 2010.03.24 |