Advertisement
GGGG2468

Notification

Jan 30th, 2023
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.86 KB | None | 0 0
  1. activity_main.xml
  2.  
  3.  
  4. <?xml version="1.0" encoding="utf-8"?>
  5. <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
  6.  xmlns:tools="http://schemas.android.com/tools"
  7.  android:layout_width="match_parent"
  8.  android:layout_height="match_parent"
  9.  tools:context=".MainActivity"
  10.  tools:ignore="Deprecated">
  11.  <Button
  12.  android:id="@+id/idStart"
  13.  android:layout_width="200dp"
  14.  android:layout_height="wrap_content"
  15.  android:layout_x="16dp"
  16.  android:layout_y="16dp"
  17.  android:text="Start Notification"/>
  18.  <Button
  19.  android:id="@+id/idStop"
  20.  android:layout_width="200dp"
  21.  android:layout_height="wrap_content"
  22.  android:layout_x="16dp"
  23. android:layout_y="64dp"
  24.  android:text="Stop Notification"/>
  25. </AbsoluteLayout>
  26.  
  27.  
  28. MainActivity.java
  29.  
  30. package com.example.program7;
  31. import android.os.Bundle;
  32. import android.view.View;
  33. import android.widget.Button;
  34. import android.content.Intent;
  35. import androidx.appcompat.app.AppCompatActivity;
  36. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  37.  Button start, stop;
  38.  @Override
  39.  protected void onCreate(Bundle b) {
  40.  super.onCreate(b);
  41.  setContentView(R.layout.activity_main);
  42.  start = findViewById(R.id.idStart);
  43.  stop = findViewById(R.id.idStop);
  44.  start.setOnClickListener(this);
  45.  stop.setOnClickListener(this);
  46.  }
  47.  @Override
  48.  public void onClick(View v) {
  49.  if(v.equals(start)) {
  50.  Intent it = new Intent(this,MyService.class);
  51.  startService(it);
  52.  }
  53.  else if(v.equals(stop)) {
  54.  Intent it = new Intent(this, MyService.class);
  55.  stopService(it);
  56.  }
  57.  }
  58. }
  59.  
  60.  
  61. MyService.java
  62.  
  63. package com.example.program7;
  64. import android.os.Bundle;
  65. import android.os.Handler;
  66. import android.os.IBinder;
  67. import android.os.Message;
  68. import android.app.Service;
  69. import android.widget.Toast;
  70. import android.content.Intent;
  71. import android.app.Notification;
  72. import android.app.PendingIntent;
  73. import androidx.annotation.NonNull;
  74. import android.app.NotificationManager;
  75. import androidx.core.app.NotificationCompat;
  76. public class MyService extends Service {
  77.  boolean isRunning = false;
  78.  MyThread thread;
  79.  @Override
  80.  public void onCreate() {
  81.  super.onCreate();
  82.  isRunning = true;
  83.  Toast.makeText(getBaseContext(),"Service Created", Toast.LENGTH_SHORT).show();
  84.  thread = new MyThread();
  85.  thread.start();
  86.  }
  87.  @Override
  88.  public int onStartCommand(Intent intent, int flags, int startId) {
  89.  super.onStartCommand(intent, flags, startId);
  90.  Toast.makeText(getBaseContext(),"Service Started", Toast.LENGTH_SHORT).show();
  91.  Bundle b = intent.getBundleExtra("data");
  92.  isRunning = b.getBoolean("stop");
  93.  if(!thread.isAlive()) {
  94.  thread = new MyThread();
  95.  thread.start();
  96. }
  97.  return Service.START_NOT_STICKY;
  98.  }
  99.  @Override
  100.  public IBinder onBind(Intent intent) {
  101.  return null;
  102.  }
  103.  @Override
  104.  public void onDestroy() {
  105.  isRunning = false;
  106.  Toast.makeText(getBaseContext(),"Service Stopped", Toast.LENGTH_SHORT).show();
  107.  super.onDestroy();
  108.  }
  109.  Handler hand = new Handler(){
  110.  @Override
  111.  public void handleMessage(@NonNull Message msg) {
  112.  NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  113.  NotificationCompat.Builder builder=new NotificationCompat.Builder(getBaseContext());
  114.  builder.setContentTitle("From Service");
  115.  builder.setContentText("Hi! " + msg.what);
  116.  builder.setSmallIcon(R.drawable.ic_launcher_foreground);
  117.  builder.setContentIntent(PendingIntent.getActivity(getBaseContext(), 1, new
  118. Intent(getBaseContext(),MainActivity.class),Intent.FILL_IN_ACTION));
  119.  manager.notify(1, builder.build());
  120.  }
  121.  };
  122.  private class MyThread extends Thread {
  123.  @Override
  124.  public void run() {
  125.  int i = 0;
  126.  while (isRunning){
  127.  try {
  128.  Thread.sleep(5000);
  129.  }
  130.  catch(InterruptedException e) {
  131. e.printStackTrace();
  132.  }
  133.  hand.sendEmptyMessage(i++);
  134.  }
  135.  }
  136.  }
  137. }
  138.  
  139.  
  140.  
  141. AndroidManifest.xml
  142.  
  143. <service
  144.  android:name=".MyService"
  145.  android:enabled="true"
  146.  android:exported="true"/>
  147.  
  148.  
  149.  
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement