Created
April 19, 2020 16:37
-
-
Save s0lver/fb3863f0b99afd762c1be051492f1d59 to your computer and use it in GitHub Desktop.
Part of Continuous background operation in Android: Services
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Hello World!" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<Button | |
android:id="@+id/btnSum" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:onClick="onClickSum" | |
android:text="Sum" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<Button | |
android:id="@+id/btnDivide" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:onClick="onClickDivide" | |
android:text="Divide" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.s0lver"> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:roundIcon="@mipmap/ic_launcher_round" | |
android:supportsRtl="true" | |
android:theme="@style/AppTheme"> | |
<activity android:name=".MainActivity"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
<service android:name="com.s0lver.MyBoundService" /> | |
<service android:name="com.s0lver.MyStartedService" /> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// for bound service | |
package com.s0lver; | |
import android.content.ComponentName; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.ServiceConnection; | |
import android.os.Bundle; | |
import android.os.IBinder; | |
import android.view.View; | |
import android.widget.Toast; | |
import androidx.appcompat.app.AppCompatActivity; | |
import java.util.Random; | |
public class MainActivity extends AppCompatActivity { | |
private MyBoundService myBoundService; | |
private Random random; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
random = new Random(0); | |
} | |
@Override | |
protected void onStart() { | |
super.onStart(); | |
Intent intent = new Intent(this, MyBoundService.class); | |
bindService(intent, connection, Context.BIND_AUTO_CREATE); | |
} | |
@Override | |
protected void onDestroy() { | |
unbindService(connection); | |
super.onDestroy(); | |
} | |
private ServiceConnection connection = new ServiceConnection() { | |
@Override | |
public void onServiceConnected(ComponentName className, | |
IBinder service) { | |
MyBoundService.TheBinder binder = (MyBoundService.TheBinder) service; | |
myBoundService = binder.getService(); | |
} | |
@Override | |
public void onServiceDisconnected(ComponentName arg0) { | |
} | |
}; | |
public void onClickSum(View view) { | |
double a = random.nextDouble(); | |
double b = random.nextDouble(); | |
double sum = myBoundService.sum(a, b); | |
Toast.makeText(this, String.format("%s + %s = %s", a, b, sum), Toast.LENGTH_SHORT).show(); | |
} | |
public void onClickDivide(View view) { | |
double a = random.nextDouble(); | |
double b = random.nextDouble(); | |
double division = myBoundService.divide(a, b); | |
Toast.makeText(this, String.format("%s / %s = %s", a, b, division), Toast.LENGTH_SHORT).show(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// for started service | |
package com.s0lver; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.util.Log; | |
import androidx.appcompat.app.AppCompatActivity; | |
public class MainActivity extends AppCompatActivity { | |
private Intent serviceIntent; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
serviceIntent = new Intent(this, MyStartedService.class); | |
serviceIntent.putExtra("name", "Rafael"); | |
startService(serviceIntent); | |
} | |
@Override | |
protected void onDestroy() { | |
Log.i(this.getClass().getSimpleName(), "onDestroy()"); | |
stopService(serviceIntent); | |
super.onDestroy(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.s0lver; | |
import android.app.Service; | |
import android.content.Intent; | |
import android.os.Binder; | |
import android.os.IBinder; | |
import android.util.Log; | |
import androidx.annotation.Nullable; | |
public class MyBoundService extends Service { | |
public IBinder theBinder = new TheBinder(); | |
public class TheBinder extends Binder { | |
MyBoundService getService() { | |
return MyBoundService.this; | |
} | |
} | |
@Nullable | |
@Override | |
public IBinder onBind(Intent intent) { | |
Log.i(this.getClass().getSimpleName(), "onBind()"); | |
return theBinder; | |
} | |
public double sum(double a, double b) { | |
return a + b; | |
} | |
public double divide(double a, double b) { | |
return a / b; | |
} | |
@Override | |
public void onDestroy() { | |
Log.i(this.getClass().getSimpleName(), "onDestroy()"); | |
super.onDestroy(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.s0lver; | |
import android.app.Service; | |
import android.content.Intent; | |
import android.os.IBinder; | |
import android.util.Log; | |
import androidx.annotation.Nullable; | |
import androidx.core.app.NotificationCompat; | |
import java.util.Random; | |
import java.util.Timer; | |
import java.util.TimerTask; | |
public class MyStartedService extends Service { | |
private Timer myTimer; | |
private static final int ID = new Random().nextInt(99999); | |
@Nullable | |
@Override | |
public IBinder onBind(Intent intent) { | |
return null; | |
} | |
@Override | |
public int onStartCommand(Intent intent, int flags, int startId) { | |
Log.i(this.getClass().getSimpleName(), "onStartCommand()"); | |
String name = intent.getStringExtra("name"); | |
NotificationCompat.Builder b = new NotificationCompat.Builder(this, "channel_id_value"); | |
b.setSmallIcon(R.drawable.ic_launcher_background) | |
.setContentTitle("MyStartedService") | |
.setContentText("MyStartedService has been, well, started..."); | |
startForeground(ID, b.build()); | |
doTask(name); | |
return START_STICKY; // START_NOT_STICKY | |
} | |
private void doTask(final String name) { | |
myTimer = new Timer(); | |
myTimer.scheduleAtFixedRate(new TimerTask() { | |
@Override | |
public void run() { | |
Log.i("MyServiceStarted", String.format("Hello %s", name)); | |
} | |
}, 0, 3 * 1000); | |
} | |
@Override | |
public void onCreate() { | |
Log.i(this.getClass().getSimpleName(), "onCreate()"); | |
} | |
@Override | |
public void onDestroy() { | |
Log.i(this.getClass().getSimpleName(), "onDestroyService()"); | |
myTimer.cancel(); | |
stopForeground(true); | |
super.onDestroy(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment