Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 初級開發 >> 創建android的Service

創建android的Service

編輯:初級開發

創建android的Service:

1,首先要定義一個服務:如下:

public static class UpdateService extends Service{

@Override

public void onStart(Intent intent, int startId){

Time estTime = new Time("BEIJING");

String time1 = estTime.getCurrentTimezone();

Time time = new Time(time1);

time.setToNow();//將獲取的時區的時間設置為當前時間。

int week = time.getWeekNumber();

RemoteViews updateViews = new RemoteVIEws(getPackageName(), R.layout.digitalclock);

updateViews.setTextVIEwText(R.id.time, time.format("%H:%M:%S"));

updateViews.setTextVIEwText(R.id.time1, week+time1);

updateVIEws.setTextColor(R.id.time, Color.GREEN);

ComponentName thisWidget = new ComponentName(this, widget.class);

//指定程序部件的ID

AppWidgetManager manager = AppWidgetManager.getInstance(this);

manager.updateAppWidget(thisWidget, updateVIEws);

}

@Override

public IBinder onBind(Intent intent){

return null;

}

2.要開啟這個服務:如下

@Override

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

context.startService(new Intent(context, UpdateService.class)); //啟動一個服務

}

3.在Menifest.XML中注冊這個服務:如下

< service

android:name=".widget$UpdateService"

/>

此句話的意思是:該服務的名字為UpdateService,它是widget的一個內部類

4,總結

服務的簡單使用就三個步驟:

定義,注冊,開啟。 關於Service的更多使用,還要看bindService()和 unbindService()

Chap06ex1.Java

package com.android;

import android.app.Activity;

import android.app.Service;

import android.content.ComponentName;

import android.content.Intent;

import android.content.ServiceConnection;

import android.os.IBinder;

import android.util.Log;

import android.view.VIEw;

import android.view.VIEw.OnClickListener;

import android.widget.Button;

import android.os.Bundle;

public class chap06ex1 extends Activity {

/** Called when the activity is first created. */

//listener;

/*Button startbt=(Button)findVIEwById(R.id.start);

Button stopbt=(Button)findVIEwById(R.id.stop);

Button bindbt=(Button)findVIEwById(R.id.bind);

Button unbindbt=(Button)findVIEwById(R.id.unbind);*/

ServiceConnection connection;

OnClickListener listener;

//connection;

//findVIEwById(R.id.start).setOnClickListener(listener)

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentVIEw(R.layout.main);

listener=new OnClickListener(){

public void onClick(VIEw v){

Intent i=new Intent(chap06ex1.this,SampleService.class);

switch(v.getId()){

case R.id.start:

startService(i);

break;

case R.id.stop:

stopService(i);

break;

case R.id.bind:

bindService(i,connection,BIND_AUTO_CREATE);

break;

case R.id.unbind:

unbindService(connection);

break;

default:

break;

}

}

};

findVIEwById(R.id.start).setOnClickListener(listener);

findVIEwById(R.id.stop).setOnClickListener(listener);

findVIEwById(R.id.bind).setOnClickListener(listener);

findVIEwById(R.id.unbind).setOnClickListener(listener);

/*startbt.setOnClickListener(listener);

stopbt.setOnClickListener(listener);

bindbt.setOnClickListener(listener);

unbindbt.setOnClickListener(listener);

*/

}

}

SampleService.Java

package com.android;

import android.app.Activity;

import android.app.Service;

import android.content.Intent;

import android.os.IBinder;

import android.util.Log;

import android.os.Bundle;

public class SampleService extends Service{

@Override

public void onCreate() {

// TODO Auto-generated method stub

super.onCreate();

Log.d(TAG,"OnCreate");

}

@Override

public void onDestroy() {

// TODO Auto-generated method stub

super.onDestroy();

Log.d(TAG,"OnDestroy");

}

@Override

public void onRebind(Intent intent) {

// TODO Auto-generated method stub

super.onRebind(intent);

Log.d(TAG,"OnRebind");

}

@Override

public void onStart(Intent intent, int startId) {

// TODO Auto-generated method stub

super.onStart(intent, startId);

Log.d(TAG,"OnStart");

}

@Override

public boolean onUnbind(Intent intent) {

// TODO Auto-generated method stub

Log.d(TAG, "OnUnBind");

return super.onUnbind(intent);

}

final String TAG="Service";

@Override

public IBinder onBind(Intent intent) {

// TODO Auto-generated method stub

Log.d(TAG,"OnBind");

return null;

}

}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved