[JAVA] Foreground Service, send Intent object and thread

2021. 2. 20. 18:57Android

*Foreground Service를 동작하면서 Thread를 돌리고

Foregroudn Service를 동작할때 Intent에 Intent Object를 전달하는 방법

 

*MainActivity

            Intent getIntent = getIntent();

            testSerializable testSerializable = (com.example.myapplication.testSerializable) getIntent.getSerializableExtra("userIntent");

            if(testSerializable != null){

                Log.d(TAG, "NUM : " + testSerializable.getNum());

            }else{

                Log.d(TAG,"userObject Null");

            }
            

 

1. getIntent를 통해서 Intent값을 불러옴

2. getIntent.getSerializableExtra("userIntent"); 을 통해서 값을 가져옴

 

    Thread th = new Thread (new Runnable() {

        @Override
        public void run() {

            while (b_foregroundService) {
                int intReturn = testS.sendNum(i_number);
                if(i_number<100)
                    Log.d(TAG, i_number + " * " + i_number +" = " + intReturn);
                i_number++;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });

thread 안에서 Serializable 한 객체를를 이용해서 Action을 취한다.

 

    public int onStartCommand(Intent intent, int flags, int startId) {
        String input = intent.getStringExtra("inputExtra");
        createNotificationChannel();
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, 0);

        //end option

        Intent notificationcancleIntent = new Intent(this, MainActivity.class);
        //stop userIntent input
        notificationcancleIntent.setAction("Stop");
        PendingIntent canclependingIntent = PendingIntent.getActivity(this,-99,notificationcancleIntent,PendingIntent.FLAG_UPDATE_CURRENT);


        Notification notification = new NotificationCompat.Builder(this, TAG)
                .setContentTitle("Foreground Service")
                .setContentText(input)
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentIntent(pendingIntent)
                .setStyle(new NotificationCompat.BigTextStyle().bigText("Foreground Service App"))
                .addAction(R.drawable.ic_launcher_foreground,"EXIT", canclependingIntent)
                .setAutoCancel(true)
                .build();

        b_foregroundService = true;
        th.start();
        startForeground(1, notification);
        //do heavy work on a background thread
        //stopSelf();
        return START_NOT_STICKY;        //return super.onStartCommand(intent, flags, startId);
    }

 

Foreground Service를 start할 때 Thread도 시작한다.

Destroy 될 때 Thread도 Interrupt 해야한다.

    public void onDestroy() {
        super.onDestroy();

        th.interrupt();
    }

 

Full Code

더보기

MainActivity

package com.example.myapplication;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Button btnStartService, btnStopService;
    private static final int PERMISSIONS_REQUEST_RECEIVE_SMS = 0;
    private static final String TAG = "SMSReceiver";


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnStartService = findViewById(R.id.btn_Start);
        btnStopService = findViewById(R.id.btn_Stop);

        // Request the permission immediately here for the first time run
        requestPermissions(Manifest.permission.RECEIVE_SMS, PERMISSIONS_REQUEST_RECEIVE_SMS);

        if(fgService.b_foregroundService)
        {
            Intent getIntent = getIntent();

            testSerializable testSerializable = (com.example.myapplication.testSerializable) getIntent.getSerializableExtra("userIntent");

            if(testSerializable != null){

                Log.d(TAG, "NUM : " + testSerializable.getNum());

            }else{

                Log.d(TAG,"userObject Null");

            }

            if(getIntent.getAction()!= null) {

                if (getIntent.getAction().equals("Stop")) {
                    stopService();

                    //direct end
                    moveTaskToBack(true);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        finishAndRemoveTask();
                    }
                    System.exit(0);
                }

            }
        }


        btnStartService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startService();
            }
        });
        btnStopService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopService();
            }
        });

    }

    public void startService() {
        Intent serviceIntent = new Intent(this, fgService.class);
        serviceIntent.putExtra("inputExtra", "Foreground Service Example in Android");
        ContextCompat.startForegroundService(this, serviceIntent);
    }
    public void stopService() {
        Intent serviceIntent = new Intent(this, fgService.class);
        stopService(serviceIntent);
    }


    private void requestPermissions(String permission, int requestCode) {
        // Here, thisActivity is the current activity
        if (ContextCompat.checkSelfPermission(this, permission)
                != PackageManager.PERMISSION_GRANTED) {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {

                // Show an explanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.
                Toast.makeText(this, "Granting permission is necessary!", Toast.LENGTH_LONG).show();

            } else {

                // No explanation needed, we can request the permission.

                ActivityCompat.requestPermissions(this,
                        new String[]{permission},
                        requestCode);

                // requestCode is an
                // app-defined int constant. The callback method gets the
                // result of the request.
            }
        }
    }
}

 

fgService.class

package com.example.myapplication;

import android.Manifest;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

public class fgService extends Service {
    public static BroadcastReceiver mReciver;
    private static final String TAG = "SMSReceiver";
    public static MediaPlayer mp = new MediaPlayer();
    public static boolean b_foregroundService = false;

    public int i_number = 0;
    public static testSerializable testS;


    public static class MyReciver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {

            Toast myToast = Toast.makeText(context.getApplicationContext(),"문자가 도착했습니다!",Toast.LENGTH_LONG);
            myToast.show();

            mp = MediaPlayer.create(context,R.raw.do_sound);
            mp.start();
        }
    }

    public void fgService(){

    }

    @Override
    public void onCreate() {
        super.onCreate();

        IntentFilter filter = new IntentFilter();
        mReciver = new MyReciver();
        registerReceiver(mReciver,filter);

        testS = new testSerializable();
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        String input = intent.getStringExtra("inputExtra");
        createNotificationChannel();
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, 0);

        //end option

        Intent notificationcancleIntent = new Intent(this, MainActivity.class);
        //stop userIntent input
        notificationcancleIntent.setAction("Stop");
        PendingIntent canclependingIntent = PendingIntent.getActivity(this,-99,notificationcancleIntent,PendingIntent.FLAG_UPDATE_CURRENT);


        Notification notification = new NotificationCompat.Builder(this, TAG)
                .setContentTitle("Foreground Service")
                .setContentText(input)
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentIntent(pendingIntent)
                .setStyle(new NotificationCompat.BigTextStyle().bigText("Foreground Service App"))
                .addAction(R.drawable.ic_launcher_foreground,"EXIT", canclependingIntent)
                .setAutoCancel(true)
                .build();

        b_foregroundService = true;
        th.start();
        startForeground(1, notification);
        //do heavy work on a background thread
        //stopSelf();
        return START_NOT_STICKY;        //return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mReciver);
        b_foregroundService = false;

        th.interrupt();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel serviceChannel = new NotificationChannel(
                    TAG,
                    "Foreground Service Channel",
                    NotificationManager.IMPORTANCE_DEFAULT
            );
            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(serviceChannel);
        }
    }


    Thread th = new Thread (new Runnable() {

        @Override
        public void run() {

            while (b_foregroundService) {
                int intReturn = testS.sendNum(i_number);
                if(i_number<100)
                    Log.d(TAG, i_number + " * " + i_number +" = " + intReturn);
                i_number++;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });


}

 

testSerializable.class

package com.example.myapplication;

import java.io.Serializable;

public class testSerializable implements Serializable {

    public static int testNumber;

    public testSerializable() {
    }

    public int sendNum(int myNumber){
        testNumber = myNumber;
        return myNumber*myNumber;
    }

    public int getNum(){
        return testNumber;
    }

}

 

*manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">

    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication">

        <service
            android:name=".fgService"
            android:enabled="true"
            android:exported="true"></service>

        <receiver
            android:name=".fgService$MyReciver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>