android 初識service(一)

熟悉android的朋友對service應該都不陌生了 今天我簡單介紹一下service 希望可以幫助一下人更快捷的認識service
從字面意思看 就是服務 運行於後臺 (當然有需要的話也可以運行在前臺,看需求)
首先介紹一下service的生命週期
1 onCreate() 在service 建立時調用
2 onStartCommand() 在service被startService() 被調用
3 onBind() 在service被綁定時被調用
4 onDestroy() 當service被stopService()被調用
這是一個簡單的service
-# 創建service時 記住要在清單文件中聲明 方可使用 #-

public class MyService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return my;
}

@Override
public void onCreate() {
// 只在創建時被調用 
    super.onCreate();

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// 開啓時被調用
    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
// 只在關閉時被調用
    super.onDestroy();
}

}
}
service 高頻活動的方法是 onStartCommand() 當service被創建後 再次調用 首先走得就是onStartCommand 而不是onCreate();
service 和activity 通信
是靠onbind(); 方法 一個service 可以被多個activity綁定 此時service 是不能杯關閉的 除非service 處於不被綁定狀態時 纔可以被銷燬

service
public class MyService extends Service {
private MyBinder my = new MyBinder();
@Nullable
@Override
public IBinder onBind(Intent intent) {
return my;
}

@Override
public void onCreate() {
    super.onCreate();

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
    super.onDestroy();
}

}
class MyBinder extends Binder{
public void toToast(final Context context){

  try {
      Thread.sleep(3000);
      Toast.makeText(context, "執行了", Toast.LENGTH_SHORT).show();
  } catch (InterruptedException e) {
      e.printStackTrace();
  }

}

}

activity

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

private Button start;
private Button stop;
private Button bind;

ServiceConnection connection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        binder = (MyBinder) service;
        binder.toToast(MainActivity.this);
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {

    }
};
private MyBinder binder;
private Button unbind;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initview();
}

private void initview() {
    start = (Button) findViewById(R.id.start_service);
    stop = (Button) findViewById(R.id.stop_service);
    bind = (Button) findViewById(R.id.bind_service);
    unbind = (Button) findViewById(R.id.unbind_service);
    start.setOnClickListener(this);
    stop.setOnClickListener(this);
    bind.setOnClickListener(this);
    unbind.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.start_service:
            Intent starts= new Intent(this,MyService.class);
            startService(st);
            break;
        case R.id.stop_service:
            Intent stops = new Intent(this,MyService.class);
            stopService(stops);
            break;
        case R.id.bind_service:
            Intent binds = new Intent(this,MyService.class);
            bindService(binds,connection,BIND_AUTO_CREATE);
            break;
        case R.id.unbind_service:
            unbindService(connection);
            break;
    }
}

創建前臺 service

可以自定義一個通知欄
在service 的 onCreate();
@Override
public void onCreate() {
super.onCreate();
Notification.Builder builder = new Notification.Builder(getApplicationContext());
builder.setSmallIcon(R.drawable.lamsejiahao);
builder.setTicker(“新消息”); // 通知提示
builder.setContentTitle(“第一個通知”); // 通知欄消息標題
builder.setContentText(“每天進步一點點”); // 通知欄消息內容
builder.setWhen(System.currentTimeMillis()); //發送時間
builder.setDefaults(Notification.DEFAULT_ALL);
Notification notification = builder.build();
NotificationManager notificationManager = (NotificationManager)getApplicationContext()
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(123, notification);
Log.d(TAG, “onCreate: 創建服務-自定義通知欄–”);
}
如果你需要執行一個下載任務 並顯示進度 顯然這樣使用體驗更好
好了 service (一 )到此結束 下次更新service(二)再來探討
每天都能進步一點點 大家共勉

發佈了72 篇原創文章 · 獲贊 80 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章