Launching the inbound SMS service in Android Oreo

I am developing an application that should run some code (Networking) whenever an SMS is received.
 In API 25 and below, this is normal, I register the implicit receiverin the manifest file and start my service in the specified class that I expanded BroadcastReceiver. However, in API 26 you cannot register android.provider.Telephony.SMS_RECEIVEDin receiver, since it will not work.

From the documentation for Android:

Note. If your application targets API level 26 or higher, you cannot use the manifest to declare the recipient for implicit translations (translations that are not specifically designed for your application), except for a few implicit translations that are exempt from this restriction. In most cases, you can use scheduled tasks.

I have read several articles, such as this one at the intermediate level . There are solutions like JobScheduleror Explicit Receiver, but the first one is used to change the state of the network, and I could not find a way to start the task for the event SMS_RECEIVED, and the second until your activity was up and running.

Due to the nature of my application, I need to listen to incoming SMS whether the application works or not. How to do this in API 26+?

Edit

, JobInfoBuilder - Android. . Uri, SMS ( , SMS ContentObserver)

+2
2

android O, . , SMS_RECEIVED.

, ():

MainActivity.java:

String action = "START"
final Intent intent = new Intent(this, CallMonitorService.class);
intent.setAction(action);
startService(intent);

CallMonitorService.java onCreate() , BroadcastReceiver callExplicitReceiver :

    final IntentFilter intentFilter = new IntentFilter();
    intentFilter.setPriority(2147483647);
    intentFilter.addAction("android.intent.action.PHONE_STATE");
    this.callExplicitReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
                // do the stuff here
            }
        }
    };
    registerReceiver(callExplicitReceiver, intentFilter);

onStartCommand():

    if (intent.getAction().equals("START")) {
        Intent callServiceNotificationIntent = new Intent(this, MainActivity.class);
        callServiceNotificationIntent.setFlags(
            Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent
            .getActivity(this, CALL_SERVICE_REQUEST_CODE,
                    callServiceNotificationIntent, CALL_SERVICE_FLAG);

        Notification notification = new NotificationCompat.Builder(this)
            .setContentTitle(CALL_NOTIFICATION_CONTENT_TITLE)
            .setTicker(CALL_NOTIFICATION_TICKER)
            .setContentText(CALL_NOTIFICATION_CONTENT_TEXT)
            .setSmallIcon(R.drawable.ic_info_outline_black_24dp)
            .setContentIntent(pendingIntent)
            .setOngoing(true)
            .build();
        startForeground(CALL_NOTIFICATION_ID, notification);
    }

, :

@Override
public void onDestroy() {
    super.onDestroy();
    unregisterReceiver(callExplicitReceiver);
}

, - , Android- Oreo, ( ).

+2

, , SMS_RECEIVED_ACTION list. , , ( ). , , .

+1

Source: https://habr.com/ru/post/1690325/


All Articles