Advertisement
Qpel

Untitled

Nov 29th, 2019
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.99 KB | None | 0 0
  1. package com.example.uzduotisdu;
  2.  
  3. import android.app.Service;
  4. import android.content.Intent;
  5. import android.os.IBinder;
  6. import android.widget.Toast;
  7.  
  8.  
  9. import static android.app.Service.START_STICKY;
  10.  
  11. public class SimpleService extends Service {
  12.     @Override
  13.     public IBinder onBind(Intent intent){
  14.         return null;
  15.     }
  16.  
  17.     @Override
  18.     public int onStartCommand(Intent intent, int flags, int startId) {
  19.         NotificationHelper notificationHelper = new NotificationHelper(getBaseContext());
  20.         notificationHelper.createNotification("Just a title", "And some meaningful text here");
  21.         return START_STICKY;
  22.     }
  23.  
  24.  
  25.     @Override
  26.     public void onDestroy(){
  27.         super.onDestroy();
  28.         Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
  29.     }
  30.  
  31. }
  32.  
  33.  
  34.  
  35. ServiceTestActivity:
  36.  
  37.  
  38.  
  39. package com.example.uzduotisdu;
  40.  
  41. import androidx.appcompat.app.AppCompatActivity;
  42. import androidx.core.app.NotificationCompat;
  43.  
  44. import android.app.NotificationChannel;
  45. import android.app.NotificationManager;
  46. import android.app.PendingIntent;
  47. import android.content.Context;
  48. import android.graphics.Color;
  49. import android.provider.Settings;
  50.  
  51. import android.app.ActivityManager;
  52. import android.content.Intent;
  53. import android.os.Bundle;
  54. import android.view.View;
  55. import android.widget.Toast;
  56.  
  57.  
  58.  
  59. class NotificationHelper {
  60.     private Context mContext;
  61.     private NotificationManager mNotificationManager;
  62.     private NotificationCompat.Builder mBuilder;
  63.     private static final String NOTIFICATION_CHANNEL_ID = "10001";
  64.  
  65.     NotificationHelper(Context context) {
  66.         mContext = context;
  67.     }
  68.  
  69.     void createNotification(String title, String message)
  70.     {
  71.         Intent resultIntent = new Intent(mContext, MainActivity.class);
  72.         resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  73.  
  74.         PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
  75.                 0 /* Request code */, resultIntent,
  76.                 PendingIntent.FLAG_UPDATE_CURRENT);
  77.         mBuilder = new NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID);
  78.         mBuilder.setSmallIcon(R.mipmap.ic_launcher);
  79.         mBuilder.setContentTitle(title)
  80.                 .setContentText(message)
  81.                 .setAutoCancel(false)
  82.                 .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
  83.                 .setContentIntent(resultPendingIntent);
  84.         mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
  85.  
  86.         if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
  87.         {
  88.             int importance = NotificationManager.IMPORTANCE_HIGH;
  89.             NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
  90.                     "NOTIFICATION_CHANNEL_NAME", importance);
  91.             notificationChannel.enableLights(true);
  92.             notificationChannel.setLightColor(Color.RED);
  93.             notificationChannel.enableVibration(true);
  94.             notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
  95.             assert mNotificationManager != null;
  96.             mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
  97.             mNotificationManager.createNotificationChannel(notificationChannel);
  98.         }
  99.  
  100.         assert mNotificationManager != null;
  101.         mNotificationManager.notify(0, mBuilder.build());
  102.     }
  103.  
  104. }
  105.  
  106. public class ServiceTestACtivity extends AppCompatActivity implements View.OnClickListener {
  107.  
  108.  
  109.  
  110.     private void setup_on_clicks(){
  111.         findViewById(R.id.start_btn).setOnClickListener(this);
  112.         findViewById(R.id.stop_btn).setOnClickListener(this);
  113.     }
  114.  
  115.     @Override
  116.     protected void onCreate(Bundle savedInstanceState) {
  117.         super.onCreate(savedInstanceState);
  118.         setContentView(R.layout.activity_service_test);
  119.         setup_on_clicks();
  120.     }
  121.  
  122.  
  123.  
  124.     private void startService() {
  125.         if(!isMyServiceRunning(SimpleService.class)) {
  126.             startService(new Intent(getBaseContext(), SimpleService.class));
  127.         } else {
  128.             Toast.makeText(this, "Service already running", Toast.LENGTH_LONG).show();
  129.         }
  130.     }
  131.     private void stopService() {
  132.         stopService(new Intent(getBaseContext(), SimpleService.class));
  133.     }
  134.  
  135.     private boolean isMyServiceRunning(Class<?> serviceClass) {
  136.         ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
  137.         for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
  138.             if (serviceClass.getName().equals(service.service.getClassName())){
  139.                 return true;
  140.             }
  141.         }
  142.         return false;
  143.     }
  144.     public void onClick(View view){
  145.         switch (view.getId()){
  146.             case (R.id.start_btn):
  147.                 startService();
  148.                 break;
  149.             case (R.id.stop_btn):
  150.                 stopService();
  151.                 break;
  152.         }
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement