Android學習--13-服務

首先寫個簡單的例子

建立

public class MyService extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    
    @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();
    }
}
// 註冊
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
……
<service android:name=".MyService" ></service>
</application>

啓動,中止

Intent startIntent = new Intent(this, MyService.class);
startService(startIntent); // 啓動服務

Intent stopIntent = new Intent(this, MyService.class);
stopService(stopIntent); // 中止服務

onBind() 是幹什麼用的呢?java

活動和服務

服務啓動之後,活動就管不住着它了。 怎麼辦?android

活動想指揮服務,就得藉助 onBind()app

好比MyService 提供了一個下載功能,讓活動什麼時候去下載,隨時看下載進度。ide

public class MyService extends Service {
    private DownloadBinder mBinder = new DownloadBinder();

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    class DownloadBinder extends Binder {
        public void startDownload() {
            Log.d("MyService", "startDownload executed");
        }

        public int getProgress() {
            Log.d("MyService", "getProgress executed");
            return 0;
        }
    }
}

綁定和解綁ui

private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
downloadBinder = (MyService.DownloadBinder) service;
downloadBinder.startDownload();
downloadBinder.getProgress();
}
};


Intent bindIntent = new Intent(this, MyService.class);
bindService(bindIntent, connection, BIND_AUTO_CREATE); // 綁定服務

unbindService(connection); // 解綁服務

好比說 又想像墨跡天氣同樣,讓服務停留在前臺。this

其實還能夠防止內存不足被系統回收。線程

前臺服務

在Service 中onCreate 方法中寫code

Notification notification = new Notification(R.drawable.ic_launcher,"Notification comes", System. currentTimeMillis());
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
notification.setLatestEventInfo(this, "This is title", "This iscontent", pendingIntent);
startForeground(1, notification);

上面的代碼其實已通過時了,這是新的。ip

Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setTicker("Notification comes");
        builder.setContentTitle("第一個通知");
        builder.setContentText("天天進步一點點");
        builder.setWhen(System.currentTimeMillis());

        builder.setContentIntent(pendingIntent);
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
      // 1是標識符,隨便寫。之後有新的消息會覆蓋上去,固然移除的時候也能夠用到。
       nm.notify(1, notification);

IntentService

服務都是默認在主線程中進行的,服務若是處理耗時的操做,容易引發ANR。內存

怎麼辦? 弄個子線程唄。

若是想在服務啓動後,自動結束。調用此 stopSelf();

IntentService 就是爲了防止忘記開個線程去執行服務,就提供了這個。

public class MyIntentService extends IntentService {
    public MyIntentService() {
        super("MyIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.d("MyIntentService",
            "Thread id is " + Thread.currentThread().getId());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("MyIntentService", "onDestroy executed");
    }
}
// 啓動
Intent intentService = new Intent(this, MyIntentService.class);
startService(intentService);
相關文章
相關標籤/搜索