Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.98 KB | None | 0 0
  1. public class BootCompletedIntentListener extends BroadcastReceiver {
  2. @Override
  3. public void onReceive(Context context, Intent intent) {
  4. if("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())){
  5. Intent serviceIntent = new Intent(context,ClipMonitorService.class);
  6. ContextCompat.startForegroundService(context,serviceIntent);
  7. }
  8. }
  9. }
  10.  
  11. public class ClipMonitorService extends Service {
  12. private static final String TAG = "ClipboardManager";
  13.  
  14. private ExecutorService mThreadPool = Executors.newSingleThreadExecutor();
  15. private ClipboardManager mClipboardManager;
  16. private PrefManager prefManager;
  17.  
  18. @Override
  19. public void onCreate() {
  20. super.onCreate();
  21. prefManager = new PrefManager(this);
  22. }
  23.  
  24. @Override
  25. public void onDestroy() {
  26. super.onDestroy();
  27. if (mClipboardManager != null) {
  28. mClipboardManager.removePrimaryClipChangedListener(
  29. mOnPrimaryClipChangedListener);
  30. }
  31. }
  32.  
  33. @Override
  34. public IBinder onBind(Intent intent) {
  35. return null;
  36. }
  37.  
  38. @Override
  39. public int onStartCommand(Intent intent, int flags, int startId) {
  40. Intent notificationIntent = new Intent(this, MainActivity.class);
  41. PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
  42.  
  43. Intent settingIntent = new Intent(this, SettingActivity.class);
  44. PendingIntent pendingSettIntent = PendingIntent.getActivity(this, 0, settingIntent, 0);
  45.  
  46. RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
  47. remoteViews.setOnClickPendingIntent(R.id.btn_action, pendingSettIntent);
  48. remoteViews.setTextViewText(R.id.notif_subtitle, "1 Clips copied Today");
  49.  
  50.  
  51. Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
  52. .setSmallIcon(R.mipmap.ic_launcher_round)
  53. .setContent(remoteViews)
  54. .setVisibility(Notification.VISIBILITY_SECRET)
  55. .setPriority(NotificationCompat.PRIORITY_MIN)
  56. .setContentIntent(pendingIntent)
  57. .setColor(getResources().getColor(R.color.colorPrimary))
  58. .setShowWhen(false)
  59. .build();
  60.  
  61. startForeground(1, notification);
  62. mClipboardManager =
  63. (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
  64. mClipboardManager.addPrimaryClipChangedListener(
  65. mOnPrimaryClipChangedListener);
  66.  
  67. return START_STICKY;
  68. }
  69.  
  70. private ClipboardManager.OnPrimaryClipChangedListener mOnPrimaryClipChangedListener =
  71. new ClipboardManager.OnPrimaryClipChangedListener() {
  72. @Override
  73. public void onPrimaryClipChanged() {
  74. Log.d(TAG, "onPrimaryClipChangeds");
  75.  
  76. try {
  77.  
  78. String textToPaste = mClipboardManager.getPrimaryClip().getItemAt(0).getText().toString();
  79.  
  80. if (textToPaste.length() > 200) {
  81. if (prefManager.isClipNotifOns()) {
  82. mThreadPool.execute(new MakeNotifRunnable(
  83. textToPaste));
  84.  
  85. }
  86.  
  87. }
  88. } catch (Exception ignored) {
  89.  
  90. }
  91.  
  92.  
  93. }
  94. };
  95.  
  96. private class MakeNotifRunnable implements Runnable {
  97. private final CharSequence mTextToWrite;
  98.  
  99. public MakeNotifRunnable(CharSequence text) {
  100. mTextToWrite = text;
  101. }
  102.  
  103. @Override
  104. public void run() {
  105. Intent notifIntent = new Intent(getApplicationContext(), PostNewsActivity.class);
  106. notifIntent.putExtra("post", mTextToWrite);
  107.  
  108. NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
  109.  
  110. int notificationId = 2;
  111. String channelId = "channel1";
  112. String channelName = "Clipboard Monitor Notification";
  113. int importance = 0;
  114. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
  115. importance = NotificationManager.IMPORTANCE_DEFAULT;
  116. }
  117.  
  118. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
  119. NotificationChannel mChannel = new NotificationChannel(
  120. channelId, channelName, importance);
  121. notificationManager.createNotificationChannel(mChannel);
  122. }
  123.  
  124. NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(), channelId)
  125. .setSmallIcon(R.mipmap.ic_launcher_round)
  126. .setContentTitle("Verify copied content")
  127. .setContentText(mTextToWrite)
  128. .setAutoCancel(true)
  129. .setOnlyAlertOnce(true);
  130.  
  131. TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
  132. stackBuilder.addNextIntent(notifIntent);
  133. PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
  134. 0,
  135. PendingIntent.FLAG_UPDATE_CURRENT
  136. );
  137. mBuilder.setContentIntent(resultPendingIntent);
  138.  
  139. notificationManager.notify(notificationId, mBuilder.build());
  140. }
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement