在Android Oreo中的传入SMS上运行服务 [英] Run service on incoming SMS in android Oreo

查看:100
本文介绍了在Android Oreo中的传入SMS上运行服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,无论何时收到短信,该应用程序都需要运行一些代码(联网).
在API 25及更低版本中,我可以在清单文件中注册一个隐式receiver,并在扩展了BroadcastReceiver的指定类中启动我的服务.但是,在API 26中,您无法在receiver中注册android.provider.Telephony.SMS_RECEIVED,因为它不起作用.

I'm developing an app which needs to run some code (Networking) whenever an SMS is received.
In API 25 and lower it's fine, I register an implicit receiver in Manifest file and start my service in the specified class which extended BroadcastReceiver. In API 26 however you cannot register android.provider.Telephony.SMS_RECEIVED in a receiver since it won't work.

来自Android文档:

From Android documentation:

注意:如果您的应用程序的目标是API级别26或更高级别,则您不能使用清单为隐式广播(不专门针对您的应用程序的广播)声明接收方,但一些不受该限制的隐式广播除外.在大多数情况下,您可以改用预定作业.

Note: If your app targets API level 26 or higher, you cannot use the manifest to declare a receiver for implicit broadcasts (broadcasts that do not target your app specifically), except for a few implicit broadcasts that are exempted from that restriction. In most cases, you can use scheduled jobs instead.

我已经阅读了几篇文章,例如

I've read several articles like this one on medium. There are solutions like JobScheduler or Explicit Receiver, however the first one is used for changes in network state and I couldn't find a way to trigger the job on SMS_RECEIVED event and the second one is valid until your activity is up and running.

由于我的应用程序的性质,无论该应用程序是否运行,我都需要监听传入的SMS.如何在API 26+中做到这一点?

Because of the nature of my application I need to listen for incoming SMS whether the app is running or not. How to do that in API 26+?

修改

也许是Android网站上JobInfoBuilder文档中的代码可以帮助您.它监视设备上照片的更改,并在更改后开始作业.但是我找不到合适的Uri来对SMS进行相同操作(甚至不确定是否可以通过ContentObserver监视SMS)

Maybe the code in JobInfoBuilder doc on android website could help. It monitors the changes in the photos on a device and start the job on change. However I cannot find a proper Uri to do the same with the SMS (not even sure if it's possible to monitor SMS via ContentObserver)

推荐答案

由于在android O中有很多方法可以完成这项工作,因此我发布了此答案并提及了解决问题的方法.显然,问题是指一般问题,而不是SMS_RECEIVED接收器本身.

Since there are lots of ways to do the job in android O, I post this answer and mention my approach to solve the problem. Obviously by problem I mean the general problem not the SMS_RECEIVED receiver itself.

我启动前台服务,然后在其中注册一个动态或显式接收器以侦听传入的呼叫(例如):

I start a foreground service and in there I register a dynamic or explicit receiver to listen to the incoming calls (for instance):

MainActivity.java中:

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

CallMonitorService.javaonCreate()方法中,我将BroadcastReceiver callExplicitReceiver作为字段:

In CallMonitorService.javas onCreate() method where I have BroadcastReceiver callExplicitReceiver as a field:

    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想要的,但是通过应用程序中的按钮,用户可以停止该服务和监视接收器是销毁服务的直接结果(我清除了那部分代码).

I think of this as a good approach since the user is notified of the running service because of the undismissable notification and that's what android Oreo wants, however through a button in the app user could stop the service and the monitoring receiver as a direct result of destroying service (I cleared that part of code).

这篇关于在Android Oreo中的传入SMS上运行服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆