Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.uzduotisdu;
- import android.app.Service;
- import android.content.Intent;
- import android.os.IBinder;
- import android.widget.Toast;
- import static android.app.Service.START_STICKY;
- public class SimpleService extends Service {
- @Override
- public IBinder onBind(Intent intent){
- return null;
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- NotificationHelper notificationHelper = new NotificationHelper(getBaseContext());
- notificationHelper.createNotification("Just a title", "And some meaningful text here");
- return START_STICKY;
- }
- @Override
- public void onDestroy(){
- super.onDestroy();
- Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
- }
- }
- ServiceTestActivity:
- package com.example.uzduotisdu;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.core.app.NotificationCompat;
- import android.app.NotificationChannel;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Context;
- import android.graphics.Color;
- import android.provider.Settings;
- import android.app.ActivityManager;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Toast;
- class NotificationHelper {
- private Context mContext;
- private NotificationManager mNotificationManager;
- private NotificationCompat.Builder mBuilder;
- private static final String NOTIFICATION_CHANNEL_ID = "10001";
- NotificationHelper(Context context) {
- mContext = context;
- }
- void createNotification(String title, String message)
- {
- Intent resultIntent = new Intent(mContext, MainActivity.class);
- resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
- 0 /* Request code */, resultIntent,
- PendingIntent.FLAG_UPDATE_CURRENT);
- mBuilder = new NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID);
- mBuilder.setSmallIcon(R.mipmap.ic_launcher);
- mBuilder.setContentTitle(title)
- .setContentText(message)
- .setAutoCancel(false)
- .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
- .setContentIntent(resultPendingIntent);
- mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
- if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
- {
- int importance = NotificationManager.IMPORTANCE_HIGH;
- NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
- "NOTIFICATION_CHANNEL_NAME", importance);
- notificationChannel.enableLights(true);
- notificationChannel.setLightColor(Color.RED);
- notificationChannel.enableVibration(true);
- notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
- assert mNotificationManager != null;
- mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
- mNotificationManager.createNotificationChannel(notificationChannel);
- }
- assert mNotificationManager != null;
- mNotificationManager.notify(0, mBuilder.build());
- }
- }
- public class ServiceTestACtivity extends AppCompatActivity implements View.OnClickListener {
- private void setup_on_clicks(){
- findViewById(R.id.start_btn).setOnClickListener(this);
- findViewById(R.id.stop_btn).setOnClickListener(this);
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_service_test);
- setup_on_clicks();
- }
- private void startService() {
- if(!isMyServiceRunning(SimpleService.class)) {
- startService(new Intent(getBaseContext(), SimpleService.class));
- } else {
- Toast.makeText(this, "Service already running", Toast.LENGTH_LONG).show();
- }
- }
- private void stopService() {
- stopService(new Intent(getBaseContext(), SimpleService.class));
- }
- private boolean isMyServiceRunning(Class<?> serviceClass) {
- ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
- for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
- if (serviceClass.getName().equals(service.service.getClassName())){
- return true;
- }
- }
- return false;
- }
- public void onClick(View view){
- switch (view.getId()){
- case (R.id.start_btn):
- startService();
- break;
- case (R.id.stop_btn):
- stopService();
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement