WEB开发网
开发学院手机开发Android 开发 Android 教程 使用Service 阅读

Android 教程 使用Service

 2010-05-06 16:23:00 来源:WEB开发网   
核心提示:public class LocalBinder extends Binder {TestService getService() {return TestService.this;}}@Overridepublic boolean onUnbind(Intent i) {Log.e(TAG, “> TestSe

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”);

上一页  1 2 3 4 5 6  下一页

Tags:Android 教程 使用

编辑录入:coldstar [复制链接] [打 印]
赞助商链接