Service binder
This commit is contained in:
parent
48fead3b55
commit
236b76188a
|
@ -22,6 +22,7 @@ import android.content.SharedPreferences;
|
|||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.os.Binder;
|
||||
import android.os.Build;
|
||||
import android.os.IBinder;
|
||||
import android.os.StrictMode;
|
||||
|
@ -97,38 +98,18 @@ public class StreamingService extends Service {
|
|||
DELETE,
|
||||
NONE
|
||||
}
|
||||
private final IBinder iBinder = new StreamingServiceBinder();
|
||||
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
if( intent != null){
|
||||
String accountId = intent.getStringExtra("accountId");
|
||||
String accountAcct = intent.getStringExtra("accountAcct");
|
||||
if( accountId != null && accountAcct != null){
|
||||
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
||||
Account account = new AccountDAO(getApplicationContext(), db).getAccountByIDAcct(accountId, accountAcct);
|
||||
if( account != null)
|
||||
callAsynchronousTask(account);
|
||||
}else {
|
||||
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
||||
List<Account> accounts = new AccountDAO(getApplicationContext(), db).getAllAccount();
|
||||
if( accounts != null){
|
||||
for (Account account: accounts) {
|
||||
intent = new Intent(getApplicationContext(), StreamingService.class);
|
||||
intent.putExtra("accountId", account.getId());
|
||||
intent.putExtra("accountAcct", account.getAcct());
|
||||
startService(intent);
|
||||
}
|
||||
}
|
||||
}
|
||||
public class StreamingServiceBinder extends Binder {
|
||||
public StreamingService getService() {
|
||||
return StreamingService.this;
|
||||
}
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
return iBinder;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -16,10 +16,12 @@ package fr.gouv.etalab.mastodon.activities;
|
|||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.ServiceConnection;
|
||||
import android.content.SharedPreferences;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.graphics.PorterDuff;
|
||||
|
@ -27,6 +29,8 @@ import android.net.Uri;
|
|||
import android.os.AsyncTask;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.IBinder;
|
||||
import android.os.Messenger;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.design.widget.FloatingActionButton;
|
||||
import android.support.design.widget.TabLayout;
|
||||
|
@ -903,6 +907,36 @@ public class MainActivity extends AppCompatActivity
|
|||
new UpdateAccountInfoByIDAsyncTask(getApplicationContext(), MainActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
}
|
||||
|
||||
|
||||
StreamingService streamingService = null;
|
||||
boolean mBound = false;
|
||||
private ServiceConnection serviceConnection = new ServiceConnection() {
|
||||
@Override
|
||||
public void onServiceConnected(ComponentName className, IBinder service) {
|
||||
StreamingService.StreamingServiceBinder binder = (StreamingService.StreamingServiceBinder) service;
|
||||
streamingService = binder.getService();
|
||||
mBound = true;
|
||||
}
|
||||
@Override
|
||||
public void onServiceDisconnected(ComponentName arg0) {
|
||||
mBound = false;
|
||||
}
|
||||
};
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
Intent intent = new Intent(this, StreamingService.class);
|
||||
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
|
||||
}
|
||||
@Override
|
||||
protected void onStop() {
|
||||
super.onStop();
|
||||
if (mBound) {
|
||||
unbindService(serviceConnection);
|
||||
mBound = false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
|
|
Loading…
Reference in New Issue