On Android, if you want to process in the background, you will use service. Let's examine service and create a simple sample code.
There are two types of services, with different methods to call.
・ Started service
Start with startService ()
.
The started service will continue to move until it is stopped.
It does not return the result to the caller, it just does the work.
· Bound service
Bind with bindService ()
.
You can control the bound service, such as sending requests and getting results.
However, when the calling Activity ends, it ends at the same time.
onCreate()
Since it is called once at the beginning, it will be initialized.
onStartCommand()
Write the process to be executed by the service.
onDestroy()
Service termination processing
onBind()
If you call it with bindService ()
, you can call it.
Background execution limit
Restrictions have been added to background processing from Android Oreo (8.0 / API Level 26).
Therefore, the newly introduced startForegroundService ()
replaces the above startService
Is used.
(Actually, it is necessary to separate the processing according to the SDK version)
There were various specifications when using startForegroundService ()
.
Demonstrate background service restrictions from Android O.
The use cases are summarized here in an easy-to-understand manner, so I used it as a reference.
Press the start button to start the service, and the stop button to stop the service. Since the log is output 10 seconds after the service starts, try checking by putting the application in the background before that.
AndroidManifest.xml
<service android:name=".TestService"/>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startButton = findViewById(R.id.button_start);
Button stopButton = findViewById(R.id.button_stop);
//Start of the service
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent serviceIntent = new Intent(getApplication(), TestService.class);
startForegroundService(serviceIntent);
}
});
//Service outage
stopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent serviceIntent = new Intent(getApplication(), TestService.class);
stopService(serviceIntent);
}
});
}
}
TestService.java
public class TestService extends Service {
@Override
public void onCreate() {
super.onCreate();
Log.d("DEBUG", "called TestService.onCreate()");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("DEBUG", "called TestService.onStartCommand()");
String channelId = "service";
String title = "TestService";
//Notification settings
NotificationManager notificationManager =
(NotificationManager)getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel =
new NotificationChannel(channelId, title, NotificationManager.IMPORTANCE_DEFAULT);
if(notificationManager != null) {
notificationManager.createNotificationChannel(channel);
Notification notification = new Notification.Builder(getApplicationContext(), channelId)
.setContentTitle(title)
.setSmallIcon(R.drawable.menu)
.setContentText("service start")
.build();
//Run in the foreground
startForeground(1, notification);
//Log output after 10 seconds
try {
Thread.sleep(10000);
Log.i("INFO", "processing service");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("DEBUG", "called TestService.onDestroy()");
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
An event occurred in which crashes occurred repeatedly when the service was started.
When I looked it up, it was due to the setting of the notification passed to the argument with startForeground ()
.
In Android 8.0, it seems that you should not specify Adaptive icon with Notification.setSmallIcon ()
.
I crashed with R.mipmap.ic_launcher
.
It is modified to specify an appropriate image for avoidance.
System UI crashes when displaying notifications on Android 8.0
I tried to verify the movement of the service easily. (The verification method wasn't very clean, but ...) The implementation method differs depending on the version, and there are various restrictions, so it seems that more study is required to make good use of it.
The following is a reference site. How to use [Android] Service Android Oreo: Foreground notifications and services (https://rakuishi.com/archives/android-oreo-notification-foreground/) Services | Android Developers
Recommended Posts