Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. import android.app.Activity;
  2. import android.app.IntentService;
  3. import android.app.Notification;
  4. import android.app.NotificationManager;
  5. import android.app.PendingIntent;
  6. import android.app.TaskStackBuilder;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.support.v4.content.LocalBroadcastManager;
  10. import android.support.v7.app.NotificationCompat;
  11. import android.widget.Toast;
  12. import java.util.Random;
  13.  
  14. public class MyService extends IntentService {
  15.  
  16. public MyService()
  17. {
  18. super("Test Service");
  19. }
  20.  
  21. @Override
  22. public int onStartCommand(Intent intent, int flag, int startId){
  23. return super.onStartCommand(intent, flag, startId);
  24. }
  25.  
  26. @Override
  27. public void onCreate(){
  28. super.onCreate();
  29. Toast.makeText(getApplicationContext(), "Tworzenie usługi", Toast.LENGTH_SHORT).show();
  30. }
  31.  
  32. @Override
  33. public void onDestroy(){
  34. super.onDestroy();
  35. Toast.makeText(getApplicationContext(), "Zakończenie usługi", Toast.LENGTH_SHORT).show();
  36. }
  37.  
  38. @Override
  39. protected void onHandleIntent(Intent intent) {
  40. synchronized (this){
  41. try{
  42. Thread.sleep(3000);
  43. Message("Zakończono powodzeniem");
  44. UpdateUI("Zakończono powodzeniem");
  45. }catch (Exception ex){
  46. ex.printStackTrace();
  47. }
  48. }
  49. }
  50.  
  51. private void UpdateUI(String message){
  52. Intent intent = new Intent("androidlabs.uslugi.MyService");
  53. intent.putExtra("value", message);
  54. intent.putExtra("code", Activity.RESULT_OK);
  55. LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
  56. }
  57.  
  58. private void Message(String message){
  59. Intent intent = new Intent(this, MainActivity.class);
  60. //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
  61. PendingIntent pendingIntent =
  62. PendingIntent.getActivity(this, 0, intent, 0);
  63. Notification notification = new Notification.Builder(this)
  64. .setSmallIcon(R.mipmap.ic_launcher)
  65. .setContentTitle("ProgressBar")
  66. .setContentText(message)
  67. .setAutoCancel(true)
  68. .setContentIntent(pendingIntent)
  69. .setPriority(NotificationCompat.PRIORITY_DEFAULT)
  70. .build();
  71. String txt = "Powodzenie!!";
  72.  
  73. NotificationManager notificationManager =
  74. (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  75.  
  76. notificationManager.notify(txt, 1, notification);
  77.  
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement