Android 教程 使用Service
2010-05-06 16:23:00 来源:WEB开发网public class LocalBinder extends Binder {
TestService getService() {
return TestService.this;
}
}
@Override
public boolean onUnbind(Intent i) {
Log.e(TAG, “============> TestService.onUnbind”);
return false;
}
@Override
public void onRebind(Intent i) {
Log.e(TAG, “============> TestService.onRebind”);
}
@Override
public void onCreate() {
Log.e(TAG, “============> TestService.onCreate”);
_nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
showNotification();
}
@Override
public void onStart(Intent intent, int startId) {
Log.e(TAG, “============> TestService.onStart”);
}
@Override
public void onDestroy() {
_nm.cancel(R.string.service_started);
Log.e(TAG, “============> TestService.onDestroy”);
}
private void showNotification() {
Notification notification = new Notification(R.drawable.face_1,
“Service started”, System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, TestServiceHolder.class), 0);
// must set this for content view, or will throw a exception
notification.setLatestEventInfo(this, “Test Service”,
“Service started”, contentIntent);
_nm.notify(R.string.service_started, notification);
}
}
其中用到Notification是为了明显地表明Service存活的状态,跟demo的code学过来的,这样看上去直观一点,更多关于 Notification的内容以后UI部分来写吧,现在就知道怎么使用就好了。
Java代码
@Override
public void onCreate() {
Log.e(TAG, “============> TestService.onCreate”);
更多精彩
赞助商链接