Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity"
- tools:ignore="Deprecated">
- <Button
- android:id="@+id/idStart"
- android:layout_width="200dp"
- android:layout_height="wrap_content"
- android:layout_x="16dp"
- android:layout_y="16dp"
- android:text="Start Notification"/>
- <Button
- android:id="@+id/idStop"
- android:layout_width="200dp"
- android:layout_height="wrap_content"
- android:layout_x="16dp"
- android:layout_y="64dp"
- android:text="Stop Notification"/>
- </AbsoluteLayout>
- MainActivity.java
- package com.example.program7;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.content.Intent;
- import androidx.appcompat.app.AppCompatActivity;
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
- Button start, stop;
- @Override
- protected void onCreate(Bundle b) {
- super.onCreate(b);
- setContentView(R.layout.activity_main);
- start = findViewById(R.id.idStart);
- stop = findViewById(R.id.idStop);
- start.setOnClickListener(this);
- stop.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- if(v.equals(start)) {
- Intent it = new Intent(this,MyService.class);
- startService(it);
- }
- else if(v.equals(stop)) {
- Intent it = new Intent(this, MyService.class);
- stopService(it);
- }
- }
- }
- MyService.java
- package com.example.program7;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.IBinder;
- import android.os.Message;
- import android.app.Service;
- import android.widget.Toast;
- import android.content.Intent;
- import android.app.Notification;
- import android.app.PendingIntent;
- import androidx.annotation.NonNull;
- import android.app.NotificationManager;
- import androidx.core.app.NotificationCompat;
- public class MyService extends Service {
- boolean isRunning = false;
- MyThread thread;
- @Override
- public void onCreate() {
- super.onCreate();
- isRunning = true;
- Toast.makeText(getBaseContext(),"Service Created", Toast.LENGTH_SHORT).show();
- thread = new MyThread();
- thread.start();
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- super.onStartCommand(intent, flags, startId);
- Toast.makeText(getBaseContext(),"Service Started", Toast.LENGTH_SHORT).show();
- Bundle b = intent.getBundleExtra("data");
- isRunning = b.getBoolean("stop");
- if(!thread.isAlive()) {
- thread = new MyThread();
- thread.start();
- }
- return Service.START_NOT_STICKY;
- }
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- @Override
- public void onDestroy() {
- isRunning = false;
- Toast.makeText(getBaseContext(),"Service Stopped", Toast.LENGTH_SHORT).show();
- super.onDestroy();
- }
- Handler hand = new Handler(){
- @Override
- public void handleMessage(@NonNull Message msg) {
- NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
- NotificationCompat.Builder builder=new NotificationCompat.Builder(getBaseContext());
- builder.setContentTitle("From Service");
- builder.setContentText("Hi! " + msg.what);
- builder.setSmallIcon(R.drawable.ic_launcher_foreground);
- builder.setContentIntent(PendingIntent.getActivity(getBaseContext(), 1, new
- Intent(getBaseContext(),MainActivity.class),Intent.FILL_IN_ACTION));
- manager.notify(1, builder.build());
- }
- };
- private class MyThread extends Thread {
- @Override
- public void run() {
- int i = 0;
- while (isRunning){
- try {
- Thread.sleep(5000);
- }
- catch(InterruptedException e) {
- e.printStackTrace();
- }
- hand.sendEmptyMessage(i++);
- }
- }
- }
- }
- AndroidManifest.xml
- <service
- android:name=".MyService"
- android:enabled="true"
- android:exported="true"/>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement